如何在采购R6类时解释错误“元素.....必须命名”?

时间:2017-07-28 23:13:20

标签: r curl r6

chernan's sample REST queries获取的代码片段用于定义R6类的一个私有方法,两个公共属性和一个构造函数:

library(R6)
library(RCurl)
library(RJSONIO)
Symbol <- R6Class("Symbol",
  private = list(
    #
    # define a generic function to send an HTTP GET request
    #
    rcurl_request <- function(service_url, parameters) {
      # Collapse all parameters into one string
      all_parameters <- paste(
        sapply(names(parameters), 
               FUN=function(param_name, parameters) {
                 paste(param_name, paste(parameters[[param_name]], collapse=','), collapse='', sep='=')
               }, 
               parameters),
        collapse="&")
      # Paste base URL and parameters
      requested_url <- paste0(service_url, all_parameters)
      # Encode URL (in case there would be any space character for instance)
      requested_url <- URLencode(requested_url)
      # Start request to service
      response <- getURL(requested_url, .opts = list(ssl.verifypeer = FALSE))
      return(response)
    }
  ),                
  public = list(
    species = NULL,
    bdbnJSON = NULL,
    initialize = function(symbol, bdbSpecies){
      parameters <- list(method="dbfind", 
                         inputValues=symbol,
                         output="geneid",
                         taxonId=bdbSpecies,
                         format="col"
      )
      base_url = "https://biodbnet-abcc.ncifcrf.gov/webServices/rest.php/"
      json_url = paste0(base_url, "biodbnetRestApi.json?")    
      self$bdbnJSON <- rcurl_request(json_url, parameters)
    }
  )                  
)

现在我来源包含文件并得到一个奇怪的错误。

> source("parseSymbol.R")
Error in R6Class("Symbol", private = list(rcurl_request <- function(service_url,  : 
  All elements of public, private, and active must be named.

这看起来应该意味着我试图做一些像“自我$ [我没有公开或私下宣布的事情]&lt; - 新值”但似乎没有这样的错误。这是怎么回事?

1 个答案:

答案 0 :(得分:9)

您必须使用=中的list运算符代替<-
因此,在您的情况下,使用rcurl_request = function(service_url, parameters) {来解决问题。

错误信息不是很有用,我已经多次遇到过这个问题了。