R Shiny store the user input from multiple dynamically generated textAreaInput fields in an object in the server part

妖精的绣舞 提交于 2019-12-01 01:23:53

First, you want to make sure to assign each of your dynimcally added elements to have a unique name. You have just hard coded the letter "i" in the sample. You want something like

textAreaInput(inputId = paste0("varconst_",i), label = paste("Variables constituting scale", i), 
    width = "700px", height = "100px", value = NULL)

Then you can observe those text boxes with something like this

observeEvent(lapply(paste0("varconst_", input$selectedScoresCheckBoxes), function(x) input[[x]]), {
  obj <- Map(function(x) input[[paste0("varconst_",x)]], input$selectedScoresCheckBoxes)
  dput(obj)
})

Here I just used dput to dump the list to the console so you can see it as it gets updated but you can do whatever you want with that.

I have modified the code of the application as per MrFlick's answer. To leave a paper trail of the complete solution, I am posting it below. The few additional modifications I have made include the printout of the list with the variables for each of the generated textAreaInput fields, so that the list can be viewed in the application itself. I have also added some further modifications of the obj, after it is generated, to obtain the list as desired.

If there are more dynamically generated output sections where check boxes and related text areas, the varconst_ index has to be made unique across the different chunks of code (e.g. varconst1_, varconst2_, varconst3_, etc.).

Here is the code:

library(shiny)

ui <- fluidPage(

  mainPanel(
    fileInput(inputId = "file", label = "Choose File", multiple = TRUE, accept = ".csv"),
    uiOutput(outputId = "varCheckBoxesIndivScores"),

    fluidRow(
      column(width = 3,
             uiOutput(outputId = "selectedScoresCheckBoxes")),

      conditionalPanel(condition = "input.selectedScoresCheckBoxes",
                       column(width = 6,
                              uiOutput(outputId = "variablesConstitutingScale")))),
    br(),

    fluidRow(
      conditionalPanel(condition = "input.selectedScoresCheckBoxes",
                       verbatimTextOutput(outputId = "scalesVarList")))
  )
)

server = function(input, output, session) {

  df <- reactive({
    if(is.null(input$file)) {
      return(NULL)
    } else {
      tbl <- fread(input$file$datapath, stringsAsFactors = TRUE)
      return(tbl)
    }
  })

  output$varCheckBoxesIndivScores <- renderUI({
    if(is.null(df())) {
      return(NULL)
    } else if(!is.null(df())) {
      return(tags$div(align = "left",
                      class = "multicol",
                      checkboxGroupInput(inputId = "varCheckBoxesIndivScores",
                                         label = "Select variables",
                                         choices = colnames(df()))))
    }
  })

  output$selectedScoresCheckBoxes <- renderUI({
    if(is.null(df())) {
      return(NULL)
    } else if(!is.null(df())) {
      return(tags$div(align = "left",
                      checkboxGroupInput(inputId = "selectedScoresCheckBoxes",
                                         label = "",
                                         choices = input$varCheckBoxesIndivScores)))
    }
  })

  output$variablesConstitutingScale <- renderUI({
    if(is.null(df())) {
      return(NULL)
    } else if(!is.null(df()) & length(input$selectedScoresCheckBoxes > 0)) {
      var.list.input.fields <- lapply(input$selectedScoresCheckBoxes, function(i) {
        textAreaInput(inputId = paste0("varconst_",i), label = paste("Variables constituting scale", i), 
                      width = "700px", height = "100px", value = NULL)
      })
      var.list.input.fields
    }
  })

  observeEvent(lapply(paste0("varconst_", input$selectedScoresCheckBoxes), function(x) input[[x]]), {
    obj <- Map(function(x) input[[paste0("varconst_",x)]], input$selectedScoresCheckBoxes)
    obj <- sapply(obj, function(i) {
      if(length(i) > 0) {
        strsplit(x = i, split = " ")
      }
    })
    dput(obj)
    output$scalesVarList <- renderPrint({
      if(is.null(df())) {
        return(NULL)
      } else if(!is.null(df()) && length(input$selectedScoresCheckBoxes) > 0 && length(obj) > 0) {
        print(obj)
      }
    })
  })

}

shinyApp(ui = ui, server = server)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!