combining do.call() and debug() prints all arguments contents

Viewed 88

I am using R and trying to debug a function that I call with do.call() for convenience.

Combining do.call() and browser() is problematic. Basically, all the elements of the list of arguments passed to do.call() are printed, which, if the list contains for example a very large data table, is not sustainable.

Here is a reprex. I create a simple getsum() function that sums elements of a vector. I create a func() function that calls getsum() for a list of vectors.

#getsum returns the sum of a vector's elements 
#func returns the vector of the sum of a list of vectors


func <- function(vec_list){
    browser()
    sums = lapply(FUN=getsum, X=vec_list)
    sum = unlist(sums)
    return(sum)
}

getsum <- function(vec){
    sum = sum(vec)
    return(sum)
}


args = list(vec_list=list(rnorm(5), rnorm(5)))

do.call(func, args)

That's the output I get :

Called from: (function(vec_list){
    browser()
    sums = lapply(FUN=getsum, X=vec_list)
    sum = unlist(sums)
    return(sum)
})(vec_list = list(c(-0.0801864335418185, 0.448324209935905, 
-2.86518616779484, -0.359284963520417, -0.620062639582574), c(1.74835180362954, 
-0.904288222154223, 0.746007117029027, 0.625889703799832, -0.908748727897187
)))
Browse[1]> 

One might tell me "why are you using do.call()?". Indeed, if I simply call the function myself, the problem does not arise (see below). In this example I don't need to use do.call(), but sometimes it's extremely convenient.

#instead of do.call() use : 
func(vec_list=args$vec_list)

The output is then :

Called from: func(vec_list = args$vec_list)
Browse[1]> 

EDIT :

I have tried the argument browser(skipCalls=TRUE) that solves the problem but defies the purpose of browser(). It makes R executing all the function's command at once. Other suggestions welcome.

0 Answers
Related