如何在Shiny的conditionalPanel中使用str_detect?

时间:2018-10-30 20:16:25

标签: javascript r shiny

我有一个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'",但这不是我想要的。有人知道我该怎么做吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

关注我的评论,这是另一种方法-

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)

答案 1 :(得分:1)

您可以使用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)
相关问题