在Shiny中动态添加/删除整个UI元素

时间:2017-10-25 23:23:06

标签: r shiny

我一直在尝试生成一个Shiny页面,用户可以根据this snippet添加或删除元素。我尝试将其用于UI元素而不是按钮,但我明显做错了。

我这里只包含了添加按钮部分 - 每次添加"添加"单击按钮,添加正确数量的UI元素,但它们似乎都是相同的元素,而只是将新的元素添加到现有列表中(标签中的数字相同,而每个元素应该不同) )并且初始文本值被删除(或者,可能只是"默认"从最终元素重复多次)。

如何添加新的UI元素并保留用户在已存在的元素中输入的任何文本输入?

代码:

library(shiny)

# function to render UI element

renderUiElement <- function(id, nameDef) {
  renderUI(box(
    actionButton(paste0("rmBtn", id), label = "", icon = icon("times")),

    # text initial value supplied as a variable

    textInput(paste0("nameText", id), label = paste("Item", id), value = nameDef)
  ))
}


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

  rv <- reactiveValues(uiComponents = list(), uiIds = list(), 
                       nameVals = list(), lastId = 0)

  # on "Add" button click: 

  observe({
    if(is.null(input$addBtn) || input$addBtn == 0) return()

    isolate({

      # store current text inputs and append new default text

      rv$nameVals <- lapply(rv$uiIds, function(x) input[[paste0("nameText", x)]])
      rv$nameVals <- append(rv$nameVals, "New Text")

      # create (and store) new id number and add it to the list of ids in use

      rv$lastId <- rv$lastId + 1 # get the new code to use for these UI elements
      rv$uiIds <- append(rv$uiIds, rv$lastId)

      # for each id, render the UI element using the appropriate default text

      for(i in seq_along(rv$uiIds)) {
        thisId <- rv$uiIds[[i]]
        output[[paste0("uiEl", thisId)]] <- renderUiElement(
          id = thisId, nameDef = rv$nameVals[[i]]
        )
      }

      # add the new UI element into the reactiveValues list

      rv$uiComponents <- append(
        rv$uiComponents, 
        list(
          list(
            uiOutput(paste0("uiEl", rv$lastId)),
            br(),br()
          )
        )
      )
    })
  })

  # render all the UI elements inside the container

  output$container <- renderUI({
    rv$uiComponents
  })

}

ui <- fluidPage(
  titlePanel("Dynamically Add/Remove UI Elements"),
  hr(),
  actionButton("addBtn", "Add"),
  br(),br(),
  uiOutput("container")
)

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

您好动态添加闪亮的对象具有插入功能。

您的代码看起来与此类似

library(shiny)
library(shinydashboard)

# function to render UI element

renderUiElement <- function(id, nameDef) {
  box(
    actionButton(paste0("rmBtn", id), label = "", icon = icon("times")),

    # text initial value supplied as a variable

    textInput(paste0("nameText", id), label = paste("Item", id), value = nameDef)
  )
}


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


  # on "Add" button click: 
  counter <- 0
  observeEvent(
    input$addBtn,
    {
      counter <<- counter +1
      insertUI(
        selector = '#container',
        where = "beforeEnd",
        ui = renderUiElement(
          id = paste0("ui",counter),
          nameDef = "New Text"
          ),
        session = session
        )
    },
    ignoreNULL = TRUE,
    ignoreInit = TRUE
  )



  output$container <- renderUI({
    div()
  })

}

ui <- fluidPage(
  titlePanel("Dynamically Add/Remove UI Elements"),
  hr(),
  actionButton("addBtn", "Add"),
  br(),br(),
  uiOutput("container")
)

shinyApp(ui, server)

希望它有所帮助。