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