如何限制orderInput小部件(来自包shinyjqui)中的元素数量?

例如,在下面的代码段中,我想在第一个小部件中选择最多2个月的时间。

用户界面

library(shiny)
library(shinyjqui)

shinyUI(fluidPage(
    uiOutput("ui_source"), br(),
    uiOutput("ui_target1"), br(),
    uiOutput("ui_target2"), br()
))


服务器

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


})

最佳答案

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

关于jquery - Shinyjqui::orderInput中的最大项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47902288/

10-11 12:33