【问题标题】:conditionalPanel() based on includes logic from stringconditionalPanel() 基于包含来自字符串的逻辑
【发布时间】:2020-05-15 19:56:42
【问题描述】:

我的查询感觉接近this inqury,但提供的支持仅限于应用于一个数据元素的逻辑。

我想学习 JavaScript 以应用于字符串/向量/数组(不确定在我的示例中使用哪种语言),这样如果选择了任何哺乳动物,则会出现消息,但如果选择了任何鸟,该消息不会出现。

library(shiny)

birds <- c("finch","robin","crow","duck")
mammals <- c("elephant", "human", "dog", 'cat')

ui <- fluidPage(
   titlePanel("Select Conditional"),
     mainPanel(
       column(4,
       selectizeInput(inputId = "animals",
                      label = "Select An Animal",
                      choices= list('Examples of Birds' = birds, 
                                    'Example of mammals' = mammals)
       )),
       column(8,
       conditionalPanel(condition = "input.animals.indexOf('birds')",
       textOutput("text")))
     ))

server <- function(input, output) {
      output$text <- renderText({"This is a mammal."})
}

shinyApp(ui = ui, server = server)

我尝试过

conditionalPanel(condition = "input.animals.Array.indexOf('birds')",

conditionalPanel(condition = "input.animals.str.indexOf('birds')"

conditionalPanel(condition = "input.animals.Vector.indexOf('birds')"

conditionalPanel(condition = "input.animals.String.indexOf('birds')"

感谢您的任何想法。请原谅我缺乏关于向量、数组、字符串的知识。

【问题讨论】:

    标签: javascript r shiny conditional-statements


    【解决方案1】:

    R 中的非输入变量不会传递给 JavaScript,作为一种解决方法,我将字符向量构建到 JS 数组中并在条件内传递。 因此,在这种情况下传递的实际条件是 ['elephant', 'human', 'dog', 'cat'].includes(input.animals),它可以满足您的需求。

    library(shiny)
    
    birds <- c("finch","robin","crow","duck")
    mammals <- c("elephant", "human", "dog", 'cat')
    
    ui <- fluidPage(
      titlePanel("Select Conditional"),
      mainPanel(
        column(4,
               selectizeInput(inputId = "animals",
                              label = "Select An Animal",
                              choices= list('Examples of Birds' = birds, 
                                            'Example of mammals' = mammals)
               )),
        column(8,
               conditionalPanel(condition = paste0(paste0("[",toString(paste0("'",mammals,"'")),"]"),".includes(input.animals)"),
                                textOutput("text")))
      ))
    
    server <- function(input, output) {
      output$text <- renderText({"This is a mammal."})
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢您快速且内容丰富的回复。这在我运行条件 = paste0(paste0("[",toString(paste0("'",birds,"'")),"]"),".indexOf(input.animals)" 时有效,但不适用于 .包括
    • 这可能是一个浏览器问题,因为includes 是一个 ES6 命令或者它可能是一个闪亮的版本问题,它适用于带有 Chrome v81.0.4044.138 的 Shiny v1.4.0 和 R v3.6.1 .无论如何,很高兴我能提供帮助。
    • 我没有意识到我必须在浏览器中查看应用程序才能完全看到条件的影响。
    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多