1

I have the following R Shiny code that displays CSV results. For the time being, it only works with R's default datasets. I'd want to display the CSV results in the same way for datasets on my local machine.

Examples CSVs: Study1.csv, Study2.csv, Study3.csv

Could someone maybe explain how to accomplish this in R shiny?

ui <- fluidPage(
  
  # App title ----
  titlePanel("Downloading Data"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Choose dataset ----
      selectInput("dataset", "Choose a dataset:",
                  choices = c("rock", "pressure", "cars")),
      
      # Button
      downloadButton("downloadData", "Download")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      tableOutput("table")
      
    )
    
  )
)

server <- function(input, output) {
  
  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  
  # Table of selected dataset ----
  output$table <- renderTable({
    datasetInput()
  })
  
  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )
  
}

shinyApp(ui, server)
Kumar
  • 55
  • 5

1 Answers1

0

I added a dataset:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
    header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))

You can also read a csv and use it as dataset 1 via

dataset1 <- read.csv("your path",sep="your seperator")

Your UI with the added dataset:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
        header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
    -3L))
ui <- fluidPage(
              
              # App title ----
              titlePanel("Downloading Data"),
              
              # Sidebar layout with input and output definitions ----
              sidebarLayout(
                        
                        # Sidebar panel for inputs ----
                        sidebarPanel(
                                  
                                  # Input: Choose dataset ----
                                  selectInput("dataset", "Choose a dataset:",
                                              choices = c("rock", "pressure", "cars", "dataset1")),
                                  
                                  # Button
                                  downloadButton("downloadData", "Download")
                                  
                        ),
                        
                        # Main panel for displaying outputs ----
                        mainPanel(
                                  
                                  tableOutput("table")
                                  
                        )
                        
              )
    )

Your server with added dataset1

server <- function(input, output) {
          
          # Reactive value for selected dataset ----
          datasetInput <- reactive({
                    switch(input$dataset,
                           "rock" = rock,
                           "pressure" = pressure,
                           "cars" = cars,
                           "dataset1" = dataset1)
          })
          
          # Table of selected dataset ----
          output$table <- renderTable({
                    datasetInput()
          })
          
          # Downloadable csv of selected dataset ----
          output$downloadData <- downloadHandler(
                    filename = function() {
                              paste(input$dataset, ".csv", sep = "")
                    },
                    content = function(file) {
                              write.csv(datasetInput(), file, row.names = FALSE)
                    }
          )
          
}

shinyApp(ui, server)
pbraeutigm
  • 455
  • 4
  • 8
  • When I used this code, it worked perfectly. I did the same thing in my old code, however, the result button isn't working. Could you please assist me in resolving the problem? The following is the URL to the question: https://stackoverflow.com/q/67606990/15418723 – Kumar May 20 '21 at 03:15