[R] 中的 kableExtra 突然崩溃

Sudden collapse of kableExtra in [R]

我可能应该指出,我对使用 RMarkdownkableExtra R 包还是很陌生,但我有一份上周可以编织的文档,现在不再编织了文档没有物理变化。我收到的错误信息如下

Error in save_kable_latex(x, file, latex_header_includes, keep_tex) :我们在尝试使用 magick 读取生成的 PDF 文件时遇到错误.您可以检查您的 magick 安装并尝试使用 magick::image_read 手动阅读 PDF 文件。也有可能你没有安装 ghostscript。调用 ... -> as_image -> save_kable -> save_kable_latex 执行暂停

我已经通过重新安装 magick R 包、安装 ghostscript(通过 Homebrew)等尝试了所有我能想到的方法

下面给出的代码块似乎是问题发生的地方,其中 tab2 是一个数据框,其中一些元素是 LaTeX 表达式,例如 "\sum_x f(x)*\left ( pe(x) - lcl(x) \right )".

kable( tab2, format="latex", escape=FALSE, align="c", col.names=NULL ) %>%
  kable_styling( latex_options=c('hold_position') ) %>%
  footnote( general="Given x successes out of n trials, the holistic Jeffreys 0*(1-\\alpha)\\%$ Lower $\\textit{Credible}$ Limit is the value $p$ such that $\\int_0^p \\frac{t^{x+0.5-1}(1-t)^{n-x+0.5-1}}{B(x+0.5,n-x+0.5)} dt = \\alpha$ where B(a,b) is the Beta function given by $\\int_0^1 t^{(x-1)}(1-t)^{(y-1)} dt$.",
           general_title="", threeparttable = TRUE,
            footnote_as_chunk=TRUE, escape=FALSE ) %>%
  as_image( file="tab2.png", width=8, units="in" )

并稍后在新幻灯片上使用 include_graphics() 功能打印为 PDF。

任何帮助将不胜感激,因为这是工作演示。

编辑#1

根据要求,这是一个最低限度的工作示例

prob.success <- sample( seq(.5,.99,.01), size=1 )
conf.alpha <- sample( seq(.5,.99,.01), size=1 )

tab1 <- data.frame( x=0:5, f=round(dbinom(0:5,5,prob.success),3) ) %>%
  mutate( pe=x/5, lcl=qbeta(1-conf.alpha,x+0.5,5-0:5+0.5) ) %>%
  mutate( lcl=pmin(pe,lcl) ) %>%
  mutate( delta=pe-lcl ) %>%
  mutate( f_delta=f*delta )

exp.expr <- "\sum_x f(x)*\left ( pe(x) - lcl(x) \right )"
exp.delta <- format( round(sum( tab1$f_delta ),4), nsmall=4 )

tab2 <- tab1 %>%
  mutate( x=as.character(x), f=format(round(f,4),nsmall=4) ) %>%
  mutate( pe=format(round(pe,4),nsmall=4) ) %>%
  mutate( lcl=format(round(lcl,4),nsmall=4) ) %>%
  mutate( delta=format(round(delta,4),nsmall=3) ) %>%
  mutate( f_delta=format(round(f_delta,4),nsmall=4) ) %>%
  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="",f_delta="") ) %>%
  rbind( ., data.frame(x="", f="", pe="Exp", lcl="Diff", delta="=", f_delta=exp.expr) ) %>%
  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="=",f_delta=exp.delta) ) %>%
  rbind( data.frame(x="x",f="f(x)",pe="pe(x)",lcl="lcl(x)",delta="pe(x)-lcl(x)",
                    f_delta="f(x)\times\left(pe(x)-lcl(x)\right)"), . )

编辑 #2

这些是 .Rmd 文件中使用的 R 包

library( knitr )
library( tibble )
library( magrittr )
library( dplyr )
library( kableExtra )
library( stringr )
library( magick )

您似乎错过了 table 中的 $ 标记。

prob.success <- sample( seq(.5,.99,.01), size=1 )
conf.alpha <- sample( seq(.5,.99,.01), size=1 )

tab1 <- data.frame( x=0:5, f=round(dbinom(0:5,5,prob.success),3) ) %>%
  mutate( pe=x/5, lcl=qbeta(1-conf.alpha,x+0.5,5-0:5+0.5) ) %>%
  mutate( lcl=pmin(pe,lcl) ) %>%
  mutate( delta=pe-lcl ) %>%
  mutate( f_delta=f*delta )

exp.expr <- "$\sum_x f(x)*\left ( pe(x) - lcl(x) \right )$"  # <- Here
exp.delta <- format( round(sum( tab1$f_delta ),4), nsmall=4 )

tab2 <- tab1 %>%
  mutate( x=as.character(x), f=format(round(f,4),nsmall=4) ) %>%
  mutate( pe=format(round(pe,4),nsmall=4) ) %>%
  mutate( lcl=format(round(lcl,4),nsmall=4) ) %>%
  mutate( delta=format(round(delta,4),nsmall=3) ) %>%
  mutate( f_delta=format(round(f_delta,4),nsmall=4) ) %>%
  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="",f_delta="") ) %>%
  rbind( ., data.frame(x="", f="", pe="Exp", lcl="Diff", delta="=", f_delta=exp.expr) ) %>%
  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="=",f_delta=exp.delta) ) %>%
  rbind( data.frame(x="x",f="f(x)",pe="pe(x)",lcl="lcl(x)",delta="pe(x)-lcl(x)",
                    f_delta="$f(x)\times\left(pe(x)-lcl(x)\right)$"), . )  # <- And here

在与 kableExtra 作者 Hao Zhu 的几封电子邮件之后,建议使用 HTML table(而不是 LaTeX)。结果,以下代码能够成功呈现。非常感谢郝

对原始 post 的更改包括

exp.expr <- "$\sum_x f(x)*\left ( pe(x) - lcl(x) \right )$"
tab2 <- tab1 %>%
  mutate( x=as.character(x), f=format(round(f,4),nsmall=4) ) %>%
  mutate( pe=format(round(pe,4),nsmall=4) ) %>%
  mutate( lcl=format(round(lcl,4),nsmall=4) ) %>%
  mutate( delta=format(round(delta,4),nsmall=3) ) %>%
  mutate( f_delta=format(round(f_delta,4),nsmall=4) ) %>%
#  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="",f_delta="") ) %>%
  rbind( ., data.frame(x="", f="", pe="Exp", lcl="Diff", delta="=", f_delta=exp.expr) ) %>%
  rbind( ., data.frame(x="",f="",pe="",lcl="",delta="=",f_delta=exp.delta) ) # %>%
#  rbind( data.frame(x="x",f="f(x)",pe="pe(x)",lcl="lcl(x)",delta="pe(x)-lcl(x)",
#                    f_delta="f(x)\times\left(pe(x)-lcl(x)\right)"), . )
tab.cols <- c( "x", "f(x)", "pe(x)", "lcl(x)", "pe(x)-lcl(x)",
                "$f(x)\times\left(pe(x)-lcl(x)\right)$" )
kable( tab2, format="html", escape=FALSE, align="c", col.names=tab.cols ) %>%
  kable_styling( "striped", full_width = F, position="center" ) %>%
  footnote( general="Given x successes out of n trials, the holistic Jeffreys 0*(1-\alpha)\%$ Lower *Credible* Limit is the value $p$ such that $\int_0^p \frac{t^{x+0.5-1}(1-t)^{n-x+0.5-1}}{B(x+0.5,n-x+0.5)} dt = \alpha$ where B(a,b) is the Beta function given by $\int_0^1 t^{(x-1)}(1-t)^{(y-1)} dt$.",
           general_title="Note:", footnote_as_chunk=TRUE, escape=FALSE )