Home > Back-end >  What is `Train_MEml()[[method]](...)` doing?
What is `Train_MEml()[[method]](...)` doing?

Time:07-15

I am using the following function from github (https://github.com/nguforche/MEml/blob/master/R/MEml.R#L110).

MEml2 <- function(method, data, id,  resp.vars, rhs.vars, rand.vars=NULL, para,  ...){


  mod.res <- tryCatch(
{
    trn <- data[, unique(c(resp.vars, rhs.vars, rand.vars, id)), drop = FALSE]
    Train_MEml()[[method]](trn=trn, para=para, resp.vars=resp.vars, rand.vars=rand.vars, rhs.vars=rhs.vars, groups = id) 

}, error=function(e){ 
  cat("Error in the Expression: ",  paste(e$call, collapse= ", "), 
      ": original error message = ", e$message, "\n") 
  list()
}) ## tryCatch
  collect.garbage()
  return(mod.res)
}

I do not know what the line Train_MEml()[[method]](trn=trn, para=para, resp.vars=resp.vars, rand.vars=rand.vars, rhs.vars=rhs.vars, groups = id) is doing. It seems that it is calling a function. However, there is no function called Train_MEml in corresponding MEml package.

In the package, there are other functions coming with UseMethod(method) where corresponding method substituted here.

Q1: What is Train_MEml()[[method]](trn=trn, para=para, resp.vars=resp.vars, rand.vars=rand.vars, rhs.vars=rhs.vars, groups = id) doing here?

Q2: What is Train_MEml()[[method]] doing?

CodePudding user response:

From the syntax, it appears that Train_MEml() returns a named list of functions, and [[method]] (where method should be a character string) is used to select a particular one. Then you can call this function by putting (loads_of_arguments) after it.

Indeed, Train_MEml is defined here: https://github.com/nguforche/MEml/blob/master/R/MEml_train.R


I would also like to comment about your attempt to understand what's going on.

It seems it is calling a function. However, there is no function called Train_MEml in corresponding MEml package.

This function is an internal function, not intended to be seen or called directly by users. As you see from the NAMESPACE file: https://github.com/nguforche/MEml/blob/master/NAMESPACE, this function is not exported. If you want to access this function in your R session, use MEml:::TrainMEml.

In the package, there are other functions coming with UseMethod(method) where corresponding method substituted here.

No. It is completely a different concept. UseMethod is used for S3 method dispatching.

  • Related