Maximum item in shinyjqui::orderInput

元气小坏坏 提交于 2021-01-28 00:57:10

问题


How to limit the number of element in an orderInput widget (from package shinyjqui) ?

For example, in the piece of code below I would like to select maximum 2 months in the first widget.

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


})

回答1:


You can trigger a re-render as soon as there are more than 2 or 3 items in the orderInput, like that:

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/maximum-item-in-shinyjquiorderinput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!