我在使用shinyjs::toggle时遇到了问题。
我有一个框,显示选定的输入,当用户选择了一个输入时,我想要显示它,当用户没有选择一个输入时,就隐藏它。
shinyUI <- function(id) {
checkBoxGroupInput(inputId = "foo", ......),
div(id=ns("selected_box"),
box(
width = 24,
title = "Selected Foo",
textOutput(ns('selected_foo'))))
}据我理解,这个服务器代码:
shinyjs::toggle(id='selected_box', isTruthy(input$foo)})应该具有与此代码相同的效果:
if(isTruthy(input$foo)) {
shinyjs::show(id='selected_box', anim = TRUE)
}
else {
shinyjs::hide(id='selected_box', anim = TRUE)
}
})但是,当我使用shinyjs::toggle时,selected_box div每次更改input$foo时都会显示/隐藏,而不是只在输入$foo为空时显示/隐藏。
发布于 2022-07-27 21:45:21
您可能想尝试使用conditionalPanel代替
library(shiny)
ui <- fluidPage(
checkboxGroupInput('foo', "foo", c('a', 'b', 'c')),
conditionalPanel(
'typeof input.foo !== "undefined" && input.foo.length > 0',
textOutput('text')
)
)
server <- function(input, output, session) {
output$text <- renderText({"abc"})
}
shinyApp(ui, server)

或者,如果您想使用shinyjs,下面是工作代码
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
checkboxGroupInput('foo', "foo", c('a', 'b', 'c')),
textOutput('text')
)
server <- function(input, output, session) {
observeEvent(input$foo, {
print(is.null(input$foo))
toggle(id='text', condition = !is.null(input$foo))
}, ignoreNULL = FALSE)
output$text <- renderText({"abc"})
}
shinyApp(ui, server)问题是您需要指定condition = xx参数,第二个位置参数是anim而不是condition,不能在这里懒惰。在xD之前也犯过同样的错误。
https://stackoverflow.com/questions/73144525
复制相似问题