conditionalPanel() 基于包含字符串中的逻辑

我的询问感觉接近于这个问题,但所提供的支持仅限于应用于一个数据元素的逻辑。


我想学习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')"

感谢您的任何想法。请原谅我对向量,数组,字符串缺乏了解。


明月笑刀无情
浏览 72回答 1
1回答

HUWWW

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(&nbsp; titlePanel("Select Conditional"),&nbsp; mainPanel(&nbsp; &nbsp; column(4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;selectizeInput(inputId = "animals",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label = "Select An Animal",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; choices= list('Examples of Birds' = birds,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Example of mammals' = mammals)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)),&nbsp; &nbsp; column(8,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conditionalPanel(condition = paste0(paste0("[",toString(paste0("'",mammals,"'")),"]"),".includes(input.animals)"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textOutput("text")))&nbsp; ))server <- function(input, output) {&nbsp; output$text <- renderText({"This is a mammal."})}shinyApp(ui = ui, server = server)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript