【发布时间】:2018-11-02 12:26:19
【问题描述】:
我有一个闪亮的应用程序,我希望每次用户在以前的 selectInput 中选择包含特定单词但不是确切单词的字符串时出现一个条件面板。这是我目前拥有的:
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)
如果我可以在条件面板中使用简单的 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'",但这不是我想要的。有谁知道我该怎么做?
提前致谢!
【问题讨论】:
-
你可以在条件面板外使用
str_detect和 if 语句来创建反应变量吗?然后使用is.null或==some value等逻辑表达式检查反应变量并在面板内运行条件语句? -
一种明显的方法是使用
uiOutput并将条件放在server一侧的renderUI中。但是,很高兴看看ui本身是否有办法做到这一点。
标签: javascript r shiny