我有以下代码,可以接受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)
}
)
})
})
谢谢!
req()
。来自https://shiny.rstudio.com/articles/req.html: "当您使用req检查前提条件时,失败不仅会停止当前计算...,还会停止调用堆栈上的任何调用者。" - Megatron