错误:使用kable

时间:2018-12-04 05:07:42

标签: r latex r-markdown pdflatex kable

我正在尝试使用r markdownkablekableExtra输出乳胶表。我需要使用条件逻辑通过cell_spec将颜色添加到表中。但是在pdf输出中,它显示的乳胶代码如下;

pdf output with latex code

如果我在escape = false中添加kable(),则会出现以下错误,

  

!缺少$。                   $ l.142 sepal_               length&sepal_width&petal_length&petal_width&Species \这是您使用的TeX内存量:14185个字符串   492970 208670个字符串中的3125261 323511个单词中的一个   内存不足3000000 17834个多字符控制序列   15000 + 200000 23725个字词,用于40种字体,总3000000   对于8191中的9000 1141断字例外   41i,9n,38p,1027b,272s个堆栈位置   5000i,500n,10000p,200000b,50000s

我是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")


}


```

2 个答案:

答案 0 :(得分:2)

此错误是由于数据框中带有下划线“ _”引起的。在这种情况下,列名称包含下划线。在TeX中,此符号用于在数学环境中设置下标。这就是为什么在普通文本中使用时必须转义(\_)的原因。删除,转义或将下划线替换为例如

names(data) <- gsub("_", "", names(data))    # remove
names(data) <- gsub("_", "\\_", names(data)) # escape
names(data) <- gsub("_", " ", names(data))   # replace with space

答案 1 :(得分:0)

就我而言,我需要从数据块名称中删除下划线。

例如,这有效:

const data = [{"abc":{"companyCity":"Cupertino","conpanyName":"Apple"}},{"def":{"companyCity":"Mountain View","conpanyName":"Google"}}]

const result = data.flatMap(Object.values)

console.log(result)

但这不起作用:

```{r summaryTableBaseline, results='asis', echo=FALSE}
summaryTableBaseline %>%
    kable(col.names = c("Docker Image", "Min", "Mean", "Median", "Max"),
          caption = "Baseline Benchmark") %>%
    kable_styling()
```