2

我有兴趣使用 patchwork 包来安排一组图,然后最终用户可以使用下载按钮下载这些图。为简单起见,我在此示例中仅显示了一个图。

示例脚本:

library(shiny)
library(tidyverse)
library(Cairo)
library(grDevices)

ui <- fluidPage(

  mainPanel(
    tabsetPanel(
      tabPanel(
        "PDF this plot with patchwork", 
        plotOutput("diamonds"),
        downloadButton("downloadPlot", "Download Plot")
      )
    )
  )
)

server <- function(input, output, session){

  output$diamonds <- renderPlot({
    ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + geom_smooth()
  })

  output$downloadPlot <- downloadHandler(
    filename = "my_plot.pdf",

    content = function(file){
      cairo_pdf(filename = file,
                width = 18, height = 10, pointsize = 12, family = "sans", bg = "transparent",
                antialias = "subpixel",fallback_resolution = 300)
      patchwork::wrap_plots(ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + geom_smooth())
      dev.off()
    },

    contentType = "application/pdf"
  )
}

shinyApp(ui=ui, server=server)

在这种情况下,生成的 pdf 是空白的。我查看了这个相关问题,如果我用 替换patchwork::wrap_plotsgridExtra::grid.arrange应用程序现在可以工作,但是有很多我更喜欢使用的原因patchwork,我没有推荐(情节命名、字幕等)。对于我的真实脚本,我也对使用 Markdown 不感兴趣。

4

0 回答 0