Sudden collapse of kableExtra in [R]

北城以北 提交于 2020-05-15 08:05:41

问题


I should probably point out that I am still fairly new to working with RMarkdown and the kableExtra R package, but I have a document that was knitable last week and now no longer knits despite no physical changes to the document. The error message I receive is the following

Error in save_kable_latex(x, file, latex_header_includes, keep_tex) : We hit an error when trying to use magick to read the generated PDF file. You may check your magick installation and try to use the magick::image_read to read the PDF file manually. It's also possible that you didn't have ghostscript installed. Calls ... -> as_image -> save_kable -> save_kable_latex Execution halted

I have tried everything that I can think of by re-installing the magick R package, installing ghostscript (through Homebrew), etc.

And the code chunk given below seems to be where the issues are occurring, where tab2 is a data frame with some of its elements being a LaTeX expressions such as "\\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 $100*(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" )

and printed to the PDF later on using the include_graphics() function on a new slide.

Any assistance would be greatly appreciated as this is for a work presentation.

EDIT #1

As requested, here is a Minimum Working Example

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)"), . )

EDIT #2

And these are the R packages used in the .Rmd file

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

回答1:


After several emails with the kableExtra author Hao Zhu, it was suggested that a HTML table (instead of LaTeX) should be used. As a result, the following code was able to render successfully. Many thanks to Hao.

Changes to the original post include

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 $100*(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 )



回答2:


It seems like you missed the $ marks in your 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


来源:https://stackoverflow.com/questions/61343132/sudden-collapse-of-kableextra-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!