簡體   English   中英

kable 和 kableExtra 未在 markdown 文檔的 pdf 中呈現

[英]kable and kableExtra not rendering in pdf from markdown document

我正在嘗試在從 shiny 應用程序生成的 markdown 文檔中使用 kable 將表格呈現為 pdf。 它呈現正常,直到我使用column_spec的任何功能(下面代碼中的 column_spec),此時我收到以下錯誤:

: LaTeX 錯誤。 數組 arg 中的非法字符。

這個問題看起來很基本,我不是第一個遇到這個問題的人,但其他解決方案(例如thisthis )沒有幫助。 該表呈現為 HTML,但似乎 kableExtra 似乎不知道它正在呈現 latex。如果我將knitr.table.format更改為"pandoc" ,它會呈現但仍然是column_spec行的錯誤。 有任何想法嗎?

用戶界面.R

fluidPage(
  title = 'Download a PDF report',
  sidebarLayout(
    sidebarPanel(
      helpText(),
      div("Create a Mark Down Document"),
      radioButtons('format', 'Document format', c('PDF', 'HTML'),
                   inline = TRUE),
      downloadButton('downloadReport')
    ),
    mainPanel(
      plotOutput('regPlot')
    )
  )
)

服務器.R

library(rmarkdown)
library(shiny)
library(magrittr)
library(kableExtra)

function(input, output) {

  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html'
      ))
    },

    content = function(file) {
      src <- normalizePath('report.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document()
      ))
      file.rename(out, file)
    }
  )

}

報告.Rmd

 Test kable:
 
```{r}

 auto_set_format <- function() { 
   if (knitr::is_latex_output()) { 
     options(knitr.table.format = "latex") 
   } else { 
     options(knitr.table.format = "html") 
   } 
 }

auto_set_format()

tbl <- knitr::kable(mtcars[1:6, 1:6], caption = 'A subset of mtcars.') 

# THIS LINE CAUSES THE PROBEM WHEN WRITING TO PDF. COMMENT OUT AND IT WORKS OK
tbl <- tbl %>% column_spec(3, color = "#62BD19")

tbl
```

對於column_spec()你需要給knitr一個額外的 Latex package ,即將你的報告改為

---
title: "Example Table"
header-includes:
   - \usepackage{dcolumn}
---
 Test kable:
 
```{r}

 auto_set_format <- function() { 
   if (knitr::is_latex_output()) { 
     options(knitr.table.format = "latex") 
   } else { 
     options(knitr.table.format = "html") 
   } 
 }

auto_set_format()

tbl <- knitr::kable(mtcars[1:6, 1:6], caption = 'A subset of mtcars.') %>% 
              column_spec(3, color = "#62BD19")

tbl
```

您將能夠生成這兩種報告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM