我需要能够在一个闪亮的应用程序中并排添加textInput()。应该有一个textInput(),它带有新文本框和命令按钮的标签,其中每次单击命令按钮时,都应将一个新的文本框添加到列表中,该列表中的标签应从第一个txtInput处获取。
例如:
1stTextBox:[ Application ]
{commandButton}
当我单击commandButton时,我应该在commandButton下面有一个textInput,
Application:[ ]
如果我将其他东西放到1stTextBox上并单击命令按钮,则应将其添加到textInput列表中。
任何想法如何动态地做到这一点?
这是错误:
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
可能超出必要范围的东西,以避免不必要的行为。
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句