我有以下闪亮的应用程序
library(shiny)
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
options = list(placeholder = 'Get',
onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
choiceValues = list(1, 2, 3),
choiceNames = list(
div(style = "font-size:24px;", "1%"),
div(style = "font-size:24px;", "2%"),
div(style = "font-size:24px;", "3%")
))
),
server = function(input, output, session) {
observeEvent(input$aaa, {
if(grepl("a", input$aaa)){
shinyjs::enable("bbb")
}else{
shinyjs::disable("bbb")
}
})
}
))当我选择a, b, c,时,我将启用Radiobutton,现在如果我删除所有选择,我仍然会看到它处于启用状态。这对我来说很奇怪,因为我希望它会被禁用。然后,在删除所有内容后,如果我在不使用a的情况下重新选择,则它仍然保持启用状态。
有人能告诉我哪里出了问题吗?
发布于 2019-07-04 17:20:58
我做了两个更改,它几乎如您所希望的那样工作:-将单选按钮初始化为禁用-只将一个true/false传递给if
library(shiny)
library(shinyjs)
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
options = list(placeholder = 'Get',
onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
shinyjs::disabled( # buttons disable at the launching
radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
choiceValues = list(1, 2, 3),
choiceNames = list(
div(style = "font-size:24px;", "1%"),
div(style = "font-size:24px;", "2%"),
div(style = "font-size:24px;", "3%")
))
)
),
server = function(input, output, session) {
observeEvent(input$aaa, {
# any(grepl(...)) returns TRUE if one of them is TRUE
# just grepl(..) would return a vector of TRUE and FALSE
if(any(grepl("a", input$aaa))){
shinyjs::enable("bbb")
}else{
shinyjs::disable("bbb")
}
})
}
))我不知道为什么在选择了"a“之后没有选择任何东西的情况下,这些按钮仍然处于启用状态。
编辑:当输入的值为空时,看起来observeEvent没有反应。相反,使用observe()解决了这个问题。
library(shiny)
library(shinyjs)
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
selectizeInput(inputId = "aaa", label = NULL, choices = c('a', 'b', 'c', 'd', 'e', 'f'), selected = NULL, width = '90%',
options = list(placeholder = 'Get',
onInitialize = I('function() { this.setValue(""); }'), maxItems = 5)),
shinyjs::disabled(
radioButtons("bbb", label = NULL, inline = TRUE, width = "100%", selected = 95,
choiceValues = list(1, 2, 3),
choiceNames = list(
div(style = "font-size:24px;", "1%"),
div(style = "font-size:24px;", "2%"),
div(style = "font-size:24px;", "3%")
))
)
),
server = function(input, output, session) {
observe({
if(any(grepl("a", input$aaa))){
shinyjs::enable("bbb")
}else{
shinyjs::disable("bbb")
}
}
)
}
))https://stackoverflow.com/questions/56883944
复制相似问题