通过闪亮的按钮动态添加/删除输入字段并保留值

G.D*_*.D. 5 shiny shiny-server

我的问题是以下讨论的后续问题:

如何通过闪亮的按钮动态添加/删除输入字段

我希望能够在闪亮的应用程序上使用操作按钮动态添加/删除输入,而且当我添加新输入时,我希望输入字段的值保持不变,而不是像现在这样更改。你能帮我解决这个问题吗?

例如,如果我更改第一个框并通过按钮添加另一个文本输入,则第一个框的值已重置为默认值。

library(shiny)

ui <- shinyUI(fluidPage(

  sidebarPanel(

      actionButton("add_btn", "Add Textbox"),
      actionButton("rm_btn", "Remove Textbox"),
      textOutput("counter")

    ),

  mainPanel(uiOutput("textbox_ui"))

))

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$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      lapply(seq_len(n), function(i) {
        textInput(inputId = paste0("textin", i),
                  label = paste0("Textbox", i), value = "Hello World!")
      })
    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

SBi*_*sta 11

可能有更好的解决方案,但这也行得通。

library(shiny)

ui <- shinyUI(fluidPage(

  sidebarPanel(
    actionButton("add_btn", "Add Textbox"),
    actionButton("rm_btn", "Remove Textbox"),
    textOutput("counter")

  ),

  mainPanel(uiOutput("textbox_ui"))

))

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

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

  #Track the number of input boxes previously
  prevcount <-reactiveValues(n = 0)

  observeEvent(input$add_btn, {
        counter$n <- counter$n + 1
        prevcount$n <- counter$n - 1})

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

  })

  output$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      # If the no. of textboxes previously where more than zero, then 
      #save the text inputs in those text boxes 
      if(prevcount$n > 0){

         vals = c()
        if(prevcount$n > n){
          lesscnt <- n
          isInc <- FALSE
        }else{
          lesscnt <- prevcount$n
          isInc <- TRUE
        }
        for(i in 1:lesscnt){
          inpid = paste0("textin",i)
         vals[i] = input[[inpid]] 
        }
        if(isInc){
          vals <- c(vals, "New text box")
        }

        lapply(seq_len(n), function(i) {
          textInput(inputId = paste0("textin", i),
                    label = paste0("Textbox", i), value = vals[i])
        })

      }else{
        lapply(seq_len(n), function(i) {
          textInput(inputId = paste0("textin", i),
                    label = paste0("Textbox", i), value = "New text box")
        }) 
      }

    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!