I want to make several data sets available for download from the Shiny app. I don't wan to display them or making any other calculations. Just make them downloadable. I've created a subdirectory called data, where I placed several csv files. My current code writes just an empty csv file, and ignores the datasets.
ui <- fluidPage(
selectInput(inputId = "dataset", label = "Select a dataset",
choices = c("",
"a",
"b",
"c")
),
br(),
downloadButton("downloadData", "Download") )
and the server side
server <- function( input, output, session ) {
output$sample <- renderTable( sampleData() )
sampleData <- reactive({
switch(input$dataset,
"a" = read.csv("/data/a.csv"),
"b" = read.csv("/data/a.csv"),
"c" = read.csv("/data/a.csv") )
output$sample <- renderTable( sampleData() )
output$downloaData <- downloadHandler(
filename = function(){
paste("sample_dataset.csv", sep = "")
},
content = function(file) {
write.csv(sampleData(), file, row.names = FALSE)
})
}
Thank you!
EDIT
Here is the second version of the code:
ui <- fluidPage(
fluidRow(
column(6,
selectInput(inputId = "dataset",
label = "Select a sample dataset",
choices = c("",
"a",
"b",
"c",
"d",
"e"
)),
br(),
downloadButton("downloadData", "Download")),
tableOutput('sample'),
)
SERVER:
server< - function( input, output, session ) {
output$sample <- renderTable( sampleData() )
sampleData <- reactive({
switch(input$dataset,
"a" = read.csv("data/a.csv"),
"b" = read.csv("data/b.csv"),
"c" = read.csv("data/c.csv"),
"d" = read.csv("data/d.csv"),
"e" = read.csv("data/e.csv")
)})
output$sample <- renderTable( sampleData() )
output$downloadData <- downloadHandler(
filename = function(){
paste("sample_dataset.csv", sep = "")
},
content = function(file) {
write.csv(sampleData(), file, row.names = FALSE)
})
}