如何有条件地下载情节?

How to conditionally download a plot?

下面的可重现代码允许用户 select 数据 table 或数据图以供查看(通过 input$view)。我正在尝试围绕 downloadHandler() 创建一个条件,以便如果用户正在查看数据 table 并选择下载,则下载数据;否则,如果用户正在查看绘图并选择下载,则会下载 PNG 格式的绘图。我 运行 关注 input$view 反应性问题。我将如何修改下面的代码以有条件地下载用户正在查看的任何内容(数据或绘图)?

下面发布的代码可用于查看数据或绘图,但只允许下载数据 table。注释掉否则会导致崩溃的违规代码行。

可重现代码:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  radioButtons("view",
               label = "View data or plot",
               choiceNames = c('Data','Plot'),
               choiceValues = c('viewData','viewPlot'),
               selected = 'viewData',
               inline = TRUE
  ),
  conditionalPanel("input.view == 'viewData'",tableOutput("DF")),
  conditionalPanel("input.view == 'viewPlot'",plotOutput("plotDF")),
  downloadButton("download","Download",style = "width:20%;")
)

server <- function(input, output, session) {
  
  data <- data.frame(Period = c(1,2,3,4,5,6),Value = c(10,20,15,40,35,30))
  
  data1 <- reactiveValues()
  
  inputView <- reactive(input$view) # attempt to make input$view reactive
  
  observeEvent(input$view,{data1$plot <- ggplot(data, aes(Period,Value)) + geom_line()})
  
  output$DF <- renderTable(data)
  output$plotDF <- renderPlot(data1$plot)
  
  output$download <-
    # if(inputView() == 'viewData'){
      downloadHandler(
        filename = function() 
        paste("dataDownload","csv",sep="."),
        content = function(file){
          write.table( 
            data,
            na = "", 
            file, 
            sep = ",", 
            col.names = TRUE, 
            row.names = FALSE)
        }
      )
    # }
  
    # else{
    #   downloadHandler(
    #     filename = function(){paste("plotDownload",'.png',sep='')},
    #     content = function(file){
    #       ggsave(file,plot=data1$plot)
    #     }
    #   )
    # }
  
}

shinyApp(ui, server)

试试这个

library(shiny)
library(ggplot2)

ui <- fluidPage(
  radioButtons("view",
               label = "View data or plot",
               choiceNames = c('Data','Plot'),
               choiceValues = c('viewData','viewPlot'),
               selected = 'viewData',
               inline = TRUE
  ),
  conditionalPanel("input.view == 'viewData'",tableOutput("DF")),
  conditionalPanel("input.view == 'viewPlot'",plotOutput("plotDF")),
  #downloadButton("download","Download",style = "width:20%;")
  uiOutput("plotrtable")
)

server <- function(input, output, session) {
  
  data <- data.frame(Period = c(1,2,3,4,5,6),Value = c(10,20,15,40,35,30))
  
  data1 <- reactiveValues()
  
  inputView <- reactive(input$view) # attempt to make input$view reactive
  
  observeEvent(input$view,{data1$plot <- ggplot(data, aes(Period,Value)) + geom_line()})
  
  output$DF <- renderTable(data)
  output$plotDF <- renderPlot(data1$plot)
  
  output$plotrtable <- renderUI({
    if(input$view == 'viewData'){downloadButton("download","Download",style = "width:20%;") }
    else {downloadButton("downloadp","Download",style = "width:20%;") }
  })
  
  output$download <-  downloadHandler(
      filename = function() 
        paste("dataDownload","csv",sep="."),
      content = function(file){
        write.table( 
          data,
          na = "", 
          file, 
          sep = ",", 
          col.names = TRUE, 
          row.names = FALSE)
      }
  )
  
  output$downloadp <- downloadHandler(
      filename = function(){paste("plotDownload",'.png',sep='')},
      content = function(file){
        ggsave(file,plot=data1$plot)
      }
  )
  
}

shinyApp(ui, server)