Student
Student

Reputation: 35

Read and write csv file in R shiny

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)
    }) 
}

Upvotes: 0

Views: 1399

Answers (1)

ViviG
ViviG

Reputation: 1726

You are missing a few things in your code:

1 - you are missing )} closing sampleData <- reactive

2 - You have an extra /before data in read.csv

3 - DownloadData is missing a lower-case d in Render

server <- function( input, output, session ) {

output$sample <- renderTable( sampleData() )



sampleData <- reactive({
     switch(input$dataset, 
           
            "a"               = read.csv("data/a.csv"), # you had an extra / before data
            "b"               = read.csv("data/a.csv"),
            "c"                = read.csv("data/a.csv") )}) # you were missing `)}` here

output$sample <- renderTable( sampleData() )
  
output$downloadData <- downloadHandler( #Here was DownloadData misspelled
      filename = function(){
      paste("sample_dataset.csv", sep = "")
    },
    
    content = function(file) {
    write.csv(sampleData(), file, row.names = FALSE)
    })
 }

Upvotes: 1

Related Questions