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

时间:2015-07-17 06:27:47

标签: r text-files shiny

我目前正在开发一个项目,在该项目中,用户向应用程序输入一组上传数据集的编辑规则。用户通过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_1,输入$ 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

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

1 个答案:

答案 0 :(得分: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 = " "))
                                          }