Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is Train_MEml()[[method]](…) line doing?

I have 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 is the line Train_MEml()[[method]](trn=trn, para=para, resp.vars=resp.vars, rand.vars=rand.vars, rhs.vars=rhs.vars, groups = id) doing. It seems it is calling a function. However, there is no function called Train_MEml() in corresponding MEml R package.

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading