【问题标题】:how do you dynamically add inputText in shiny application?如何在闪亮的应用程序中动态添加 inputText?
【发布时间】:2016-03-22 16:31:03
【问题描述】:

我需要能够在闪亮的应用程序中并排添加 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

【问题讨论】:

  • 您的服务器代码中需要observeEventrenderUI 的组合。 observeEvent 将监视按钮单击,然后使用 renderUI 绘制具有指定标签的第二个文本输入。
  • @warmoverflow,我对闪亮很陌生。你有什么例子吗?
  • 这是我为另一个问题制作的示例。它观察 numericInput 的变化并呈现更多 numericInput。 stackoverflow.com/questions/36094718/r-shiny-dynamic-input/…

标签: r shiny


【解决方案1】:

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

我正在使用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))

【讨论】:

  • 我不断收到此错误“错误:密钥/已在使用中”。我将整个代码放在 rstudio 窗口中。有什么想法吗?
  • 我不知道。只是为了检查,是否可以重新启动rstudio,将整个代码粘贴到编辑器中,然后单击rstudio的源按钮?在我的环境中,这将启动应用程序而没有错误。如果这仍然导致同样的错误,请告诉我。
  • 是的,它奏效了。快速提问。我需要能够使用文本框中的值进行一些计算。 1)如何确保文本框中的值是数字? 2)我如何知道文本框的 id 并从每个文本框中检索值。例如,每个文本框都会有所不同。第一个是网络服务器的数量。有没有办法跟踪这些动态生成的文本框的 id 及其值?
  • 1) 由于 textInput 的值是一个字符,您需要检查该字符是否代表一个数字。例如,要检查字符是否仅由数字组成,regexpr("^[0-9]+$", text)。 2) 在我的代码中,文本框的 id 存储在info_keeper$input_info 中。每个元素都是一个[ID, label, value]的字符向量,按照创建顺序对应文本框。由于 ID 是在代码中自动命名的,因此您可能希望使用标签来查找某个文本框。
  • 我接受了你的回答,它回答了我原来的问题。我还有一些问题,我并没有真正遵循您的代码。我应该问一个新问题吗?这样做的目的是有一些输入文本框,从用户那里获取数据,进行一些计算并输出每个输入文本框的结果。假设第一个输入文本框是网络服务器,计算完成后,我需要打印输出网络服务器。需要对每个输入文本框执行此操作,因此我需要知道哪些文本框是哪些以及用户在其中放置了什么。这有意义吗?
猜你喜欢
  • 2017-07-27
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2016-07-02
  • 2021-04-09
  • 2021-11-05
相关资源
最近更新 更多