假设我在Shiny
应用程序下面-
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
checkboxInput("checkbox", label = "Choice a value", value = TRUE),
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
),
server = function(input, output) {
observeEvent(input$checkbox, {
toggleState("variable")
})
}
)
现在,如果单击了checkbox
,那么总是从variable
中选择Cylinders
。
按照上述方法,这是不可能发生的。当前当用户选择其他值i.g时。Gears
在启用variable
之后,然后用户继续使用checkbox
禁用variable
,Gears
的选择仍然保留在UI中,我不想这样做,而且每当variable
被禁用时,都想立即返回Cylinders
。
任何指针如何完成这一点将受到高度赞赏。
bretauv's
回复后的更新-
他的回答对shiny's
本机selectInput()
很有用,但是如果我使用shinyWidgets
包的pickerInput()
,如下所示,似乎不起作用-
library(shiny)
library(shinyjs)
library(shinyWidgets)
shinyApp(
ui = fluidPage(
useShinyjs(),
checkboxInput("checkbox", label = "Choice a value", value = FALSE),
pickerInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
width = "50%",
selected = "gear")
),
server = function(input, output, session) {
observe({
if (input$checkbox) {
toggleState(id = "variable")
} else {
toggleState(id = "variable")
updatePickerInput(session = session,
inputId = "variable", label = NULL,
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
selected = "Cylinders")
}
})
}
)
任何用于解决此问题的指针都将受到高度赞赏。
发布于 2020-03-21 02:02:57
您可以使用updateSelectInput
(不要忘记在function(input, output, session)
中添加session
)。另外,当checkboxInput
是TRUE
或FALSE
时,您需要详细说明情况。
下列守则应能发挥作用:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
checkboxInput("checkbox", label = "Choice a value", value = FALSE),
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
selected = "gear")
),
server = function(input, output, session) {
observe({
if(input$checkbox == "TRUE"){
toggleState(id = "variable")
}
else if(input$checkbox == "FALSE"){
toggleState(id = "variable")
updateSelectInput(session = session,
inputId = "variable",
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
selected = "cyl")
}
})
}
)
https://stackoverflow.com/questions/60786330
复制相似问题