如何有条件下载图?

发布于 2025-01-31 03:24:49 字数 1865 浏览 4 评论 0原文

以下可重现的代码允许用户选择数据表或查看数据的图(通过Input $ view)。我正在尝试围绕下载handler()创建条件,以便如果用户正在查看数据表并选择下载,则下载数据;否则,如果用户正在查看绘图并选择下载,则下载了PNG格式的图。我正在遇到输入$ view反应性的问题。我将如何修改以下代码以有条件地下载用户正在查看的任何(数据或图)?

下面发布的代码可用于查看数据或绘图,但仅允许下载数据表。否则会导致崩溃的代码线被评论。

可重复的代码:

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)

The below reproducible code allows the user to select either a data table or a plot of the data for viewing (via input$view). I'm trying to create a conditional around the downloadHandler() so that if the user is viewing the data table and chooses to download, then the data is downloaded; otherwise if the user is viewing the plot and chooses to download then a plot in PNG format is downloaded. I'm running into issues around input$view reactivity. How would I modify the code below to conditionally download whichever (data or plot) the user is viewing?

The code as posted below works for viewing either data or plot, but only allows the data table to be downloaded. Offending lines of code that otherwise cause a crash are commented out.

Reproducible code:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2025-02-07 03:24:49

尝试一下

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)

Try this

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文