如何限制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"))
})
})发布于 2018-05-14 13:57:28
只要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)https://stackoverflow.com/questions/47902288
复制相似问题