简体   繁体   English

如何在R Shiny中将insertUI()输出添加到renderText()输出?

[英]How do I add insertUI() output to renderText() ouput in R Shiny?

I am developing a shiny app to provide automation for system development, and I need a way to add text boxes for user input, and add the resulting user input to renderText() output. 我正在开发一个闪亮的应用程序,以为系统开发提供自动化,并且我需要一种方法来添加用于用户输入的文本框,并将结果的用户输入添加到renderText()输出中。 I am not sure if there is a way to loop through reactive values, so for now I am forced to make specific calls to labels in the textInput() results. 我不确定是否有办法遍历反应性值,所以现在我被迫对textInput()结果中的标签进行特定的调用。 Code is below, note that the part that needs to be changed is the output$value part at bottom of server section: 代码在下面,请注意,需要更改的部分是服务器部分底部的output $ value部分:

library(shiny)
ui <- fluidPage(
            fluidRow(
                column(6,
                    textInput("mainDesc1", label = "Main Description", value = "", width = '100%')
                    ),
                column(1,
                    actionButton(inputId = 'insertBtn', label = "More")
                    ),
                column(1,
                    actionButton(inputId = 'removeBtn', label = "Less")
                    )
                ),
            tags$div(id = 'placeholder'),
            fluidRow(column(12, verbatimTextOutput("value")))
)

server <- function(input, output) {
    ## keep track of elements inserted and not yet removed
    inserted <- c()
    #btn <- 1
    observeEvent(input$insertBtn, {
        btn <- input$insertBtn + 1
        id <- paste0("mainDesc", btn)
        insertUI(
            selector = '#placeholder',
        ## wrap element in a div with id for ease of removal
            ui = tags$div(
                tags$p(fluidRow(
                    column(6,
                        textInput(paste("mainDesc", btn + 1, sep = ""), label = "", value = "", width = '100%')
                        )
                    )
                ),
                id = id
                )
            )
        inserted <<- c(id, inserted)
    })

    observeEvent(input$removeBtn, {
        removeUI(
        ## pass in appropriate div id
          selector = paste0('#', inserted[length(inserted)])
        )
        inserted <<- inserted[-length(inserted)]
    })

    output$value <- renderText({ noquote(paste(input[[paste("mainDesc", 1, sep = "")]], "\n",
                                               input[[paste("mainDesc", btn + 1, sep = "")]], sep = ""))
                                               })
    }
shinyApp(ui = ui, server = server)

How can I loop over the results of the userInput() boxes that pop up as a result of 我如何遍历由于以下原因而弹出的userInput()框的结果

textInput(paste("mainDesc", btn + 1, sep = ""), label = "", value = "", width = '100%')

to paste line-by-line all existing outputs of the above? 逐行粘贴以上所有现有输出? Essentially, what I am looking for is if the user inputs "1st line" in the first "Main Description" text box, clicks the "More" button to add "2nd line" in the text box that pops up, clicks more again and types "3rd line" in the popup text box the renderText() box outputs: 本质上,我要寻找的是用户在第一个“主要描述”文本框中输入“第一行”,然后单击“更多”按钮以在弹出的文本框中添加“第二行”,然后再次单击并在renderText()框输出的弹出文本框中输入“ 3rd line”:

1st line
2nd line
3rd line

If more text boxes are added, the user input should be added line-by-line to the renderText() output. 如果添加了更多文本框,则应将用户输入逐行添加到renderText()输出中。 Thanks! 谢谢!

Try making btn a reactiveValue : 尝试使btnreactiveValue

vals <- reactiveValues(btn = 1)
# reference elsewhere in your app as vals$btn

See what seems to be working code here: 在这里查看似乎有效的代码:

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(6,
           textInput("mainDesc1", label = "Main Description", value = "", width = '100%')
    ),
    column(1,
           actionButton(inputId = 'insertBtn', label = "More")
    ),
    column(1,
           actionButton(inputId = 'removeBtn', label = "Less")
    )
  ),
  tags$div(id = 'placeholder'),
  fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
)

server <- function(input, output) {
  ## keep track of elements inserted and not yet removed
  vals <- reactiveValues(btn = 0)

  observeEvent(input$insertBtn, {
    vals$btn <- vals$btn + 1
    insertUI(
      selector = '#placeholder',
      ## wrap element in a div with id for ease of removal
      ui = tags$div(
        id = paste0('line', vals$btn),
        tags$p(fluidRow(
          column(6,
                 textInput(paste("mainDesc", vals$btn + 1, sep = ""), label = paste("Line", vals$btn), value = "", width = '100%')
                 )
          )
        )
      )
    )
  })

  observeEvent(input$removeBtn, {
    removeUI(
      ## pass in appropriate div id
      selector = paste0('#line', vals$btn)
    )
    vals$btn <- vals$btn - 1
  })

  output$value <- renderText({ 
    msg <- c(input[["mainDesc1"]])
    if (vals$btn > 0) {
      for (i in 1:vals$btn) {
        msg <- c(msg, input[[paste0("mainDesc", i + 1)]])
      }
      msg <- paste(msg, collapse = "\n")  
    }
  })
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM