【发布时间】: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)
【问题讨论】: