【问题标题】:Exporting save_kable as pdf in Shiny在 Shiny 中将 save_kable 导出为 pdf
【发布时间】:2020-07-09 04:54:33
【问题描述】:

我正在尝试使用 R Shiny 将 kable 表(乳胶)导出到 .pdf 文件。

这是我用来生成 kable 乳胶表的代码:

x<-kable(overall, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
             caption = "Sample Table",  
             escape = FALSE)%>%
      kable_styling(latex_options = c("striped")) %>%
      add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3))%>%
      landscape()

save_kable(x, 'SampleTable.pdf')

我可以在独立的 R 程序中导出它,但我想用 R Shiny 复制导出。我试图将代码包装在 downloadHandler 函数中,但它不起作用。

示例代码:

output$export = downloadHandler(
    filename = function() {"sampleTable.pdf"},
    content = function(file) {
     x<-kable(overall, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
         caption = "Sample Table",  
         escape = FALSE)%>%
  kable_styling(latex_options = c("striped")) %>%
  add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3))%>%
  landscape()

  save_kable(x, file)
    }
  )

任何见解将不胜感激。

【问题讨论】:

  • 当我以这种方式保存 pdf 文件时,我在文件函数中调用 grDevices::pdf(file); plot(x); grDevices::dev.off()。它可能与save_kable 相同,因此请尝试将返回的x 替换为save_kable(x, file)
  • 这似乎不起作用。

标签: r pdf shiny kable kableextra


【解决方案1】:

这对我有用(在安装 pandoctexlive-xetex 之后):


library(shiny)
library(knitr)
library(kableExtra)

library(datasets)
options(knitr.table.format = "latex") # not required in newer versions of kableExtra

server <- function(input, output) {

    # Fill in the spot we created for a plot
    output$phonePlot <- renderPlot({

        # Render a barplot
        barplot(WorldPhones[,input$region]*1000, 
                main=input$region,
                ylab="Number of Telephones",
                xlab="Year")
    })
    output$export = downloadHandler(
        filename = function() {"sampleTable.pdf"},
        content = function(file) {
            x <- kable(WorldPhones, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T,
                     caption = "Sample Table",  
                     escape = FALSE) %>%
                kable_styling(latex_options = c("striped")) %>%
                add_header_above(c(" " = 1, 'Group 1' = 3, 'Group 2' = 3)) %>%
                landscape()

            save_kable(x, file)
        },
        contentType = 'application/pdf'
    )

}

ui <- fluidPage(    

    # Give the page a title
    titlePanel("Telephones by region"),

    # Generate a row with a sidebar
    sidebarLayout(      

        # Define the sidebar with one input
        sidebarPanel(
            selectInput("region", "Region:", 
                        choices=colnames(WorldPhones)),
            hr(),
            helpText("Data from AT&T (1961) The World's Telephones."), 
            shiny::downloadButton("export", "Export")
        ),

        # Create a spot for the barplot
        mainPanel(
            plotOutput("phonePlot")  
        )

    )
)

shinyApp(ui = ui, server = server)

【讨论】:

  • 我无法让它工作。单击“导出”时,我收到以下消息:这是 XeTeX,版本 3.14159265-2.6-0.99999(MiKTeX 2.9.6930 64 位)进入扩展模式。我试图保存文件,但没有写入任何内容。
  • 可能是因为您的计算机上没有安装 MiKTeX 版本。尝试从此处 (miktex.org/download) 下载并安装它,然后重新运行代码。它应该可以工作。
  • 我已经下载了最新版本的 MikTex。不幸的是,这并没有解决问题。
  • 这可能是 MiKTeX 的问题。见stackoverflow.com/questions/50082865/…(一辉的评论)和yihui.org/en/2018/03/miktex-auto-install,开启自动安装缺失包。
  • 我将 MiKTeX 设置为在缺少软件包时自动安装软件包。如果我使用使用 save_kable 的独立 R 程序生成 PDF。不幸的是,它仍然无法在 R Shiny 应用程序中运行。
猜你喜欢
  • 2017-03-09
  • 2020-11-11
  • 2021-10-15
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多