R Shiny 中的数据集

Datasets in Rshiny

我正在学习开发闪亮的应用程序,我已经操纵了一个在线示例来加载多个数据集,然后 select 选择要可视化的数据集。代码如下:

# 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"))
            
        ))
        
)))

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

})

您可能会注意到数据集、iris、mtcars 等是通过库数据集加载的。

问题:我有各种 .rds 文件需要加载,processed/visualised 的加载方式与上述类似。但是,我不确定如何上传这些 .rds 文件并执行类似的任务,有人可以帮我解决这个问题吗?

谢谢

我过去的做法是将 rds 文件存储在应用程序目录中的特定目录中,然后将这些文件读入应用程序。

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)