Search code examples
rcurlr6

How to interpret error "elements..... must be named" when sourcing an R6 class?


Code snippets taken from chernan's sample REST queries are utilized to define an R6 class one private method, two public attributes and a constructor:

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

Now I source the containing file and get a strange error.

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

This looks like it should mean that I tried to do something like "self$[something I didn't declare in public or private] <- new value" but there appears to be no such mistake. What is going on here?


Solution

  • you have to use the = operator within the list instead of <-.
    So in your case use rcurl_request = function(service_url, parameters) { to solve the problem.

    The error message is not very informative, I was having this problem already several times.