我有一个Shiny App,我希望用户每次在上一个selectInput中选择一个包含特定单词而不是确切单词的字符串时,都出现一个conditionalPanel。这是我目前拥有的:
library(shiny)
library(tidyverse)
ui <- fluidPage(
sidebarPanel(
selectInput("input1",
"Select a word:",
choices = c("Word1 something",
"Word2 something",
"Word3 something",
"Word4 something",
"Word1 nothing")
)
)
)
server <- function(input, output){}
shinyApp(ui, server)
如果我可以在conditionalPanel内部使用简单的R代码,它将看起来像这样:
ui <- fluidPage(
sidebarPanel(
selectInput("input1",
"Select a word:",
choices = c("Word1 something",
"Word2 something",
"Word3 something",
"Word4 something",
"Word1 nothing")),
conditionalPanel(
condition = str_detect(input1, "Word1"),
selectInput("input2",
"Select another word:",
choices = c("Word10",
"Word11")))
)
)
server <- function(input, output){}
shinyApp(ui, server)
但是,conditionalPanel需要javascript代码作为条件。如果我想要确切的词,我会使用
"input.input1 == 'Word1 nothing'"
,但这不是我想要的。有人知道我该怎么做吗?提前致谢!
最佳答案
您可以使用indexOf()
javascript方法来返回指定值在字符串中首次出现的位置。如果要搜索的值永不出现,则返回-1。
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("input1",
"Select a word:",
choices = c("Word1 something",
"Word2 something",
"Word3 something",
"Word4 something",
"Word1 nothing")),
conditionalPanel("input.input1.indexOf('Word1') > -1",
selectInput("input2",
"Select another word:",
choices = c("Word10",
"Word11"))
)
)
)
server <- function(input, output, session) ({
})
shinyApp(ui, server)
关于javascript - 如何在Shiny的conditionalPanel中使用str_detect?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53072252/