我有下面的代码,它允许接受 csv 文件 -> 运行 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')
)
))
)
Run Code Online (Sandbox Code Playgroud)
服务器端
#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)
}
)
})
})
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 6
您也可以使用 req()。
在用户界面中:
uiOutput("get_the_item")
Run Code Online (Sandbox Code Playgroud)
在服务器中:
output$get_the_item <- renderUI({
req(input$file1, input$file2)
downloadButton('download_item', label = 'Download item') })
Run Code Online (Sandbox Code Playgroud)
在 renderUI 下方,添加 downloadHandler(使用文件名和内容参数完成代码):
output$download_item <- downloadHandler(
filename = function() {},
content = function(file) {}
)
Run Code Online (Sandbox Code Playgroud)
在服务器端,您可以使用:
output$download <- renderUI({
if(!is.null(input$file1) & !is.null(input$file2)) {
downloadButton('OutputFile', 'Download Output File')
}
})
Run Code Online (Sandbox Code Playgroud)
在 ui 方面,您将下载按钮替换为:
uiOutput("download")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4506 次 |
| 最近记录: |