首页 > 解决方案 > 使用仪表板中的下拉菜单切换报告时出现未知列错误

问题描述

此错误出现一秒钟然后消失,但我能够看到报告。我有一个演示要展示。请帮我。谢谢你。

library(shiny) 
library(DT)    
library(leaflet)
library(dplyr)

ui <- fluidPage(
  selectInput(
    "FILE", 
    "Select the Report:",
    choices = c("tbl1","tbl2","tbl3")),
  
  checkboxGroupInput(
    "col_n",
    "Columns to display:",
    choices = c()
  ),
  
  DT::dataTableOutput("table_data")  
)

server <- function(input, output,session) {
  
  df <-  reactive ({ 
    switch(input$FILE, 
           "tbl1" = iris, 
           "tbl2" = mtcars,
           "tbl3" = faithful
    )
  })
  
  observeEvent(input$FILE,{
    
    req(df()) 
    updateCheckboxGroupInput(
      session,
      "col_n",
      choices = colnames(df()),
      selected = colnames(df())
    )
  })
  
  output$table_data <-   DT::renderDataTable({
    req(input$col_n) 
    DT::datatable(
      df() %>% select_at(input$col_n),
      rownames = FALSE)
  })
}

shinyApp(ui, server)

标签: rshiny

解决方案


您需要这样做,isolate(df())因为应用程序尝试再次进行子集化而不更改列名值,尽管它们已经消失了。

output$table_data <- DT::renderDataTable({
  isolate(df()) %>% select_at(input$col_n)},
  rownames = FALSE
)

应用程序:

library(shiny) 
library(DT)    
library(dplyr)

ui <- fluidPage(
  selectInput(
    "FILE", 
    "Select the Report:",
    choices = c("tbl1","tbl2","tbl3")),
  
  checkboxGroupInput(
    "col_n",
    "Columns to display:",
    choices = c()
  ),
  
  DT::dataTableOutput("table_data")  
)

server <- function(input, output,session) {
  
  df <-  reactive ({ 
    switch(input$FILE, 
           "tbl1" = iris, 
           "tbl2" = mtcars,
           "tbl3" = faithful
    )
  })
  
  observeEvent(input$FILE,{
    
    req(df()) 
    updateCheckboxGroupInput(
      session,
      "col_n",
      choices = colnames(df()),
      selected = colnames(df())
    )
  })
  
      output$table_data <- DT::renderDataTable({
        isolate(df()) %>% select_at(input$col_n)},
        rownames = FALSE
      )
}

shinyApp(ui, server)

推荐阅读