R Shiny:将循环反应输入写入文本文件

R Shiny : Write looped reactive inputs to textfile

我目前正在开展一个项目,在该项目中,用户向应用程序输入一组用于上传数据集的编辑规则。用户通过 UI 中的文本框输入规则。我计划创建一个包含用户设置的编辑规则的文本文件。

现在,我的问题是创建一个代码来创建包含这些编辑规则的列表或向量,因为我想不出一种方法将用户的输入插入到代码中。这是我所做的:

textprepGen <- function(x,y,z){
  for(i in 1:x){
    z[[i]] <- paste(y[1], paste("input", 
                                paste("input",i, sep = "_"), sep = "$"), sep = " ") 
  }
  return(z)
}

它不起作用的原因是因为粘贴功能自动将代码设置为字符,因此它不会读取输入,input$input_1, input$input_2, ...

有什么建议吗?

这是 UI 和服务器:

shinyUI(fluidPage(theme="bootstrap.css",

                  titlePanel("Edit Rules"),

                  sidebarPanel(

                    fileInput('file_upload', 'Upload CSV File',
                              accept=c('text/csv', 
                                       'text/comma-separated-values,text/plain', 
                                       '.csv')),  

                    uiOutput("editrulePanel")

                  ),

                  mainPanel(
                    h4("Data Summary")

                  )
)
)

shinyServer(function(input, output, session) {

  # Text input list assignment in order for the data to show up in UI
  textinpList <-vector("list", 20)

  # Textfile preparation for the lines to be written on the textfile    
  textprepList <- vector(20)

  # Function to generate text inputs
  ## A for loop is used to generate only the columns that are in the data file
  textinpGeneration <- function(x,y,z){
    for(i in 1:x){
      z[[i]] <- list(textInput(paste("text", i, sep = "_"), 
                               label = h5(y[i]), 
                               value = ">= 0"
      )
      )
    }
    return(z)
  }

  textprepGen <- function(x,y,z){
    for(i in 1:x){
      z[[i]] <- paste(y[1], paste("input", 
                                  paste("input",i, sep = "_"), sep = "$"), sep = " ") 
    }
    return(z)
  }


  # Dynamic UI
  output$editrulePanel <- renderUI ({
    # Assigns the input of the uploaded data to a variable

    inFile <- input$file_upload

    # If no file is uploaded, no table will be displayed in the main panel

    if (is.null(inFile))
      return(NULL)

    # Read csv file uploaded

    dataFile <- read.csv(inFile$datapath)

    # Count the number of columns of the data file and assign to a variable
    ## This is used to know the number of options 
    ## to show in the editrule panel

    dataCol <- as.numeric(ncol(dataFile))

    # Read the column names and assign to a variable

    datacolName <- colnames(dataFile)

    textprepGen(dataCol, datacolName, textprepList)

    # Conditional panel for editrules show if a file has been uploaded

    conditionalPanel(condition = "is.null(inFile) == FALSE",
                     h4("Please input edit rule"),
                     textinpGeneration(dataCol,datacolName,textinpList)
    )

  })

  # Preparation for writing a textfile
  editruleFile <- file("editrules.txt")
  writeLines(c(textprepList), editruleFile)
  close(editruleFile)


})

我想要实现的结果文本文件:

# numerical rules
RECOV <= 0
PAID >= 0
CASE >= 0
INC >= 0

只有逻辑符号和数字是用户输入的。

无论如何,非常感谢 Eugene Choe。刚刚让我的代码工作。还想出了将其正确写入文本文件的方法。

这是输入的最终代码:

for(i in 1:addruleNumber){
                                              textprepList[i+1+dataCol] <- list(paste(
                                                eval(parse(text =  paste("input", 
                                                                         paste("lhand",i,sep="_"), 
                                                                         sep = "$"))),
                                                eval(parse(text =  paste("input", 
                                                                         paste("logexp",i,sep="_"), 
                                                                         sep = "$"))),
                                                eval(parse(text =  paste("input", 
                                                                         paste("rhand",i,sep="_"), 
                                                                         sep = "$"))),
                                                sep = " "))
                                            }

这是我修复文本文件输出的方法:

textprepList[1] <- "# numerical rules"  
                                          for(i in 1:dataCol){
                                            textprepList[i+1] <- list(paste(datacolName[i],
                                                                            eval(parse(text =  paste("input", 
                                                                                                     paste("text",i,sep="_"), 
                                                                                                     sep = "$"))), sep = " "))
                                          }