【问题标题】:Is there a way to dynamically insert / Add UI in a fluidRow in a Shiny app有没有办法在 Shiny 应用程序的 FluidRow 中动态插入/添加 UI
【发布时间】:2017-03-31 02:10:25
【问题描述】:

我正在尝试创建一个闪亮的应用程序,允许我动态添加 UI 控件小部件。但是,当前代码将为每个新的 UI 小部件添加一个新行。有没有办法将它们添加到流体行中,这样每行最多需要 4 个小部件。

另外,有没有一种方法可以在不默认所有当前选择的情况下添加/删除控件小部件?我现在拥有的代码基本上会在每次点击“添加”或“删除”按钮时重新生成控件小部件,这意味着所有选择都将重置为默认值。

非常感谢您的帮助。 代码如下。

谢谢!

library(shiny)

ui <- shinyUI(fluidPage(


  mainPanel(
    actionButton("add_btn", "Add Box"),
    actionButton("rm_btn", "Remove Box"),
    uiOutput("interactionUI"))

))

server <- shinyServer(function(input, output, session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  observeEvent(input$add_btn, {counter$n <- counter$n + 1})
  observeEvent(input$rm_btn, {
    if (counter$n > 0) counter$n <- counter$n - 1
  })


  interaction <- reactive({

    n <- counter$n

    if (n > 0) {
      lapply(seq_len(n), function(i) {
        selectInput(inputId = paste0("item", i),
                    label = paste0("Item", i), choices=c("test","test2"),
                    selected = NULL,
                    multiple = 8, selectize = TRUE, width = "20%", size = NULL)
      })
    }

  })

  output$interactionUI <- renderUI({ interaction() })

})

shinyApp(ui, server)

【问题讨论】:

  • 看insertUI/removeUI的demo gallery.shinyapps.io/111-insert-ui
  • 您好 Geovany,谢谢您的来信。我的问题是有没有办法将这些插入的 UI 放入一个流式行中。这样当你添加新的 UI 项时,它会在水平方向添加,而不是在垂直方向堆叠所有新 UI。

标签: r user-interface shiny


【解决方案1】:

在流动的行内添加一个 id 标记的 div,然后将此 id 作为选择器引用以插入一列。

fluidRow(tags$div(id="rowLabel",column(4,"Example1",textOutput("example1"))))

insertUI(selector = "#rowLabel",where = "afterEnd",ui = tags$div(id="columnLabel",column(4,"Example2",textOutput("example2"))))

我将 div id 包装器添加到插入的列中,以便以后删除。

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但我也遇到了同样的问题,并认为分享我的解决方案会很好。 我的灵感来自:https://www.r-bloggers.com/dynamic-ui-elements-in-shiny/

    基本上,您应该将来自 lapply 调用的 UI 元素存储在一个变量中,然后将 do.call() 与您喜欢的 Shiny 布局一起使用(在我的例子中,flowLayout 生成的结果比fluidRow 更好)。

    library(shiny)
    
    ui <- shinyUI(fluidPage(
    
    
      mainPanel(
        actionButton("add_btn", "Add Box"),
        actionButton("rm_btn", "Remove Box"),
        uiOutput("interactionUI"))
    
    ))
    
    server <- shinyServer(function(input, output, session) {
    
      # Track the number of input boxes to render
      counter <- reactiveValues(n = 0)
    
      observeEvent(input$add_btn, {counter$n <- counter$n + 1})
      observeEvent(input$rm_btn, {
        if (counter$n > 0) counter$n <- counter$n - 1
      })
    
    
      output$interactionUI <- renderUI({
    
        n <- counter$n
    
        if (n > 0) {
          interaction <- lapply(seq_len(n), function(i) {
            selectInput(inputId = paste0("item", i),
                        label = paste0("Item", i), choices=c("test","test2"),
                        selected = NULL,
                        multiple = 8, selectize = TRUE, width = "20%", size = NULL)
          })
        }
        do.call(flowLayout, interaction)
    
      })
    })
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 2021-05-24
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2021-09-11
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多