gpt4 book ai didi

javascript - r shiny - 条件为 : check item in list 的条件面板

转载 作者:行者123 更新时间:2023-11-29 10:03:40 25 4
gpt4 key购买 nike

我有 2 个列表,每个列表包含许多 ids p_id 以另一个变量 d 为条件。

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

我想在我 Shiny 的应用程序中添加一个 conditionalPanel 以相应地显示/隐藏一个 selectInput 小部件。

在我的 UI dashboardPage 中,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"
)
)
),

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

我尝试了 condition = "input.p_id.indexOf(d1)!=-1" 但它也不起作用。

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

最佳答案

我认为您可以以更简单的方式实现您想要的效果,而无需使用 conditionalPanels。 我们可以生成一次 selectInput,然后使用 更新它updateSelectInput 每当其他 input 更改时。这是一个工作示例:

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

希望这对您有所帮助!

关于javascript - r shiny - 条件为 : check item in list 的条件面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48760041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com