【问题标题】:Shiny: dynamically add/ remove textInput rows based on indexShiny:根据索引动态添加/删除 textInput 行
【发布时间】:2017-05-28 15:05:30
【问题描述】:

基于this article我改变了我的脚本:

library(shiny)

ui = fluidPage( 
  actionButton("add", "Add new Row", icon=icon("plus", class=NULL, lib="font-awesome")),
  actionButton("remove", "Remove last Row", icon=icon("times", class = NULL, lib = "font-awesome")),
  tags$div(id = 'placeholder')
)

server = function(input, output) {

  ## keep track of elements inserted and not yet removed
  inserted <- c()

  observeEvent(input$add, {
    id <- ''
    insertUI(
      selector = "#placeholder",
      where = "beforeBegin",
      ui =tags$div(
        id = id,
        fluidRow(
          column(2,
                 textInput("alias", label = h5("first"))
          ),
          column(2,
                 textInput("pop", label = h5("second"))
          ),
          column(2,
                 textInput("parent", label = h5("third"))
          ),
          column(2,
                 textInput("dims", label = h5("fifth"))
          ),
          column(2,
                 textInput("method", label = h5("fourth"))
          ),
          column(2,
                 textInput("args", label = h5("sixth"))
          )
        ) 
      )
    )

    inserted <<- c(inserted, id)

  })

  observeEvent(input$remove, {
    removeUI(
      ## pass in appropriate div id
      selector = paste0('#', inserted[length(inserted)])
    )

    inserted <<- inserted[-length(inserted)]
  })

}

shinyApp(ui = ui, server = server)

我查看了this question,但比以前更加困惑。add 按钮可以正常工作,但点击remove 按钮时行不会消失。我做错了什么?

谢谢!

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    除了 id 的分配,你已经很接近了。 你忘记设置id了吗?我看到的唯一任务是:id &lt;- ''。 此外,您应该使用 reactiveValues() 而不是闪亮的全局变量。

    请参阅下面的改编代码:

    library(shiny)
    
    ui = fluidPage( 
      actionButton("add", "Add new Row", icon=icon("plus", class=NULL, lib="font-awesome")),
      actionButton("remove", "Remove last Row", icon=icon("times", class = NULL, lib = "font-awesome")),
      tags$div(id = 'placeholder')
    )
    
    server = function(input, output) {
    
      ## keep track of elements inserted and not yet removed
      inserted <- reactiveValues(val = 0)
    
      observeEvent(input$add, {
        id <- length(inserted$val) + 1
        insertUI(
          selector = "#placeholder",
          where = "beforeBegin",
          ui =tags$div(
            id = id,
            fluidRow(
              column(2,
                     textInput("alias", label = h5("first"))
              ),
              column(2,
                     textInput("pop", label = h5("second"))
              ),
              column(2,
                     textInput("parent", label = h5("third"))
              ),
              column(2,
                     textInput("dims", label = h5("fifth"))
              ),
              column(2,
                     textInput("method", label = h5("fourth"))
              ),
              column(2,
                     textInput("args", label = h5("sixth"))
              )
            ) 
          )
        )
        inserted$val <- c(inserted$val, id)
        print(inserted$val)
      })
    
      observeEvent(input$remove,{
        print(inserted$val)
        removeUI(
          ## pass in appropriate div id
          selector = paste0('#', inserted$val[length(inserted$val)])
        )
    
        inserted$val <- inserted$val[-length(inserted$val)]
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 如果我现在想将用户输入存储在 data.table 中(以便以后在进一步的操作中使用它),我会从什么开始?
    • 有一个新问题,... ;) 不认真,我也会将 data.table 存储在 reactiveValue 并在那里更新它。如果您在那里需要帮助,我认为我们应该在一个新问题中进行,您可以在此处通知我,...
    猜你喜欢
    • 2020-03-04
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多