0

根据这篇文章,我改变了我的脚本:

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)

我调查了这个问题,但比以前更困惑。add按钮工作正常,但按下按钮时行不会消失remove。我究竟做错了什么?

谢谢!

4

1 回答 1

3

除了 id 的分配,你非常接近。你忘记设置id了吗?我看到的唯一任务是:id <- ''。此外,您应该使用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)
于 2017-05-28T17:08:05.047 回答