【问题标题】:Outputting Shiny (non-ggplot) plot to PDF将闪亮(非 ggplot)图输出到 PDF
【发布时间】:2014-12-03 16:39:46
【问题描述】:

有没有一种方法可以将(UI 端)Shiny plots 输出为 PDF 供应用程序用户下载?我尝试了各种类似于涉及ggplot的方法,但似乎downloadHandler不能这样操作。例如,以下内容只会生成无法打开的损坏 PDF。

library(shiny)
runApp(list(
  ui = fluidPage(downloadButton('foo')),
  server = function(input, output) {
    plotInput = reactive({
      plot(1:10)
    })
    output$foo = downloadHandler(
      filename = 'test.pdf',
      content = function(file) {
        plotInput()
        dev.copy2pdf(file = file, width=12, height=8, out.type="pdf")
      })
  }
))

非常感谢您的帮助。

【问题讨论】:

  • 作为参考,@Victorp 的以下方法在 shinyapps.ioError: can only print from a screen device 托管时失败。

标签: r shiny knitr


【解决方案1】:

解决了。绘图应使用pdf() 保存在本地,而不是屏幕设备(与dev.copy2pdf 一样)。这是一个工作示例:shiny::runGist('d8d4a14542c0b9d32786')。对于一个不错的基本模型尝试:

server.R

library(shiny)
shinyServer(
    function(input, output) {

        plotInput <- reactive({
            if(input$returnpdf){
                pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h))
                plot(rnorm(sample(100:1000,1)))
                dev.off()
            }
            plot(rnorm(sample(100:1000,1)))
        })

        output$myplot <- renderPlot({ plotInput() })
        output$pdflink <- downloadHandler(
            filename <- "myplot.pdf",
            content <- function(file) {
                file.copy("plot.pdf", file)
            }
        )
    }
)

ui.R

require(shiny)
pageWithSidebar(
    headerPanel("Output to PDF"),
    sidebarPanel(
        checkboxInput('returnpdf', 'output pdf?', FALSE),
        conditionalPanel(
            condition = "input.returnpdf == true",
            strong("PDF size (inches):"),
            sliderInput(inputId="w", label = "width:", min=3, max=20, value=8, width=100, ticks=F),
            sliderInput(inputId="h", label = "height:", min=3, max=20, value=6, width=100, ticks=F),
            br(),
            downloadLink('pdflink')
        )
    ),
    mainPanel({ mainPanel(plotOutput("myplot")) })
)

【讨论】:

    【解决方案2】:

    (你好),请使用pdf

    library(shiny)
    runApp(list(
      ui = fluidPage(downloadButton('foo')),
      server = function(input, output) {
        plotInput = reactive({
          plot(1:10)
        })
        output$foo = downloadHandler(
          filename = 'test.pdf',
          content = function(file) {
            pdf(file = file, width=12, height=8)
            plotInput()
            dev.off()
          })
      }
    ))
    

    编辑:我不知道......这很奇怪。一种解决方法是使用 dev.copy2pdf 就像您最初所做的那样,但在 reactive 函数中而不是 downloadHandler

    ## server.R
    library(shiny)
    shinyServer(
      function(input, output) {
        plotInput <- reactive({plot(rnorm(1000))
                               dev.copy2pdf(file = "plot.pdf")
                               })
        output$myplot <- renderPlot({ plotInput() })
        output$foo <- downloadHandler(
          filename <- "plot.pdf",
          content <- function(file) {
            file.copy("plot.pdf", file)
          })
      }
    )
    

    【讨论】:

    • 是的,对不起 Victorp,我需要重新打开它,因为解决方案是特定于 Windows 的,而且对于托管在 shinyapps.io 的应用程序也会失败。
    • 没问题,我很好奇答案。
    • 您是否尝试将contentType = "application/pdf" 作为参数添加到downloadHandler?
    • 破解了 - 查看其他答案
    • 如何将它与 knitr 一起使用以创建带有简单 ggvis 图的 pdf(通过 latex 或 rmarkdown)?
    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多