在R中读取csv文件时,闭包不是子集错误

时间:2018-09-16 21:41:18

标签: r csv shiny

我是R的新手。我编写了以下代码,这是我不得不修改的更大的R脚本的一部分。 修改的重点是:

  1. 读取一个.csv文件,该文件具有两列,分别名为“项目名称”和“任务名称”     -这是真理的来源

  2. 然后,原始脚本已经上传了一个csv文件,并且该csv文件中的数据存储在tabIn中。     -该文件是由用户更新的文件。用户输入项目名称和任务名称(除其他外),然后将csv文件上传到闪亮的

  3. 我的目的是从上面的#2中读取.csv。然后将所选的项目名称与任务名称进行比较。     -这样做的目的是确保#2中的.csv文件中指定的任务名称属于#1中相同的项目名称(真相来源)。             -用户可以选择属于另一个项目的任务名称。这段代码的目的是为了防止这种情况。     -如果将#2中的任务名称分配给了错误的项目名称,则此代码应发出警报。     -如果#1(事实来源)中根本没有#2中的任务名称,则代码也应发出警报。

当我在Rstudio中独自运行此代码时。它没有错误。但是,当我将它作为原始脚本的一部分闪亮地运行时,它会出现以下错误:

"object of type 'closure' is not subsettable"

下面是我编写的代码,我将其添加到原始脚本中:

  # Get the list of valid projectnames and valid tasknames from the dynamic source of truth
  taskNamesAndprojectNames <- read.table("/opt/task_name_controlled_vocabulary.tsv", header=TRUE, sep="\t")
  allowedProjects <- taskNamesAndprojectNames[,1]
  allowedTasks <- taskNamesAndprojectNames[,-1]
  cmbPntn <- setNames(as.list(allowedProjects), allowedTasks)
  ##############################################################################################
  # read in the .csv file which the user updates.  #2 above. The person who wrote this script already read in the csv file into tabIn.  
  # i dont want to play with that variable so I duplicate it to tabInB.
  tabInB <- tabIn
  ## split data.frame into name list of vectors
  split <- split(tabInB, tabInB$project_name)
  namedListManifest <- lapply(split, function(x){
   as.character(x[["task_name"]])
  })
  ##############################################################################################
  for (projectTaskName in tabIn$task_name){
     if(!grepl("^\\s*$", projectTaskName) && !grepl("string", projectTaskName )) {
        # For each taskname in the user submitted manifest excel (#2), check which project name is assigned to it
        projMatchChkUEM <- cmbPntn[[projectTaskName]]
        # If you find a taskname that is not found in the source of truth tsv file, error out and inform the user.
        if(is.null(projMatchChkUEM) || is.na(projMatchChkUEM) || grepl("^\\s*$", projMatchChkUEM)) {
                dataResult <- paste("Task [", projectTaskName, "] is not a valid Task Name. If this is a new Task Name you wish to add, please contact John or Jane to add it to the sc1 source of truth.")
                print(dataResult)
                # this will cause the script to abort in the shiny UI
                chkstatus <- 0
        } else {
                split <- split(taskNamesAndprojectNames, data$Project.Name)
                #print(split)
                ## split data.frame into name list of vectors
                namedList <- lapply(split, function(x){
                        as.character(x[["Task.Name.for.Manifest"]])
                })
                for (projName in tabIn$project_name){
                        #print(projectTaskName,projName)
                        if(!grepl("^\\s*$", projName) && !grepl("string", projName )) {
                                if(projectTaskName %in% namedListManifest[[projName]]) {
                                        if (!(projectTaskName %in% namedList[[projName]])){
                                                dataResult <- paste("Project [", projName, "] and Task [", projectTaskName, "] are incorrectly mapped! Check the [ sc1 ] source of truth file for the correct mappings!!!")
                                                print(dataResult)
                                                # this will cause the script to abort in the shiny UI
                                                chkstatus <- 0
                                        }
                                }
                        }
                }
          }
      }
  }

0 个答案:

没有答案