Javascript conditionalPanel()基于字符串中的includes逻辑
我的查询很接近,但提供的支持仅限于应用于一个数据元素的逻辑 我想学习将JavaScript应用于字符串/向量/数组(不确定在我的示例中使用哪种语言),这样,如果选择了任何哺乳动物,消息就会出现,但如果选择了任何鸟类,消息就不会出现Javascript conditionalPanel()基于字符串中的includes逻辑,javascript,r,shiny,conditional-statements,Javascript,R,Shiny,Conditional Statements,我的查询很接近,但提供的支持仅限于应用于一个数据元素的逻辑 我想学习将JavaScript应用于字符串/向量/数组(不确定在我的示例中使用哪种语言),这样,如果选择了任何哺乳动物,消息就会出现,但如果选择了任何鸟类,消息就不会出现 library(shiny) birds <- c("finch","robin","crow","duck") mammals <- c("elephant", "human", "dog", 'cat') ui <- fluidPage(
library(shiny)
birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')
ui <- fluidPage(
titlePanel("Select Conditional"),
mainPanel(
column(4,
selectizeInput(inputId = "animals",
label = "Select An Animal",
choices= list('Examples of Birds' = birds,
'Example of mammals' = mammals)
)),
column(8,
conditionalPanel(condition = "input.animals.indexOf('birds')",
textOutput("text")))
))
server <- function(input, output) {
output$text <- renderText({"This is a mammal."})
}
shinyApp(ui = ui, server = server)
谢谢你的建议。请原谅我对向量、数组、字符串缺乏了解 R中的非输入变量不会传递给JavaScript,作为一种解决方法,我将字符向量构建到JS数组中,并在条件中传递。 因此,在本例中传递的实际条件是
['elephant'、'human'、'dog'、'cat']。包括(input.animals)
,它执行您想要的操作
library(shiny)
birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')
ui <- fluidPage(
titlePanel("Select Conditional"),
mainPanel(
column(4,
selectizeInput(inputId = "animals",
label = "Select An Animal",
choices= list('Examples of Birds' = birds,
'Example of mammals' = mammals)
)),
column(8,
conditionalPanel(condition = paste0(paste0("[",toString(paste0("'",mammals,"'")),"]"),".includes(input.animals)"),
textOutput("text")))
))
server <- function(input, output) {
output$text <- renderText({"This is a mammal."})
}
shinyApp(ui = ui, server = server)
库(闪亮)
感谢您的快速而翔实的回复。当我运行condition=paste0(paste0(“[”),toString(paste0(“”,birds(“”)),“]”)”.indexOf(input.animals)时,这就起作用了,但不适用于。include这可能是浏览器问题,因为includes
是一个ES6命令,也可能是一个闪亮的版本问题,它在闪亮的v1.4.0和R v3.6.1中使用Chrome v81.0.4044.138。无论如何,很高兴我能提供帮助。我没有意识到我必须在浏览器中查看应用程序才能完全看到条件反射的影响。
library(shiny)
birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')
ui <- fluidPage(
titlePanel("Select Conditional"),
mainPanel(
column(4,
selectizeInput(inputId = "animals",
label = "Select An Animal",
choices= list('Examples of Birds' = birds,
'Example of mammals' = mammals)
)),
column(8,
conditionalPanel(condition = paste0(paste0("[",toString(paste0("'",mammals,"'")),"]"),".includes(input.animals)"),
textOutput("text")))
))
server <- function(input, output) {
output$text <- renderText({"This is a mammal."})
}
shinyApp(ui = ui, server = server)