Angelo
Angelo

Reputation: 5059

Datasets in Rshiny

I am learning to develop shiny apps, I have manipulated an online example to load multiple datasets and then select the dataset of choice to be visualized. The code is as follows:

# ui.R

library(shiny)
library(shinythemes)
# Define UI for dataset viewer application
shinyUI(fluidPage(theme = shinytheme("cerulean"), pageWithSidebar(
    
    # Application title
    headerPanel("Shiny App with multiple datasets"),
    # Sidebar with controls to select a dataset and specify the number
    # of observations to view
    sidebarPanel(
        selectInput("dataset", "Choose a dataset:", 
                    choices = c("rock", "pressure", "cars","iris")),
        
        numericInput("obs", "Number of observations to view:",100)
    ),
    
    # Show a summary of the dataset and an HTML table with the requested
    # number of observations
    mainPanel(
        tabsetPanel(
            tabPanel('Summary Stats', verbatimTextOutput("summary")),
            tabPanel('Table', DT::DTOutput("view"))
            
        ))
        
)))

and

# server.R

library(shiny)
library(datasets)

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
  
  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars,
           "iris" = iris)
  })
  
  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })
  
  # Show the first "n" observations
  output$view <- DT::renderDT({
    head(datasetInput(), n = input$obs)
  })

})

As you may notice that the datasets, iris, mtcars,etc are loaded through the library dataset.

Problem: I have various .rds files that I would like to be loaded and processed/visualised in a similar way as above. However, I am not sure how to upload these .rds files and perform similar task, could anyone please help me with this?

Thank you

Upvotes: 1

Views: 376

Answers (1)

csmontt
csmontt

Reputation: 624

I have done in the past by storing the rds files in an specific directory within your app directory and reading those files into the app.

library(shiny)
library(shinythemes)
library(datasets)

# saved some date as rds files to test app

iris <- iris
saveRDS(iris, "./data/iris.rds")
mtcars <- mtcars
saveRDS(mtcars, "./data/mtcars.rds")

# Getting the file names
rdsfiles <- list.files("./data", pattern = "\\.rds$")

# Define UI for dataset viewer application
ui <- shinyUI(fluidPage(theme = shinytheme("cerulean"), pageWithSidebar(

 # Application title
 headerPanel("Shiny App with multiple datasets"),
 # Sidebar with controls to select a dataset and specify the number
 # of observations to view
 sidebarPanel(
   selectInput("dataset", "Choose a dataset:", 
               choices = rdsfiles),

   numericInput("obs", "Number of observations to view:",100)
   ),

   # Show a summary of the dataset and an HTML table with the requested
   # number of observations
   mainPanel(
    tabsetPanel(
    tabPanel('Summary Stats', verbatimTextOutput("summary")),
    tabPanel('Table', DT::DTOutput("view"))
  
  ))

)))


# Define server logic required to summarize and view the selected dataset
server <- shinyServer(function(input, output) {

# Return the requested dataset
datasetInput <- reactive({
   df <- readRDS(paste0("./data/", input$dataset))
   return(df)
})

 # Generate a summary of the dataset
 output$summary <- renderPrint({
  dataset <- datasetInput()
  summary(dataset)
 })

 # Show the first "n" observations
 output$view <- DT::renderDT({
   head(datasetInput(), n = input$obs)
 })

})


shinyApp(ui, server)

Upvotes: 3

Related Questions