首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shinyjqui::orderInput中的最大项

shinyjqui::orderInput中的最大项
EN

Stack Overflow用户
提问于 2017-12-20 09:06:39
回答 1查看 326关注 0票数 2

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

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

ui.R

代码语言:javascript
复制
library(shiny)
library(shinyjqui)

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

server.R

代码语言:javascript
复制
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"))
    })


})
EN

回答 1

Stack Overflow用户

发布于 2018-05-14 13:57:28

只要orderInput中有超过2或3个项,就可以触发重呈现,如下所示:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47902288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档