Shinyjqui::orderInput 中的最大项目 [英] Maximum item in shinyjqui::orderInput

查看:27
本文介绍了Shinyjqui::orderInput 中的最大项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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


})

推荐答案

只要 orderInput 中的项目超过 2 或 3 个,您就可以触发重新渲染,如下所示:

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)

这篇关于Shinyjqui::orderInput 中的最大项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆