R Kable - cell_spec 不工作(PDF 中的表打印 Latex 命令而不是颜色)

问题描述 投票:0回答:0

我正在尝试在 PDF 报告中打印表格。 在此报告中,我想使用 cell_spec(与 kable 一起)更改表格中单元格的格式。 我使用 R-Markdown(使用 PDF 输出的乳胶)创建了这份报告。

在发布之前搜索了许多其他问题,这是最接近的,但接受的答案对我不起作用:Selecting and coloring single table cells with kableExtra in R markdown cell_spec

代码如下:

```
title: "Title"
author: "Me"
output:
  pdf_document:
    toc: yes
    fig_caption: yes
header-includes:
     \usepackage{fancyhdr}
     \usepackage{graphicx}
```

```{r setup, include=FALSE}
#options(tinytex.verbose = TRUE)
knitr::opts_chunk$set(
    echo = FALSE,
    message = FALSE,
    warning = FALSE,
    out.width = "100%"
)

#.1. Load libraries
require("pacman")
p_load(tidyverse,reshape, reshape2, ggplot2, knitr, kableExtra, tinytex, scales)
```
I have a table and I would like the cell on the first row, second column to have a green background.
For the example I am using this code:

```{r, escape = FALSE, results='asis'}

col1=c('Expect_Positive', 'Expect_Negative')
Real_Positive = c(10,1)
Real_Negative=c(3,15)

conf_matrix= as.data.frame(cbind(col1, Real_Positive, Real_Negative))

conf_matrix[1,2]=cell_spec(conf_matrix[1,2], format="latex", background = "green")

conf_matrix  %>%
      kable(format = "latex", booktabs = TRUE,
            caption = paste0("Confusion Matrix Example"),
            align = "c") %>%
      kable_styling(latex_options = c("HOLD_position")) %>%
      kable_styling(font_size = 8.5)
 
```

这是我得到的:

如您所见,它打印了实际命令:

r format latex r-markdown pdflatex
© www.soinside.com 2019 - 2024. All rights reserved.