簡體   English   中英

你如何在閃亮的應用程序中動態添加 inputText ?

[英]how do you dynamically add inputText in shiny application?

我需要能夠在閃亮的應用程序中並排添加 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM