Rguy
2014-Apr-26 15:42 UTC
[R] mapply echoes function call when browser() is called from within FUN
When mapply is applied to a function that has a call to browser() within it, the result can be a disastrous amount of feedback. To clarify this situation please consider the following function, containing a call to browser within it: plus = function(a, b) {browser(); a + b} A plain vanilla call to plus() yields the following: LAPTOP_32G_01> plus(1,2) Called from: plus(1, 2) Browse[1]> [1] 3 Now consider the following application of mapply to plus: LAPTOP_32G_01> mapply(plus, 1:2, 1:2) Called from: (function (a, b) { browser() a + b })(dots[[1L]][[1L]], dots[[2L]][[1L]]) Browse[1]> Called from: (function (a, b) { browser() a + b })(dots[[1L]][[2L]], dots[[2L]][[2L]]) Browse[1]> [1] 2 4 Notice that at each step, after the browser is called, mapply prints out the function call including its arguments: Called from: (function (a, b) { browser() a + b })(dots[[1L]][[1L]], dots[[2L]][[1L]]) etc. In the present case this does no harm except to make things a little harder to read. However, if one of the inputs happens to be a data frame with a million rows, the entire million rows are printed to the screen. I have been bitten by this, which is why I am writing this note. I have a question and a request: Question: Is there some way to prevent mapply (or browser) from echoing the function call when browser is called from within FUN? Request: If not, could the ability to turn off this echoing be provided. As things stand, calling browser from within FUN, when FUN is a realistically big function or has realistically big arguments, is a disaster. Thanks. [[alternative HTML version deleted]]
Duncan Murdoch
2014-Apr-26 16:06 UTC
[R] mapply echoes function call when browser() is called from within FUN
On 26/04/2014, 11:42 AM, Rguy wrote:> When mapply is applied to a function that has a call to browser() within > it, the result can be a disastrous amount of feedback. > > To clarify this situation please consider the following function, > containing a call to browser within it: > > plus = function(a, b) {browser(); a + b} > > A plain vanilla call to plus() yields the following: > > LAPTOP_32G_01> plus(1,2) > Called from: plus(1, 2) > Browse[1]> > [1] 3 > > Now consider the following application of mapply to plus: > > LAPTOP_32G_01> mapply(plus, 1:2, 1:2) > Called from: (function (a, b) > { > browser() > a + b > })(dots[[1L]][[1L]], dots[[2L]][[1L]]) > Browse[1]> > Called from: (function (a, b) > { > browser() > a + b > })(dots[[1L]][[2L]], dots[[2L]][[2L]]) > Browse[1]> > [1] 2 4 > > Notice that at each step, after the browser is called, mapply prints out > the function call including its arguments: > > Called from: (function (a, b) > { > browser() > a + b > })(dots[[1L]][[1L]], dots[[2L]][[1L]]) > > etc. > > In the present case this does no harm except to make things a little harder > to read. However, if one of the inputs happens to be a data frame with a > million rows, the entire million rows are printed to the screen. I have > been bitten by this, which is why I am writing this note. I have a question > and a request:I don't see the argument values being printed in your example, and if I replace them with dataframes, I still don't see them. So it's not quite as simple as you describe to get the voluminous output. Reproducible examples are needed if you want something fixed.> > Question: Is there some way to prevent mapply (or browser) from echoing the > function call when browser is called from within FUN?Yes, use the skipCalls argument to browser. Duncan Murdoch> > Request: If not, could the ability to turn off this echoing be provided. As > things stand, calling browser from within FUN, when FUN is a realistically > big function or has realistically big arguments, is a disaster. > > Thanks. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >