嗨我有桌子,我想用kable打印。
ds <- read.table(header = TRUE, text ="
g1 color
A red
A yellow
B red
C red
C yellow
")
所以,让我们说当打印到pdf的时候,如果它包含&#34;黄色&#34;可能会让整行显得格格不入。
提前谢谢。 甲答案 0 :(得分:1)
使用套餐:&#39; formattable&#39;在html中执行此操作。不确定pdf。
library(formattable)
ds <- read.table(header = TRUE, text ="
g1 color
A red
A yellow
B red
C red
C yellow
")
formattable(ds, list(
area(row = which(ds$color == "yellow")) ~ formatter("span",
style = "text-decoration:line-through")))
如果你想使用kable,你可以使用formatter函数改变行并使用kableExtras包输出到html
library(kableExtra)
library(formattable)
library(dplyr)
ds <- read.table(header = TRUE, text ="
g1 color
A red
A yellow
B red
C red
C yellow
")
ds <- ds %>% mutate(g1 = formatter("span",
style = x ~ style("text-decoration" = ifelse(ds$color == "yellow", "line-through", NA)))(g1),
color = formatter("span",
style = x ~ style("text-decoration" = ifelse(ds$color == "yellow", "line-through", NA)))(color))
kable(ds, "html", escape = F)
此时你可以编织为html并使用html到pdf转换器并从R调用系统命令。
system("wkhtmltopdf --javascript-delay 1 --dpi 300 in.html out.pdf")
答案 1 :(得分:1)
帽子提示Hao Zhu
library(kableExtra)
library(formattable)
library(dplyr)
ds <- read.table(header = TRUE, text ="
g1 color
A red
A yellow
B red
C red
C yellow
")
ds %>% as.data.frame() %>% mutate_if(is.factor,as.character) %>%
mutate(
g1 = ifelse(
color == "yellow",
paste0("\\sout{", g1, "}"), g1
), color = ifelse(
color == "yellow",
paste0("\\sout{", color, "}"), color
)
) %>%
kable("latex", escape = F, booktabs = T)
注意:编译时需要包含ulem TeX包(例如,Rmd文档的YAML标题)