I have produced an app that allows the user to upload multiple csv files.
These csvs then 'rbind' together, 'read.csv' and have a column added to the df which is the filename.
The df is then processed to produce various plots which are downloadable. This works perfectly. However when I try download a specific data frame from within the reactive element I can't get it to work.
In this example I want to download df1 from within the testinput reactive function.
UI:
dashboardPage( skin = "black",
dashboardHeader(title = "myApp"),
dashboardSidebar(collapsed = TRUE,
sidebarMenu(
menuItem("Home", tabName = "dashboard1", icon = icon("home", lib =
"glyphicon"))
)
),
dashboardBody(
tags$head(tags$style(HTML('
.main-header .logo {
font-family: "Times New Roman", serif;
font-weight: bold;
font-size: 24px;
}
'))),
tabItems(
tabItem(tabName = "dashboard1",
fileInput("file1",
label="Input files:",
multiple = TRUE),
downloadButton('downloadData', 'Download Data')
)
)
)
)
Server:
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1))
return ()
else
{
nfiles = nrow(input$file1)
csv = list()
for (i in 1 : nfiles)
{
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1[['name']]
actual_names<-input$statics$name
df1<-cbind(actual_names, csv_names)
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))
df1<-cbind(df1, mydata)
}
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$downloadData, " ",Sys.Date(),".csv",sep="") },
content = function(file) {
write.csv(df1,file)
}
)
)
}
Any help would be great as I have searched lots of SO and other forums and I'm pretty stuck.
@Chabo was correct:
I had to create a new reactive() and then render as an output$ to my UI.
I could then download reactive event
I think you can return the variable you wish to download in the reactive function and then add a downloaddata in the server function like this:
library(shiny)
library(shinydashboard)
#server start
function(input, output) {
testinput<- reactive({
if(is.null(input$file1)){
return ()
}
else{
nfiles = nrow(input$file1)
csv = list()
}
for (i in 1 : nfiles){
csv[[i]] = read.csv(input$file1$datapath[i])
}
csv_names <- input$file1[['name']]
actual_names<-input$statics$name
df1<-cbind(actual_names, csv_names)
mydata <- do.call(rbind, lapply(csv_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))
df1<-cbind(df1, mydata)
return(df1)
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(testinput(), file, row.names = FALSE)
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.