采购 R6 class 时如何解释错误 "elements..... must be named"?
How to interpret error "elements..... must be named" when sourcing an R6 class?
取自 chernan's sample REST queries 的代码片段用于定义 R6 class 一个私有方法、两个 public 属性和一个构造函数:
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.
这看起来应该意味着我试图做类似 "self$[something I didn't declare in public or private] <- new value" 的事情,但似乎没有这样的错误。这是怎么回事?
您必须在 list
中使用 =
运算符,而不是 <-
。
所以在你的情况下使用 rcurl_request = function(service_url, parameters) {
来解决问题。
错误信息不是很详细,我已经遇到这个问题好几次了。
取自 chernan's sample REST queries 的代码片段用于定义 R6 class 一个私有方法、两个 public 属性和一个构造函数:
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.
这看起来应该意味着我试图做类似 "self$[something I didn't declare in public or private] <- new value" 的事情,但似乎没有这样的错误。这是怎么回事?
您必须在 list
中使用 =
运算符,而不是 <-
。
所以在你的情况下使用 rcurl_request = function(service_url, parameters) {
来解决问题。
错误信息不是很详细,我已经遇到这个问题好几次了。