编程语言
首页 > 编程语言> > javascript-r Shiny-conditionalPanel有条件:检查列表中的项目

javascript-r Shiny-conditionalPanel有条件:检查列表中的项目

作者:互联网

我有2个列表,每个列表包含以另一个变量d为条件的多个ID p_id.

d1 <- as.list(unique(df$p_id[df$d==1]))
d2 <- as.list(unique(df$p_id[df$d==2]))

我想在我闪亮的应用程序中添加一个conditionalPanel来相应地显示/隐藏selectInputwidget.

在我的UIdashboardPage,dashboardBody中,我具有以下内容:

box(
                  conditionalPanel(
                    condition = "input.p_id.indexOf('d1')!=-1" 
                    , selectInput(
                      inputId = "d_number"
                      ,label = "Select Day:"
                      ,choices = list("Day 1" = "1")
                      ,selected = "1"
                    )
                  ),

                  conditionalPanel(
                    condition = "input.p_id.indexOf('d2')!=-1" 
                    , selectInput(
                      inputId = "d_number"
                      ,label = "Select Day:"
                      ,choices = list("Day 1" = "1", "Day 2" = "2")
                      ,selected = "1"
                      )
                  )          
                ),

我的理解是条件必须在js中而不是r中.例如,我正在尝试为第一个条件复制p_id%in%d1.但是,这不起作用.

我尝试过condition =“ input.p_id.indexOf(d1)!=-1”,但它也无法正常工作.

任何人都可以建议我要实现的正确的js语法是什么?谢谢!

解决方法:

我认为您可以以更简单的方式实现所需的功能,而无需使用conditionalPanels.我们可以生成一次selectInput,然后在其他输入发生更改时使用updateSelectInput对其进行更新.这是一个工作示例:

library(shiny)

ui = fluidPage(
  selectInput('maxdays','Max number of days:', c(1,2,3)),
  selectInput('days','Days:',c(1))
)

server = function(input, output, session) {

  observe({
    updateSelectInput(session,'days',choices=seq(1,input$maxdays))
  })
}

runApp(shinyApp(ui = ui, server = server))

另一种解决方案是,每当第一个selectInput更改时,使用renderUI重新渲染selectInput:

library(shiny)

ui = fluidPage(
  selectInput('maxdays','Max number of days:', c(1,2,3)),
  uiOutput('uiDays')
)

server = function(input, output, session) {

  output$uiDays <- renderUI({
    selectInput('days','Days:', choices=seq(1,input$maxdays))
  })

}

runApp(shinyApp(ui = ui, server = server))

希望这可以帮助!

标签:shiny,javascript,r
来源: https://codeday.me/bug/20191025/1928087.html