R Shiny将来自多个动态生成的textAreaInput字段的用户输入存储在服务器部分的对象中 [英] R Shiny store the user input from multiple dynamically generated textAreaInput fields in an object in the server part

查看:237
本文介绍了R Shiny将来自多个动态生成的textAreaInput字段的用户输入存储在服务器部分的对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

New to shiny and struggling with this for more than two days now. I have created an application where the user loads .csv data file and chooses one or more variables whose names appear in the application as check boxes. When a checkbox is checked, a new checkbox appears under with the same name and when it is clicked too, a textAreaInput appears next to it where the user can add variable names that constitute the target variable as a scale. Here is an oversimplified version of the application:

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)

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

The data to load is generated like this (just an excerpt, the real one has more columns and cases):

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)

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

It works fine, no errors. Here is how it looks, after I enter the variable names in each of the generated textAreaInput fields:

但是,我想从每个动态生成的 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")

list(VAR1 = "VAR3", "VAR4", "VAR5", VAR2 = "VAR6", "VAR7")

我尝试遵循线程,但是我没有成功地提出任何解决方案,并感到非常困惑。有人可以帮忙吗?

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?

推荐答案

首先,您要确保分配给每个动态添加的元素以唯一的名称。您刚刚对示例中的字母 i进行了硬编码。您想要这样的

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)
})

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

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.

这篇关于R Shiny将来自多个动态生成的textAreaInput字段的用户输入存储在服务器部分的对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆