你可以使用reactiveValues()
记住检查的值。
我对命名列表进行了编辑以存储复选框的信息。如果您打算选择,这是必要的input A
按字母顺序排列,..
checked <- as.list(rep(T, length(df$b)))
names(checked) <- df$b
在应用程序内,您将必须使用isolate()
as the input$selectinput
更新速度快于input$checkinput
由于某种原因,这会导致选择逻辑出现问题。所以我们只想在发生变化时进行更新input$checkinput
。是的,我使用 for 循环来更新(未)检查值的列表:)。
global <- reactiveValues(checked = checked)
observe({
input$checkinput
isolate({
if(!is.null(input$checkinput)){
possible <- as.character(df$b[df$a %in% input$selectinput])
for(nr in 1:length(possible)){
global$checked[[possible[nr]]] <- (possible %in% input$checkinput)[nr]
}
}
})
})
完整的应用程序将是:
library(shiny)
df <- data.frame(
a = rep(LETTERS[1:3], each = 2),
b = letters[1:6]
)
checked <- as.list(rep(T, length(df$b)))
names(checked) <- df$b
ui <- fluidPage(
column(6, selectInput("selectinput", "Input A", levels(df$a), multiple = T)),
column(6, checkboxGroupInput("checkinput", "Input b", NULL))
)
server <- function(input, output, session) {
global <- reactiveValues(checked = checked)
observe({
input$checkinput
isolate({
if(!is.null(input$checkinput)){
possible <- as.character(df$b[df$a %in% input$selectinput])
for(nr in 1:length(possible)){
global$checked[[possible[nr]]] <- (possible %in% input$checkinput)[nr]
}
}
})
})
observe({
allPossibleB <- df$b[df$a %in% input$selectinput]
updateCheckboxGroupInput(session,
"checkinput", "Input b",
choices = allPossibleB,
selected = df$b[unlist(global$checked)], # <=== previously unchecked instead NULL?
inline = TRUE
)
})
}
shinyApp(ui, server)