【问题标题】:How can I use str_detect inside a conditionalPanel in Shiny?如何在 Shiny 的条件面板中使用 str_detect?
【发布时间】: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


【解决方案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)

【讨论】:

    【解决方案2】:

    跟进我的评论,这是另一种方式 -

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 2016-08-14
      • 2018-11-29
      • 2015-07-07
      • 2016-06-04
      • 2020-06-03
      • 2016-04-12
      • 2018-06-12
      相关资源
      最近更新 更多