Error: Missing $ inserted when using kable
Error: Missing $ inserted when using kable
我正在尝试使用 r markdown
、kable
和 kableExtra
输出乳胶 table。我需要使用条件逻辑为 table 和 cell_spec 添加颜色。但是对于 pdf 输出,它显示乳胶代码如下;
pdf output with latex code
如果我在 kable()
中添加 escape = false
,则会出现以下错误,
! Missing $ inserted.
$ l.142 sepal_
length & sepal_width & petal_length & petal_width & Species\ Here is how much of TeX's memory you used: 14185 strings
out of 492970 208670 string characters out of 3125261 323511 words
of memory out of 3000000 17834 multiletter control sequences out of
15000+200000 23725 words of font info for 40 fonts, out of 3000000
for 9000 1141 hyphenation exceptions out of 8191
41i,9n,38p,1027b,272s stack positions out of
5000i,500n,10000p,200000b,50000s
我是 rmarkdown
和 latex 的新手,请帮助我解决问题。谢谢你。
这是我的 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")
}
```
此错误是由于您的数据框中包含下划线“_”造成的。在这种情况下,列名称包含下划线。在 TeX 中,此符号用于在数学环境中设置下标。这就是为什么在普通文本中使用时必须对其进行转义 (\_
) 的原因。删除、转义或替换下划线,例如
names(data) <- gsub("_", "", names(data)) # remove
names(data) <- gsub("_", "\_", names(data)) # escape
names(data) <- gsub("_", " ", names(data)) # replace with space
在我的例子中,我需要从块名称中删除下划线。
例如,这个有效:
```{r summaryTableBaseline, results='asis', echo=FALSE}
summaryTableBaseline %>%
kable(col.names = c("Docker Image", "Min", "Mean", "Median", "Max"),
caption = "Baseline Benchmark") %>%
kable_styling()
```
但这不起作用:
```{r summary_Table_Baseline, results='asis', echo=FALSE}
summaryTableBaseline %>%
kable(col.names = c("Docker Image", "Min", "Mean", "Median", "Max"),
caption = "Baseline Benchmark") %>%
kable_styling()
```
我正在尝试使用 r markdown
、kable
和 kableExtra
输出乳胶 table。我需要使用条件逻辑为 table 和 cell_spec 添加颜色。但是对于 pdf 输出,它显示乳胶代码如下;
pdf output with latex code
如果我在 kable()
中添加 escape = false
,则会出现以下错误,
! Missing $ inserted. $ l.142 sepal_ length & sepal_width & petal_length & petal_width & Species\ Here is how much of TeX's memory you used: 14185 strings out of 492970 208670 string characters out of 3125261 323511 words of memory out of 3000000 17834 multiletter control sequences out of 15000+200000 23725 words of font info for 40 fonts, out of 3000000 for 9000 1141 hyphenation exceptions out of 8191 41i,9n,38p,1027b,272s stack positions out of 5000i,500n,10000p,200000b,50000s
我是 rmarkdown
和 latex 的新手,请帮助我解决问题。谢谢你。
这是我的 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")
}
```
此错误是由于您的数据框中包含下划线“_”造成的。在这种情况下,列名称包含下划线。在 TeX 中,此符号用于在数学环境中设置下标。这就是为什么在普通文本中使用时必须对其进行转义 (\_
) 的原因。删除、转义或替换下划线,例如
names(data) <- gsub("_", "", names(data)) # remove
names(data) <- gsub("_", "\_", names(data)) # escape
names(data) <- gsub("_", " ", names(data)) # replace with space
在我的例子中,我需要从块名称中删除下划线。
例如,这个有效:
```{r summaryTableBaseline, results='asis', echo=FALSE}
summaryTableBaseline %>%
kable(col.names = c("Docker Image", "Min", "Mean", "Median", "Max"),
caption = "Baseline Benchmark") %>%
kable_styling()
```
但这不起作用:
```{r summary_Table_Baseline, results='asis', echo=FALSE}
summaryTableBaseline %>%
kable(col.names = c("Docker Image", "Min", "Mean", "Median", "Max"),
caption = "Baseline Benchmark") %>%
kable_styling()
```