[Bioc-devel] helper function arguments: best practice

Manders-2, F.M. F@M@M@nder@ @end|ng |rom pr|n@e@m@x|m@centrum@n|
Mon Apr 19 11:14:40 CEST 2021


Hi Severin,

I'm not sure what the best practice is, but since your anonymous function is defined inside your main function, you don't need to supply it with all those arguments.
This should also work, because R has lexical scoping:

main_function <- function(alot, of, paramters) {
  var01 <- foo
  ...
  varXX <- bar
  data_list <- list(data01, data02, data03, ... , dataYY)

  lapply(
    seq_along(data_list),
    function(i) {
      stuff
      helper_function(var01, ... , varXX)
    }
}

This is already a bit shorter.

I hope this helps,
Freek

On 16/04/2021, 13:45, "Bioc-devel on behalf of Severin Bang" <bioc-devel-bounces using r-project.org on behalf of severin.bang using physik.uni-freiburg.de> wrote:

    Dear community,

    I am writing my first package with the aim to publish it on 
    Bioconductor. To structure the code and make it  (human) readable I have 
    defined some helper functions, that will not be exported.

    The main function (which will be user available) makes some data 
    structuring, defines some variables and will then call a helper function 
    in a lapply-call on a list of data.frames.

    Now I find myself passing >>10 arguments (basically everything that was 
    defined in the main function before the lapply call) to the helper 
    function when executed by lapply. All arguments (except the index of the 
    current data.frame from the list which is lapplyd over) are static for 
    all the runs.

    My question is now: What is considered best practice in this case? 
    should I put all parameters in a list? Is there a way to pass the whole 
    "parent" environment (the locale of the main function) to lapply, or 
    should I keep all the single parameters?

    Maybe a "pseudo code" example is useful; this is how my problem is 
    structured:

    main_function <- function(alot, of, paramters) {
       var01 <- foo
       ...
       varXX <- bar
       data_list <- list(data01, data02, data03, ... , dataYY)

       lapply(
         seq_along(data_list),
         function(
           i,
           var01,
           ...
           varXX
         ) {
           stuff
           helper_function(var01, ... , varXX)
         },
         var01 = var01,
         ...
         varXX = varXX
       )
    }


    Thank you very much!

    Best,
    Severin

    _______________________________________________
    Bioc-devel using r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/bioc-devel



More information about the Bioc-devel mailing list