R Shiny根据Userinput动态添加textinput并打印ui输出

我正在尝试创建一个闪亮的应用程序,该应用程序将使用户能够添加文本框或添加图像并从中创建文档。我能够添加一个文本框并显示其内容,但是当我添加另一个文本框时,内容不会显示。我以a link为起点。

这是我的示例代码,我试图通过单击添加按钮来添加更多用户输入文本框。

library(shiny)
library(shinyjqui)

ui <- shinyUI(fluidPage(

  sidebarPanel(
    actionButton("add_btn","Add Textbox"),actionButton("rm_btn","Remove Textbox"),textOutput("counter")
  ),mainPanel(
    jqui_sortable(
      div(id = 'textboxes',uiOutput("textbox_ui"),textInput("caption","Caption","Insert Text"),verbatimTextOutput("value")
      )
    )
  )
))

server <- shinyServer(function(input,output,session) {

  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)

  #Track the number of input boxes previously
  prevcount <- reactiveValues(n = 0)

  observeEvent(input$add_btn,{
    counter$n <- counter$n + 1
    prevcount$n <- counter$n - 1})

  observeEvent(input$rm_btn,{
    if (counter$n > 0) {
      counter$n <- counter$n - 1 
      prevcount$n <- counter$n + 1
    }

  })

  output$value <- renderText({ input$caption })


  output$counter <- renderPrint(print(counter$n))

  textboxes <- reactive({

    n <- counter$n

    if (n > 0) {
      # If the no. of textboxes previously where more than zero,then 
      #save the text inputs in those text boxes 
      if(prevcount$n > 0){

        vals = c()
        if(prevcount$n > n){
          lesscnt <- n
          isInc <- FALSE
        }else{
          lesscnt <- prevcount$n
          isInc <- TRUE
        }
        for(i in 1:lesscnt){
          inpid = paste0("textin",i)
          vals[i] = input[[inpid]] 
        }
        if(isInc){
          vals <- c(vals,"Insert Text")
        }

        lapply(seq_len(n),function(i) {
          textInput(inputId = paste0("textin",i),label = paste0("Subsection ",value = vals[i])
        })

      }else{
        lapply(seq_len(n),value = "Insert text")
        }) 
      }

    }

  })

  output$textbox_ui <- renderUI({ textboxes() })

})

shinyApp(ui,server)

在这方面任何帮助将不胜感激。如果有人能指出每次添加一个新框时如何动态捕获输出值的方法,那将使我朝着正确的方向前进。

pspg44oec 回答:R Shiny根据Userinput动态添加textinput并打印ui输出

您是否尝试过reactiveValuesToList函数? 这里有一个示例that might help

AllInputs <- reactive({
x <- reactiveValuesToList(input)  })

textboxes <- reactive({

n <- counter$n

if (n > 0) {
  isolate({
    lapply(seq_len(n),function(i) {
      textInput(inputId = paste0("textin",i),label = paste0("Textbox",value = AllInputs()[[paste0("textin",i)]])
    })
  })
}
}) 
本文链接:https://www.f2er.com/3141460.html

大家都在问