r - 仅当输出出现在主面板中时,Shiny R中的显示下载按钮

标签 r shiny

我有下面的代码,它允许接受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')
    )
  ))
)

服务器。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)
      }
    )
  })

})

谢谢!

最佳答案

在服务器端,您可以使用:

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

在ui端,您将下载按钮替换为:
uiOutput("download")

关于r - 仅当输出出现在主面板中时,Shiny R中的显示下载按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44179974/

相关文章:

r - 在 R 中使用匹配和应用

r - 计算包含缺失值的相关系数

在 R 中 reshape data.frame

database - 如何从 HTML 网页运行 R 脚本

删除 Shiny 数据表excel输出中标题上方的 "title"行?

r - 通过远离 for 循环来提高性能

google-analytics - Google Analytics 的使用和 GDPR

r - 分析完成后使标签可见

r - 如何在 Shinyapps.io for Google Sheets (googlesheets) 上验证 Shiny 应用程序的用户

r - Shiny:使用shinyjs来获取cookie数据