i有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]))
我想在我的闪亮应用中添加一个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
,然后在其他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))
希望这会有所帮助!
感谢@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)
和相应的列表是数据集中的变量,并在脚本开头定义。
对于想要使用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)