R 将可另存为pdf导出到

R 将可另存为pdf导出到,r,pdf,shiny,kable,kableextra,R,Pdf,Shiny,Kable,Kableextra,我正在尝试使用R将kable table latex导出为.pdf文件 下面是我用来生成kable latex表的代码: x<-kable(overall, align = c('l', 'c', 'c', 'c', 'c', 'c', 'c'), "latex", booktabs = T, caption = "Sample Table", escape = FALSE)%>% kable_styling(lat

我正在尝试使用R将kable table latex导出为.pdf文件

下面是我用来生成kable latex表的代码:

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复制导出。我试图将代码包装到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)
    }
  )

如有任何见解,将不胜感激

在安装pandoc和texlive xetex后,这对我来说很有效:


当我以这种方式保存pdf文件时,我调用grDevices::pdffile;plotx;grDevices::dev.off在文件函数中。它可能与save_kable的工作原理相同,因此请尝试将返回的x替换为save_kablex,file。这似乎不起作用。我无法使其工作。单击“导出”时,我收到以下消息:这是XeTeX,版本3.14159265-2.6-0.99999 MiKTeX 2.9.6930 64位进入扩展模式。我试图保存文件,但没有写入任何内容。可能是因为您的计算机上没有安装MiKTeX版本。尝试从此处下载并安装,然后重新运行代码。应该能用。我已经下载了MikTex的最新版本。不幸的是,这并没有解决问题。这可能是MiKTeX的问题。请参阅Yihui的评论和,并启用丢失软件包的自动安装。我已将MiKTeX设置为在丢失软件包时自动安装。如果使用save___;kable的独立R程序,则会生成PDF。不幸的是,它在R Shining应用程序中仍然不起作用。

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)