【问题标题】:Not able to Remove previously inserted UI无法删除以前插入的 UI
【发布时间】:2019-12-08 18:29:29
【问题描述】:

在下面的应用程序中,我无法删除之前插入的 UI。不知道为什么。任何人都可以帮忙.................................................................. ....................

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(rhandsontable)
```r

Column {data-width=650}
-----------------------------------------------------------------------

### Insert and Remove UI

```{r}

actionButton("add", "Add UI")
actionButton("rmv", "Remove UI")
 observeEvent(input$add, {
    insertUI(
      selector = "#add",
      where = "afterEnd",
      ui = textInput(paste0("txt", input$add),
                     "Insert some text"),multiple = FALSE
    )
  })
 observeEvent(input$rmv, {
    removeUI(
      selector = "div:has(< #txt)"
    )
  })
```

```

【问题讨论】:

  • 谢谢。它几乎可以工作了。但它只删除最后一个操作。 (对不起这是我的错)。如果我需要之前的所有操作怎么办?此外,它只删除文本框,标题保持原样。(它没有被删除)

标签: r


【解决方案1】:

我们可以使用seq_len(input$add) 获取之前的id,其中input$add 是1,2,3,...等,然后使用paste0sprintf 构造一个jQuery 语句

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(rhandsontable)
```

Column {data-width=650}
-----------------------------------------------------------------------

```{r}



  ### Insert and Remove UI

actionButton("add", "Add UI")
actionButton("rmv", "Remove UI")
observeEvent(input$add, {
    insertUI(
      selector = "#add",
      where = "afterEnd",
      ui = textInput(paste0("txt", input$add),
                     sprintf("Insert some text for %i",input$add)),multiple = FALSE
    )
  })
observeEvent(input$rmv, {
    removeUI(
      #Remove the last UI
      #selector = sprintf("div:has(> #txt%i)",input$add),
      #Remove all previous UIs
      selector = sprintf("div:has(%s)",paste0('> #txt', seq_len(input$add), collapse = ",")),
      immediate = TRUE,
      multiple = TRUE
    )
  })
```

更新

每次用户点击时删除最后一个用户界面删除用户界面

### Insert and Remove UI

actionButton("add", "Add UI")
actionButton("rmv", "Remove UI")
data = reactiveValues(tmp=0)
observeEvent(input$add, {
    data$tmp <- c(data$tmp,input$add)

    insertUI(
      selector = "#add",
      where = "afterEnd",
      ui = textInput(paste0("txt", input$add),
                     sprintf("Insert some text for %i",input$add)),multiple = FALSE
    )
  })

observeEvent(input$rmv, {
    rm <- tail(data$tmp, 1)
    data$tmp <- setdiff(data$tmp, rm)

    removeUI(
      #Remove the last UI
      #selector = sprintf("div:has(> #txt%i)",input$add),
      #Remove all previous UIs
      selector = sprintf("div:has(%s)",paste0('> #txt', rm, collapse = ",")),
      immediate = TRUE,
      multiple = TRUE
    )
  })

【讨论】:

  • @Suliman,当我将 textInput 替换为 fileInput 时,我怀疑为什么删除 UI 不起作用。它应该可以正常工作吗?
  • 不幸的是,在 R 中尝试 fileInput("file1", "Choose CSV File", accept = c( "text/csv", "text/comma-separated-values,text/plain", ".csv") )textInput(paste0("txt", 1), sprintf("Insert some text for %i",1)) 并不相同。
  • 嗨,这是解决方案吗?我的意思是我应该替换上面的代码吗?
  • NO,textInputfileInput 它们具有不同的 HTML 结构,正如您在 R 控制台中打印它们时所看到的那样。可能您应该发布一个新问题并添加shinyjQuery 作为标签
  • 我在这里添加了问题stackoverflow.com/questions/59235852/…
猜你喜欢
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多