【问题标题】:Datasets in RshinyR Shiny 中的数据集
【发布时间】:2021-03-02 15:09:48
【问题描述】:

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

# 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 文件,我想以与上述类似的方式加载和处理/可视化它们。但是,我不确定如何上传这些 .rds 文件并执行类似的任务,谁能帮我解决这个问题?

谢谢

【问题讨论】:

    标签: r shiny shinydashboard shinyapps shiny-reactivity


    【解决方案1】:

    我过去曾将 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)
    

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多