r shiny - conditionalPanel with condition:检查列表中的项目

vyswwuz2  于 2023-03-27  发布在  其他
关注(0)|答案(3)|浏览(138)

我有两个列表,每个列表包含多个id p_id,条件是另一个变量d

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

我想在我的闪亮应用程序中添加一个conditionalPanel来显示/隐藏相应的selectInput小部件。
在我的UI dashboardPagedashboardBody中,我有以下内容:

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"
                      )
                  )          
                ),

我的理解是,condition必须在js中,而不是在r中。例如,我试图为第一个条件复制p_id %in% d1。然而,这不起作用。
我试过condition = "input.p_id.indexOf(d1)!=-1",但它也不工作。
任何人都可以建议什么是正确的js语法,我试图实现?谢谢!

yzuktlbb

yzuktlbb1#

我认为你可以用一种更简单的方式来实现你想要的,而不需要使用conditionalPanels.。我们可以生成一次selectInput,然后每当其他input发生变化时,就用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))

另一种解决方案是使用renderUI在第一个selectInput更改时重新呈现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))

希望这有帮助!

wxclj1h5

wxclj1h52#

感谢@Florian。我修改了他的答案,这里有一个实现所需行为的可能方法:

library(shiny)

ui = fluidPage(
  selectInput('p_id','ID:', c(111,222,333)),
  uiOutput('uiID')
)

server = function(input, output, session) {

  maxdays <- reactive({
    if(input$p_id %in% c(111)){
      x = 1
    }else{
      if(input$p_id %in% c(222)){
        x = 2
      }else
        x = 3 
    }
    return(x)
  })

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

}

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

在我的应用程序中,列表c(111,222,333)和相应的列表是数据集中的变量,并在脚本的开头定义。

vcudknz3

vcudknz33#

对于任何想要使用conditionalPanel的人,条件是用Javascript编写的,因此您可以使用includes()方法。
IMO的好处是它包含在一个modalDialog中。

library(shiny)

##conditionally show another part of modal on checkbox
ui <- fluidPage(
  actionButton('go', "Go!")
)

server <- function(input, output, session) {
  observeEvent(input$go, {
    shiny::showModal(
      modalDialog(title = "Look at this modal!",
        shiny::checkboxGroupInput(inputId = "cond_inp",
                            label = "Select bits of modal to see:",
                            choices = c("One", "Two", "Three")),
        conditionalPanel(
          condition = "input.cond_inp.includes('One')",
          textInput("one", "Part one:")
        ),
        conditionalPanel(
          condition = "input.cond_inp.includes('Two')",
          textInput("two", "Part two:")
        ),
        conditionalPanel(
          condition = "input.cond_inp.includes('Three')",
          textInput("three", "Part three:")
        ),
        easyClose = FALSE,
        footer = tagList(
          actionButton(inputId = "go_next", label = "Confirm"),
          modalButton("Cancel")
        )
      )
    )
  })
}

shinyApp(ui, server)

相关问题