我正在开发一个Shiny App,我在不同的地方创建了一些数据,我想使用checkBoxGroup列出已经创建的数据。
因此,假设有3个数据和2个已经创建,这两个应该打勾并清空仍然为空的那个。在下面我的猜测哪个不起作用。
提前致谢
if (interactive()) {
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Input checkbox",c("data A", "data B", "data C"))
)
server <- function(input, output, session) {
data_A <- NULL
data_B <- 1
data_C <- 3
observe({
a <- data_A
b <- data_B
c <- data_C
selectedBox <- which(!c(is.null(a),is.null(b),is.null(c)))
updateCheckboxGroupInput(session, "inCheckboxGroup2",
choices = c("Data A", "Data B", "Data C"),
selected = selectedBox
)
})
}
shinyApp(ui, server)
}
答案 0 :(得分:1)
你离我不远!这对我有用
ui <- fluidPage(
checkboxGroupInput("inCheckboxGroup", "Input checkbox",c("data A", "data B", "data C")),
textOutput("showData"),
useShinyjs()
)
server <- function(input, output, session) {
loadData <- reactive({
runjs("Shiny.onInputChange('dataLoaded',true)")
list(
data_A = NULL,
data_B = 1,
data_C = 3
)
})
observeEvent({input$dataLoaded},{
selectedBox <- which(!sapply(loadData(),is.null))
updateCheckboxGroupInput(session, "inCheckboxGroup",
choices = c("Data A", "Data B", "Data C"),
selected = c("Data A", "Data B", "Data C")[selectedBox]
)
})
output$showData <- renderText(
paste(sapply(loadData(),print),collapse = "\n")
)
}
shinyApp(ui, server)
希望它有所帮助!