【问题标题】:How is it possible to save/download a barplot in R shiny?如何在 R Shiny 中保存/下载条形图?
【发布时间】:2018-06-13 19:50:29
【问题描述】:

我想以闪亮的形式保存/下载我的barplots。我用ggplotggsave 做到了,这是可能的,但我怎么能用barplot() 做到这一点?我在ui.R 中的代码是:

library(shiny)
library(shinydashboard)
library(ggplot2)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(
      title = "", 
      status = "danger", 
      solidHeader = TRUE,
      plotOutput(outputId = "myPlotMdata1")
    ),

    box(
      title = "", 
      status = "danger", 
      solidHeader = TRUE,
      plotOutput(outputId = "myPlotMdata2")
    ),

    box(
      title = "", 
      status = "danger", 
      solidHeader = TRUE,
      plotOutput(outputId = "myPlotMdata3")
    ),

    box(
      title = "", 
      status = "danger", 
      solidHeader = TRUE,
      plotOutput(outputId = "myPlotMdata4")
    ),
    box(
      title = "Download", 
      status = "success", 
      solidHeader = TRUE,
      width = 12,
      radioButtons("formatTopwords", "Document format", c("PNG"="png", "EPS"="eps", "PDF"="pdf"), inline = TRUE),
      downloadButton("downloadReportTopwords")
    )
  )
) 

server <- function(input, output) {
  output$myPlotMdata1 <- renderPlot({
    barplot(TopWords$lassoInfPos, las = 2, names.arg = TopWords$informedPos, main = "Informed Investor Top 15 positive words", ylab = "Lasso coefficient")
  })

  output$myPlotMdata2 <- renderPlot({
    barplot(TopWords$lassoNoisePos , las = 2, names.arg = TopWords$noisePos, main = "Noise Investor Top 15 positive words", ylab = "Lasso coefficient")
  })

  output$myPlotMdata3 <- renderPlot({
    barplot(TopWords$lassoInfNeg, las = 2, names.arg = TopWords$informedNeg, main = "Informed Investor Top 15 negative words", ylab = "Lasso coefficient")
  })

  output$myPlotMdata4 <- renderPlot({
    barplot(TopWords$lassoNoiseNeg, las = 2, names.arg = TopWords$noiseNeg, main = "Noise Investor Top 15 negative words", ylab = "Lasso coefficient")
  })

  fn <- reactive({paste("Plot",input$formatTopwords,sep = ".")})
  d <- reactive({input$formatTopwords})

  output$downloadReportTopwords <- downloadHandler(
    filename = fn,
    content = function(file) {
      #ggsave I use for another function, how can I save barplots here
      ggsave(file, device=d(), dpi = 600, width = 297, height = 210, units = "mm")
    }
  )
}  

shinyApp(ui, server)

【问题讨论】:

  • 您的应用程序不可重现,没有数据也没有条形图。你能包括一些虚拟数据吗..
  • @SeGa 立即查看。但重要的任务是情节的下载。我的数据框包含 70.000 行,所以我不能包含虚拟数据
  • 我知道,但是如果没有一个正常运行的应用程序,每个人都必须编写自己的虚拟数据来检查它是否工作,除非有人已经有了解决方案。无需添加您的 data.frame,只需创建一个随机的,可能只有一个数字列并且只包含一个条形图。这样可以更轻松地浏览代码并专注于重要的事情。

标签: r download shiny bar-chart


【解决方案1】:

我不确定 EPS,但以下示例适用于 PNG 和 PDF。您可以创建一个绘图函数,然后在 renderPlotdownloadHandler 中调用该函数。

library(shiny)
library(shinyjs)
library(shinydashboard)
library(ggplot2)

TopWords = data.frame(
  lassoInfPos = runif(100,1,100),
  lassoNoisePos = runif(100,1,50),
  lassoInfNeg = runif(100,1,20),
  lassoNoiseNeg = runif(100,1,10)
)

ui <- {dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(width = 6,
      plotOutput("myPlotMdata1"),
      plotOutput("myPlotMdata2")
    ),
    box(width = 6,
      plotOutput("myPlotMdata3"),
      plotOutput("myPlotMdata4")
    ),
    box(
      title = "Download", 
      status = "success", 
      solidHeader = TRUE,
      width = 12,
      radioButtons("formatTopwords", "Document format", c("PNG"="png", "EPS"="eps", "PDF"="pdf"), inline = TRUE),
      downloadButton("downloadReportTopwords")
    )
  )
)}



barplot_func <- function(input, main) {
  barplot(input, las = 2, #names.arg = TopWords$informedPos, 
          main = main, ylab = "Lasso coefficient")
}

server <- function(input, output) {
  output$myPlotMdata1 <- renderPlot({
    barplot_func(TopWords$lassoInfPos, "Informed Investor Top 15 positive words")
  })
  output$myPlotMdata2 <- renderPlot({
    barplot_func(TopWords$lassoNoisePos, "Informed Investor Top 15 positive words")
  })
  output$myPlotMdata3 <- renderPlot({
    barplot_func(TopWords$lassoInfNeg, "Informed Investor Top 15 negative  words")
  })
  output$myPlotMdata4 <- renderPlot({
    barplot_func(TopWords$lassoNoiseNeg, "Informed Investor Top 15 negative  words")
  })

  fn <- reactive({paste("Plot",input$formatTopwords,sep = ".")})
  d <- reactive({input$formatTopwords})

  output$downloadReportTopwords <- downloadHandler(
    filename = fn,
    content = function(file) {
      if (input$formatTopwords == "png") {
        png(file)
      } else if(input$formatTopwords == "pdf") {
        pdf(file)
      }
      par(mfrow=c(2,2))
      barplot_func(TopWords$lassoInfPos, "Informed Investor Top 15 positive words")
      barplot_func(TopWords$lassoNoisePos, "Informed Investor Top 15 positive words")
      barplot_func(TopWords$lassoInfNeg, "Informed Investor Top 15 negative  words")
      barplot_func(TopWords$lassoNoiseNeg, "Informed Investor Top 15 negative  words")
      dev.off() 
    }
  )
}  

shinyApp(ui, server)

【讨论】:

  • 完美的兄弟!它工作得很好!非常感谢。但我正在绘制 4 个不同的条形图。是否也可以一起下载,就像一页一样?
  • 是的,你可以,我刚刚编辑了我的答案,尽管这样做很丑陋。但是您可以将 par(mfrow()) 设置为您喜欢的任何内容,然后在其中添加所有图。但我实际上会创建一个绘图函数,它使用某些参数创建条形图,然后您只需在 renderPlot 函数和 downloadHandler 中调用该函数。
  • 我编辑了答案。它的绘图功能要好得多。 :)
  • 非常感谢,简直完美!看看我的其他问题,我无法解决它:/ stackoverflow.com/questions/50856951/…
猜你喜欢
  • 2020-10-04
  • 2021-11-02
  • 1970-01-01
  • 2022-11-29
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
相关资源
最近更新 更多