R Shiny将来自多个动态生成的textAreaInput字段的用户输入存储在服务器部分的对象中

pan*_*man 2 textarea user-input r dynamic shiny

新来shiny现在有了这个挣扎超过两天。我创建了一个应用程序,用户在其中加载.csv数据文件,并选择一个或多个变量,这些变量的名称在应用程序中显示为复选框。选中复选框后,将在其下方显示一个具有相同名称的新复选框,当单击该复选框时,其textAreaInput旁边也会出现一个,用户可以在其中添加构成目标变量的变量名称作为比例。这是应用程序的过度简化版本:

library(shiny)

ui <- fluidPage(

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

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

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

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 = "i", label = paste("Variables constituting scale", i), width = "700px", height = "100px", value = NULL)
      })
      var.list.input.fields
    }
  })

}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

加载的数据是这样生成的(只是摘录,实际的有更多的列和案例):

library(data.table)

x <- data.table(ID = c(2201:2220), VAR1 = rnorm(n = 20, mean = 10, sd = 2),
VAR2 = rnorm(n = 20, mean = 100, sd = 20), VAR3 = 1:20, VAR4 = 21:40,
VAR5 = 41:60, VAR6 = 61:80, VAR7 = 81:100)

write.csv(x = x, file = "/tmp/test_data.csv", row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)

它工作正常,没有错误。在每个生成的textAreaInput字段中输入变量名称后,它的外观如下: 在此处输入图片说明

However, I would like to take the user input from each dynamically generated textAreaInput and store it in a list like:

list(VAR1 = "VAR3 VAR4 VAR5", VAR2 = "VAR6 VAR7")
Run Code Online (Sandbox Code Playgroud)

or

list(VAR1 = "VAR3", "VAR4", "VAR5", VAR2 = "VAR6", "VAR7")
Run Code Online (Sandbox Code Playgroud)

inside the server part of the application for future use.

I tried to follow the solution in this thread, but I did not succeed to come to any solution and feel quite confused. Can someone help?

MrF*_*ick 5

首先,您要确保为每个动态添加的元素分配一个唯一的名称。您刚刚对示例中的字母“ i”进行了硬编码。你想要类似的东西

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

然后,您可以观察到像这样的文本框

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

在这里,我只是dput将列表转储到控制台,以便在更新列表时可以看到它,但是您可以执行任何操作。