简体   繁体   English

使用RMarkdown将带有颜色和unicode字符的kableExtra表渲染为PDF

[英]Rendering kableExtra table with colors and unicode characters to PDF with RMarkdown

I recently learned how to make awesome tables with kableExtra that included unicode symbols, so that they could work as interpretive tables for my plots. 我最近学会了如何使用包含unicode符号的kableExtra创建令人敬畏的表,以便它们可以作为我的绘图的解释表。

library(dplyr)
library(kableExtra)

data.frame(
  Symbol = c("●", "●", "▲"),
  Description = c("bla bla bla", "bla bla bla", "bla bla bla"),
  Result = c("bla bla bla", "bla bla bla", "bla bla bla")
) %>%
  mutate(
    Symbol = cell_spec(Symbol, color = c("yellow", "pink", "pink"), escape = F)
  ) %>%
  kable(escape = F, align = c("c", "l", "l")) %>%
  kable_styling(full_width = F)

Results in a beautiful table that looks like so: 结果在漂亮的表中看起来像这样: 在此输入图像描述

I would now like to automatically render this into a PDF, but when I stick this exact code into an RMarkdown file set to knit to PDF, that led me into an hours long rabbit hole of trying to debug various latex errors associated with the unicode characters in the table, the number-signs in the color specifications causing issues, the footnotes, errors that state ! Misplaced \\noalign. 我现在想把它自动渲染成PDF,但是当我把这个确切的代码粘贴到一个RMarkdown文件集中编织成PDF时,这让我陷入长达一小时的试图调试与unicode角色相关的乳胶错误的兔子洞在表中,数字标志中的数字标志引起问题,脚注,错误表示! Misplaced \\noalign. ! Misplaced \\noalign. that I have yet to figure out the source of, etc. 我还没弄清楚等等的来源

I'll include the text of a full Rmd file at the end of this. 我将在此末尾包含完整Rmd文件的文本。 I'm on a Mac if that makes a difference. 我在Mac上,如果这有所作为。 Using BasicTex and tlmgr to install missing latex packages. 使用BasicTex和tlmgr安装缺少的乳胶包。

Is there either (1) a way to save my kableExtra table, which looks EXACTLY how I want it when rendered within RStudio, to a scalable PDF file, that doesn't involve RMarkdown? 有没有(1)一种方法来保存我的kableExtra表,它看起来在RStudio中呈现时我想要的是一个可扩展的PDF文件,它不涉及RMarkdown? I played around with the write2pdf() function today but ran into unicode issues again. 我今天玩了write2pdf()函数,但又遇到了unicode问题。 I also tried knitting it to html and converting it via a call to pandoc , but that removed most of the table formatting that I need. 我也尝试将它编织成html并通过调用pandoc转换它,但这删除了我需要的大部分表格式。

Maybe I'm missing some easy solution out there, though... or (2) is anyone able to tell me what changes would get this Rmd code to render my chart correctly to PDF? 也许我错过了一些简单的解决方案,但是......或者(2)是否有人能够告诉我哪些更改会使这个Rmd代码正确地将我的图表呈现为PDF? The R code itself works fine. R代码本身工作正常。 It's some kind of Latex issue I guess. 我想这是某种乳胶问题。

Thanks so much for any help. 非常感谢您的帮助。

---
title: ""
output:
  pdf_document:
    latex_engine: xelatex
---

```{r, echo=FALSE, results="asis"}
# TODOREQ: add "much" grammars for acute levels
# TODOREQ: change "levels of concern" to the respective guideline name.
# TODO: Change to groupby/summarize
# TODO: Factor out package loading and global variables into a single setup file

# Packages
suppressMessages(library(ggplot2))
suppressMessages(library(extrafont))
suppressMessages(library(kableExtra))
suppressMessages(library(dplyr))


  table_info <- data.frame(Symbol=c("&#9650", "&#9670", "&#2795"),
                       Name=c("Name1", "Name2", "Name3"),
                       Description=c("Description1", "Description2",
                                     "Description3"),
                       Results=c("Bad", "Good", "Bad"),
                       Color=c("#ffde71", "#394f38",
                               "#09a2dd"))

  ktable <- table_info %>%
    mutate(Symbol = cell_spec(Symbol, color = Color, escape = FALSE)) %>%
    select(-Color) %>%
    kable(escape = FALSE, align = c("c", "l", "l")) %>%
    kable_styling(full_width = FALSE)

  ktable


```

There are a few problems. 有一些问题。

  1. cell_spec is going to insert raw HTML code unless you ask it to insert LaTeX code. cell_spec将插入原始HTML代码,除非您要求它插入LaTeX代码。 Use format = "latex" to do that. 使用format = "latex"来做到这一点。

  2. You need to specify the symbols in LaTeX code, not HTML markup. 您需要在LaTeX代码中指定符号,而不是HTML标记。 xelatex should support things like "\\\\symbol{9650}" for "&#9650" , but I couldn't get anything to show up: maybe I don't have the fonts installed. xelatex应该为"&#9650"支持"\\\\symbol{9650}"的内容,但是我无法显示任何内容:也许我没有安装这些字体。 Instead you can use AMS symbols; 相反,你可以使用AMS符号; a filled circle is "$\\\\bullet$" , an upward pointing filled triangle is "$\\\\blacktriangle$" . 一个圆圈是"$\\\\bullet$" ,向上指向的三角形是"$\\\\blacktriangle$" I didn't look up your others, but if you're lucky, you'll find them in https://www.rpi.edu/dept/arc/training/latex/LaTeX_symbols.pdf . 我没有看你的其他人,但如果你很幸运,你会在https://www.rpi.edu/dept/arc/training/latex/LaTeX_symbols.pdf找到它们。

  3. Regular kable tables are ugly in LaTeX; LaTeX中常规的kable表很难看; use the booktabs = TRUE option for nicer ones. 使用booktabs = TRUE选项booktabs = TRUE更好的选项。

So this is what your ktable specification should look like: 所以这就是你的ktable规范应该是这样的:

  table_info <- data.frame(Symbol=c("$\\bullet$", "$\\bullet$", "$\\blacktriangle$"),
                       Name=c("Name1", "Name2", "Name3"),
                       Description=c("Description1", "Description2",
                                     "Description3"),
                       Results=c("Bad", "Good", "Bad"),
                       Color=c("#ffde71", "#394f38",
                               "#09a2dd"))

  ktable <- table_info %>%
    mutate(Symbol = cell_spec(Symbol, color = Color, format = "latex", 
                              escape = FALSE)) %>%
    select(-Color) %>%
    kable(escape = FALSE, align = c("c", "l", "l"), booktabs = TRUE) %>%
    kable_styling(full_width = FALSE)

  ktable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM