关于r:如何在闪亮的应用程序中动态添加inputText?

how do you dynamically add inputText in shiny application?

我需要能够在一个闪亮的应用程序中并排添加textInput()。应该有一个textInput(),它带有新文本框和命令按钮的标签,其中每次单击命令按钮时,都应将一个新文本框添加到列表中,该列表应从第一个txtInput中获取标签。

例如:

1
2
1stTextBox:[   Application   ]
{commandButton}

当我单击commandButton时,我应该在commandButton下面有一个textInput,

1
Application:[      ]

如果我将其他内容放入1stTextBox并单击命令按钮,则应将其添加到textInput列表中。

有什么想法可以动态地做到这一点吗?

这是错误:

1
2
3
4
5
6
7
8
9
Listening on http://127.0.0.1:3091
Warning: Error in handlers$add: Key / already in use
Stack trace (innermost first):
    43: handlers$add
    42: handlerManager$addHandler
    41: startApp
    40: runApp
     1: shiny::runApp
Error in handlers$add(handler, key, tail) : Key / already in use


我给出一个示例代码。要尝试此操作,请复制脚本并运行整个脚本。

我正在使用reactiveValues对象将信息保留在后端。
在这里,info_keeper$input_info是一个列表,其中每个元素都应为[id,label,value]的3个长度的字符向量。

单击按钮时,它(1)存储已经定义的textInputs的内容; (2)添加了新元素。

我可能正在使用isolate来避免不必要的行为。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
library(shiny)

ui <- list(
  textInput("name","Type new text input name", value =""),
  actionButton("btn","click me to create text input"),
  uiOutput("newInputs")
)

server <- function(input, output)
{
  info_keeper <- reactiveValues(
    input_info = list()
  )

  observeEvent(input$btn, {
    # copy the current contents to info_keeper
    isolate(
    {
      for (i in seq_along(info_keeper$input_info))
      {
        id <- info_keeper$input_info[[i]][1]
        info_keeper$input_info[[i]][3] <- input[[id]]
      }
    })

    # add new text input to the info_keeper
    isolate(
    {
      newid <- paste(
       "text", isolate(length(info_keeper$input_info)) + 1, sep ="")
      info_keeper$input_info <- c(
        info_keeper$input_info, list(c(newid, input$name,"")))
    })

    # invoke the update of the text inputs
    info_keeper
  })

  output$newInputs <- renderUI({
    lapply(info_keeper$input_info, function(a)
      textInput(a[1], a[2], value = a[3]))
  })
}

runApp(list(ui = ui, server = server))