我在字符列中有一个带有字符串<NA>
的数据框。如果我将kable
应用于此数据框,则不会出现该字符串。
实际上,它与数据帧无关,如以下示例所示:
kable(c("a",NA,"b","<NA>","c","NA","d"))
先打印最后一个NA
(查找我的问题,我已经看到它取决于options(knitr.kable.NA
),但是中间的<NA>
不是。
通过此测试,您可以在下面查看我的输入和输出文件。
会话信息:
> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 rstudioapi_0.9.0 knitr_1.24
[4] magrittr_1.5 ggpubr_0.2.1 tidyselect_0.2.5
[7] munsell_0.5.0 colorspace_1.4-0 R6_2.4.0
[10] rlang_0.4.0 factoextra_1.0.5 highr_0.7
[13] dplyr_0.8.3 tools_3.5.2 grid_3.5.2
[16] gtable_0.2.0 xfun_0.8 htmltools_0.3.6
[19] digest_0.6.20 yaml_2.2.0 lazyeval_0.2.2
[22] assertthat_0.2.1 tibble_2.1.3 ggsignif_0.5.0
[25] crayon_1.3.4 purrr_0.3.2 ggplot2_3.2.0
[28] bitops_1.0-6 rsconnect_0.8.13 RCurl_1.95-4.12
[31] ggrepel_0.8.1 evaluate_0.14 glue_1.3.1
[34] rmarkdown_1.14 compiler_3.5.2 pillar_1.4.2
[37] scales_1.0.0 pkgconfig_2.0.2
RStudio版本1.1.463。
为什么?是否有option
可以打印?另一种方式?
谢谢!
答案 0 :(得分:1)
我认为有两种解决方法,请根据所需的输出进行选择:
R-markdown:
---
title: "testNA"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
df <- data.frame(a= c ("a",NA,"b","<NA>","c","NA","d"), b = 1:7,
stringsAsFactors = FALSE)
```
```{r}
knitr::kable(df)
```
```{r}
knitr::kable(df, format="html")
```
```{r}
ischr <- sapply(df, function(a) is.character(a) | is.factor(a))
df[ischr] <- lapply(df[ischr], htmltools::htmlEscape)
knitr::kable(df)
```