【问题标题】:Display download button in Shiny R only when output appears in Main Panel仅当输出出现在主面板中时,才在 Shiny R 中显示下载按钮
【发布时间】:2018-06-26 19:33:15
【问题描述】:

我有以下代码,它允许接受 csv 文件 -> 运行 R 代码 -> 显示 -> 下载输出。

但是,一旦应用运行,下载按钮就会出现。 有没有办法只在输出文件可用时才显示输出按钮?

下面是我正在使用的代码:

UI.R

library(shiny)

#ui.R
# Define UI for random distribution application 
shinyUI(fluidPage(

  # Application title
  titlePanel("Text Mining on R"),

  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Select the Input File',
                accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
      tags$hr(),
      fileInput('file2', 'Select the UserTopic file',
                accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))

    ),
    mainPanel(
      dataTableOutput('table'),
      downloadButton('OutputFile', 'Download Output File')
    )
  ))
)

Server.R

#server.R
library(shiny)

shinyServer(function(input, output) {

  observe({
    file1 = input$file1
    file2 = input$file2
    if (is.null(file1) || is.null(file2)) {
      return(NULL)
    }
    data1 = read.csv(file1$datapath,header = TRUE, sep=",",skipNul = TRUE)
    data2 = read.csv(file2$datapath,header = TRUE, sep=",",skipNul = TRUE)
   source("RCode.R", local = TRUE)
    #output$table <- renderDataTable(output2)
    output$table <- renderDataTable({
      my_function(file1$datapath,file2$datapath)
    })

    output$OutputFile <- downloadHandler(


      filename = function() {
        paste("OutputFile", "_",Sys.time(),".csv",sep="")
       },

      content = function(file) {


        write.csv(my_function(file1$datapath,file2$datapath), file, sep = ",",
                    row.names = FALSE)
      }
    )
  })

})

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    在服务器端你可以使用:

    output$download <- renderUI({
      if(!is.null(input$file1) & !is.null(input$file2)) {
        downloadButton('OutputFile', 'Download Output File')
      }
    })
    

    在 ui 端,您将下载按钮替换为:

    uiOutput("download")
    

    【讨论】:

      【解决方案2】:

      你也可以使用 req()。

      在用户界面中:

      uiOutput("get_the_item")
      

      在服务器中:

      output$get_the_item <- renderUI({
      req(input$file1, input$file2)
      downloadButton('download_item', label = 'Download item') })
      

      在 renderUI 下方,添加 downloadHandler(使用您的文件名和内容参数完成代码):

      output$download_item <- downloadHandler(
      filename = function() {},
      content = function(file) {}
      )
      

      【讨论】:

      • 强烈推荐使用req()。来自shiny.rstudio.com/articles/req.html:“当您使用 req 检查您的先决条件时,失败不仅会停止当前计算...,还会停止调用堆栈上的任何调用者。”
      【解决方案3】:

      shiny Web 应用程序中隐藏/显示对象的简单方法是使用来自 Dean Attali 的惊人的 shinyjs 包。该软件包有 widley 文档,您可以在 Dean 博客的 this page 上找到完整的工作示例。

      【讨论】:

        【解决方案4】:

        上述解决方案的替代方案,您也可以使用conditionalPanel,如下:

        ui.R

        conditionalPanel("output.fileUploaded",
                           downloadButton('OutputFile', 'Download Output File'))
        

        server.R

        getData <- reactive({
        if(is.null(input$file1) && is.null(input$file2)) 
          {return(NULL)}
        else {return(1)}
        })
        
        output$fileUploaded <- reactive({
        return(!is.null(getData()))
        })
        outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
        

        这只是使用conditionalPaneloutputOptions 的另一种方法。

        【讨论】:

          猜你喜欢
          • 2021-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-17
          • 2017-12-31
          • 2020-10-05
          • 2017-02-14
          • 1970-01-01
          相关资源
          最近更新 更多