[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 13:03:53 CEST 2021


Hi Severin,

I understand that the helper_function is not defined in the main function.
The anonymous function inside the lapply call however is, which means it can use the (list of) variables in the main function.
You still need to pass the list of arguments to the helper_function, but not also to the anonymous function.

Best,
Freek

On 19/04/2021, 12:24, "Severin Bang" <severin.bang using physik.uni-freiburg.de> wrote:

    Hi Freek,

    the helper_function() is not defined in the main function, but only 
    called there. I decided to pass a named list of parameters, from which 
    each helper function grabs those it needs. I also thought about defining 
    the function in the main function, but decided that this neither helps 
    the readability nor is particularly useful (I could just do what the 
    helper function does in the main function directly...).

    It would rather have the ability to define for the lapply (or the helper 
    function itself) that the locale environment of the main function should 
    be used, but this is at least a workable solution.

    But many thanks for your suggestion nonetheless :)

    Best
    Severin

    On 19.04.21 11:14, Manders-2, F.M. wrote:
    > 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