[R-pkg-devel] attach

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Apr 6 17:33:51 CEST 2020


On 06/04/2020 10:59 a.m., Ben Bolker wrote:
> 
>    Fair enough.  I'm going to derail/repurpose this thread to ask a
> couple of questions about attach().
> 
>     I have often used with() for situations where I want to evaluate a
> bunch of expressions with the elements of a parameter vector, e.g.
> inside a gradient function for deSolve::ode().  However, I have found
> this very hard to debug because (AFAIK) you can't debug-step through the
> components of a with() expression.
>    Potential solutions for this include
> 
>   1. attach(params); on.exit(detach(params))
> 
>    * this will be flagged by CRAN
>    * I have found some really surprising (to me) precedence issues with
> this _when used in a package context_ - it looks like the elements in
> 'params' are found _after_ built-in objects in R?? (I have to take some
> time to make a MRE of this

I suspect what happened is that you had a copy of a built-in function in 
the global environment.  The global environment always comes ahead of 
attach()'d environments.  On the other hand, with() gets it right.

You can see this:

x <- 123
df <- data.frame(x = 456)
with(df, x)
#> [1] 456
attach(df)
#> The following object is masked _by_ .GlobalEnv:
#>
#>     x
x
#> [1] 123
detach(df)

As for debugging, the setBreakpoint() function can set a breakpoint in 
the code block in with().  RStudio uses that or some equivalent for 
setting breakpoints, so it can set breakpoints within the block.

If you call debug() on a function and get to a with() statement, you can 
eventually get to the code within it by hitting "s" (step in) several 
times.  It's fairly scary stepping into .Internal(), but it does 
eventually get you there.

Duncan Murdoch

> 
>    2. The zeallot package does 'unpacking' as with Python tuples.  I was
> worried about dragging in tidyverse dependencies, but it looks like it
> doesn't actually Import: anything.
>     This doesn't quite do what I want, as I want to unpack using the
> names in the object (which makes it look perfect for the attach() solution)
> 
> 
>    Thoughts?
> 
> On 2020-04-06 10:49 a.m., Dirk Eddelbuettel wrote:
>>
>> On 6 April 2020 at 08:38, Ben Bolker wrote:
>> |   Just reply to the CRAN maintainers and explain this situation.  It¨s
>> | slightly buried, but the e-mail you received does say:
>> |
>> | > If you are fairly certain the rejection is a false positive, please reply-all to this
>> | > message and explain.
>>
>> True, but this misses the "Letter of the law" versus the "Spirit of the law".
>>
>> It might be worth mentioning that use of attach() is seen, to find one poor
>> analogy, pretty much like use of global variables these days. "Just because
>> you could does not mean you should".
>>
>> See e.g. one of the first google hits for 'r do not use attach' here:
>> https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead
>>
>> Dirk
>>
> 
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>



More information about the R-package-devel mailing list