【问题标题】:Add/remove input fields dynamically by a button in shiny AND keep values通过闪亮的按钮动态添加/删除输入字段并保留值
【发布时间】:2017-05-05 03:12:22
【问题描述】:

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

How to add/remove input fields dynamically by a button in shiny

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

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

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)

【问题讨论】:

    标签: shiny shiny-server


    【解决方案1】:

    可能有更好的解决方案,但这也可以:

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2019-05-01
      • 2019-04-30
      • 2020-06-01
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多