I have a Shiny App where I want a conditionalPanel to appear every time the user selects a string containing a specific word, but not the exact word, in a previous selectInput. Here is what I currently have:
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)
If I could use simple R code inside of the conditionalPanel, it would look like this:
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)
However, conditionalPanel requires javascript code as a condition. If I wanted the exact word, I would use "input.input1 == 'Word1 nothing'"
but that's not what I'm looking for. Anyone knows how I can do this?
Thanks in advance!
Following up on my comment, here's an alternative way -
library(shiny)
library(stringr)
ui <- fluidPage(
sidebarPanel(
selectInput("input1",
"Select a word:",
choices = c("Word1 something",
"Word2 something",
"Word3 something",
"Word4 something",
"Word1 nothing")),
uiOutput("cond_input")
)
)
server <- function(input, output, session) ({
output$cond_input <- renderUI({
req(str_detect(input$input1, "Word1"))
selectInput("input2",
"Select another word:",
choices = c("Word10",
"Word11"))
})
})
shinyApp(ui, server)
You can use indexOf()
javascript method which returns the position of the first occurrence of a specified value in a string. It returns -1 if the value to search for never occurs.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.