在SHILINE和Keep Values中通过按钮动态添加/删除输入字段 [英] Add/remove input fields dynamically by a button in shiny AND keep values

查看:19
本文介绍了在SHILINE和Keep Values中通过按钮动态添加/删除输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

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

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)

这篇关于在SHILINE和Keep Values中通过按钮动态添加/删除输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆