【问题标题】:Dynamically display column names in shiny app flashes error when dataset is changed更改数据集时,在闪亮的应用程序中动态显示列名闪烁错误
【发布时间】:2019-07-16 00:24:17
【问题描述】:

我有一个闪亮的应用程序,我希望允许用户根据一组上传的文件选择数据集,然后指定要从所选数据集中显示的列。如果我选择了某些列然后切换数据集,则会闪烁错误并输出到控制台,说明在应用程序切换数据集并正确显示之前,所选列是未知的。然而,在我的完整应用程序中,应用程序崩溃了,尽管我无法弄清楚如何重现崩溃。我认为这可能与一些预处理有关,这些预处理是为了添加跨数据集相同且保持选中状态的其他列,但如果没有该功能,错误是相同的。

library(shiny)
library(tidyverse)
library(DT)


ui <- fluidPage(
  checkboxGroupInput("select_var", label = "Select Variables"),
  selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
  DT::dataTableOutput("table")

)


server <- function(session, input, output) {
  # define the dataset
  data <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})

  # add a common column name that is always selected
  dataprocessed <- reactive({data <- data()
                             data$num <- seq(1:nrow(data))
                             return(data)})

  # dynamically generate the variable names
  observe({
    vchoices <- names(dataprocessed())
    updateCheckboxGroupInput(session, "select_var", choices = vchoices, selected = c("num"))
  })

  # select the variables based on checkbox
  data_sel <- reactive({
    req(input$select_var)
    df_sel <- dataprocessed() %>% select(input$select_var) 
      })

  output$table <- DT::renderDataTable(data_sel())
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny dplyr


    【解决方案1】:

    我们可以在渲染前使用req()添加条件要求来测试列是否存在:

    library(shiny)
    library(tidyverse)
    library(DT)
    
    ui <- fluidPage(
      checkboxGroupInput("select_var", label = "Select Variables"),
      selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
      DT::dataTableOutput("table")
    
    )
    
    server <- function(session, input, output) {
      # define the dataset
      data <- reactive({
        switch(input$dataset,"rock" = rock,"mtcars" = mtcars)
      })
    
      # add a common column name that is always selected
      dataprocessed <- reactive({
        data <- data()
        data$num <- seq(1:nrow(data))
        return(data)
      })
    
      # dynamically generate the variable names
      observe({
        vchoices <- names(dataprocessed())
        updateCheckboxGroupInput(session, "select_var", choices = vchoices, selected = c("num"))
      })
    
      # select the variables based on checkbox
      data_sel <- reactive({
        req(input$select_var)
        req(names(dataprocessed()) %in% input$select_var)
        a <- names(dataprocessed())[names(dataprocessed()) %in% input$select_var]
        df_sel <- dataprocessed() %>% select(a) 
      })
    
      output$table <- DT::renderDataTable(data_sel())
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 这仍然会向控制台输出错误。因为input$selet_var 有根据数据更新它的观察者,看起来应该没有问题。是时间问题吗?
    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2016-01-19
    • 2017-09-30
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多