Vis*_*ish 3 latex r pdflatex r-markdown kable
我试图输出使用乳胶表r markdown,kable和kableExtra。我需要使用条件逻辑将颜色添加到带有 cell_spec 的表格中。但是对于pdf输出,它显示的乳胶代码如下;
如果我添加escape = false的kable(),它给了我下面的错误,
!缺少 $ 插入。$ l.142 sepal_length & sepal_width & petal_length & petal_width & Species\ 以下是您使用了多少 TeX 的内存: 14185 个字符串,共 492970 208670 个字符串字符,共 3125261 323511 个字的内存,共 30008004 个控制序列,共 30008004 个多列序列+200000 40 种字体的 23725 字字体信息,9000 的 3000000 字中的 1141 断字异常,8191 41i,9n,38p,1027b,272s 堆栈位置中的 5000i,5000p,000s,5000s,5000s
我是新来的rmarkdown和乳胶,请帮我解决这个问题。谢谢你。
这是我的 rmd 文件代码:
---
title: "Iris Data Table"
output: pdf_document
header-includes: \usepackage [table]{xcolor}
geometry: margin = 1cm
params:
n: NA
datafile: "//ad.monash.edu/home/User076/vbed0001/Documents/IRIS.csv" #always set the absolute full path
---
```{r, echo=FALSE, message=FALSE}
d <- read.csv(params$datafile, header = TRUE, sep = ",")
# this uses to remove the warning messages from the pdf file
library(memisc, warn.conflicts = FALSE, quietly=TRUE)
# the package order is important, always kableExtra at the top
#options(kableExtra.latex.load_packages = FALSE)
library(kableExtra)
library(magrittr)
library(formattable)
library(dplyr)
library(knitr)
library(devtools)
options(knitr.table.format = "latex")
if(params$n == "set"){
dtset <- d %>% filter(Species == "setosa")
dtset <- d %>% filter(Species == "setosa")
dtset %>%
mutate(
sepal_length = cell_spec(sepal_length,format = "latex",background = (ifelse(sepal_length>4.5,"#D3D3D3","#ff0000")))
)%>%
kable(format = "latex",caption = "Setosa Table")%>%
kable_styling(position = "center",bootstrap_options = "bordered")
}
```
Run Code Online (Sandbox Code Playgroud)
This error is a result of having underscores "_" in your dataframe. In this case, the column names contain underscores. In TeX this symbol is used to set subscripts in math environments. This is why it has to be escaped (\_) when used in normal text. Remove, escape or replace the underscores with e.g.
names(data) <- gsub("_", "", names(data)) # remove
names(data) <- gsub("_", "\\_", names(data)) # escape
names(data) <- gsub("_", " ", names(data)) # replace with space
Run Code Online (Sandbox Code Playgroud)