如何限制orderInput
窗口小部件中的元素数量(来自包shinyjqui
)?
例如,在下面的代码段中,我想在第一个小部件中选择最多2个月。
ui.R
library(shiny)
library(shinyjqui)
shinyUI(fluidPage(
uiOutput("ui_source"), br(),
uiOutput("ui_target1"), br(),
uiOutput("ui_target2"), br()
))
server.R
shinyServer(function(input, output) {
output$ui_source <- renderUI({
orderInput("source", label = "Months", items = month.abb,
as_source = FALSE, connect = c("target1", "target2"))
})
output$ui_target1 <- renderUI({
orderInput("target1", label = "Select 2 months maximum", items = NULL, placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target2"))
})
output$ui_target2 <- renderUI({
orderInput("target2", label = "Select 3 months maximum", items = NULL, placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target1"))
})
})
答案 0 :(得分:0)
只要orderInput
中有超过2个或3个项目,您就可以触发重新渲染:
library(shiny)
library(shinyjqui)
ui <- fluidPage(
uiOutput("ui_source"), br(),
uiOutput("ui_target1"), br(),
uiOutput("ui_target2"), br()
)
server <- function(input, output) {
output$ui_source <- renderUI({
orderInput("source", label = "Months", items = month.abb,
as_source = FALSE, connect = c("target1", "target2"))
})
output$ui_target1 <- renderUI({
if(is.null(input[['target1_order']])) {
orderInput("target1", label = "Select 2 months maximum", items = NULL, placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target2"))
} else if (length(input[['target1_order']]) > 2) {
orderInput("target1", label = "Select 2 months maximum", items = input[['target1_order']][c(1,2)], placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target2"))
} else {
orderInput("target1", label = "Select 2 months maximum", items = input[['target1_order']], placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target2"))
}
})
output$ui_target2 <- renderUI({
if(is.null(input[['target2_order']])) {
orderInput("target2", label = "Select 3 months maximum", items = NULL, placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target1"))
} else if (length(input[['target2_order']]) > 3) {
orderInput("target2", label = "Select 3 months maximum", items = input[['target2_order']][c(1,2,3)], placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target1"))
} else {
orderInput("target2", label = "Select 3 months maximum", items = input[['target2_order']], placeholder = "Drag months here"
, as_source = FALSE, connect = c("source", "target1"))
}
})
}
shinyApp(ui, server)