[R] install.packages() R vs RStudio

John Fox j|ox @end|ng |rom mcm@@ter@c@
Mon Aug 17 21:33:42 CEST 2020


Hi Duncan,

What you say is entirely sensible.

Yes, it's primarily the silent part that seems problematic to me. 
Messages about masking are uninteresting until one encounters a problem, 
and then they may provide an important clue to the source of the problem.

As to this specific case: It's not clear to me why it's necessary or 
even desirable for RStudio to mask utils::install.packages(). After all 
RStudio provides an alternative route to package installation via the 
Packages tab, and it wouldn't have been hard to name the function 
something different from install.packages() to provide additional 
functionality via direct commands.

Best,
  John

On 2020-08-17 3:15 p.m., Duncan Murdoch wrote:
> Hi John.
> 
> I suspect most good front ends do similar things.  For example, on 
> MacOS, R.app messes up "history()".  I've never used ESS, but I imagine 
> one could find examples where it acts differently than base R:  isn't 
> that the point?
> 
> One hopes all differences are improvements, but sometimes they're not. 
> If the modifications cause trouble (e.g. the ones you and I have never 
> experienced with install.packages() in RStudio, or the one I experience 
> every now and then with history() in R.app), then that may be a bug in 
> the front-end.  It should be reported to the authors.
> 
> R is designed to be flexible, and to let people change its behaviour. 
> Using that flexibility is what all users should do.  Improving the user 
> experience is what front-end writers should do.  I don't find it 
> inadvisable at all.  If it's the "silent" part that you object to, I 
> think that's a matter of taste.  Personally, I've stopped reading the 
> messages like
> 
> "Attaching package: ‘zoo’
> 
> The following objects are masked from ‘package:base’:
> 
>      as.Date, as.Date.numeric"
> 
> so they may as well be silent.
> 
> Duncan Murdoch
> 
> 
> On 17/08/2020 10:02 a.m., John Fox wrote:
>> Dear Duncan,
>>
>> On 2020-08-17 9:03 a.m., Duncan Murdoch wrote:
>>> On 17/08/2020 7:54 a.m., Ivan Calandra wrote:
>>>> Dear useRs,
>>>>
>>>> Following the recent activity on the list, I have been made aware of
>>>> this discussion:
>>>> https://stat.ethz.ch/pipermail/r-help/2020-May/466788.html
>>>>
>>>> I used to install all packages in R, but for simplicity (I use RStudio
>>>> for all purposes), I now do it in RStudio. Now I am left wondering
>>>> whether I should continue installing packages directly from RStudio or
>>>> whether I should revert to using R.
>>>>
>>>> My goal is not to flare a debate over whether RStudio is better or 
>>>> worse
>>>> than R, but rather simply to understand whether there are differences
>>>> and potential issues (that could lead to problems in code) about
>>>> installing packages through RStudio.
>>>>
>>>> In general, it would be nice to have a list of the differences in
>>>> behavior between R and RStudio, but I believe this should come from the
>>>> RStudio side of things.
>>>>
>>>> Thank you all for the insights.
>>>> Ivan
>>>>
>>>
>>> To see the install.packages function that RStudio installs, just type
>>> its name:
>>>
>>>   > install.packages
>>> function (...)
>>> .rs.callAs(name, hook, original, ...)
>>> <environment: 0x7fe7dc5b65b0>
>>>
>>> You can debug it to see the other variables:
>>>
>>>   > debug(install.packages)
>>>   > install.packages("abind")
>>> debugging in: install.packages("abind")
>>> debug: .rs.callAs(name, hook, original, ...)
>>> Browse[2]> name
>>> [1] "install.packages"
>>> Browse[2]> hook
>>> function (original, pkgs, lib, repos = getOption("repos"), ...)
>>> {
>>>       if (missing(pkgs))
>>>           return(utils::install.packages())
>>>       if (!.Call("rs_canInstallPackages", PACKAGE = "(embedding)")) {
>>>           stop("Package installation is disabled in this version of
>>> RStudio",
>>>               call. = FALSE)
>>>       }
>>>       packratMode <- !is.na(Sys.getenv("R_PACKRAT_MODE", unset = NA))
>>>       if (!is.null(repos) && !packratMode &&
>>> .rs.loadedPackageUpdates(pkgs)) {
>>>           installCmd <- NULL
>>>           for (i in seq_along(sys.calls())) {
>>>               if (identical(deparse(sys.call(i)[[1]]),
>>> "install.packages")) {
>>>                   installCmd <- gsub("\\s+", " ",
>>> paste(deparse(sys.call(i)),
>>>                     collapse = " "))
>>>                   break
>>>               }
>>>           }
>>>           .rs.enqueLoadedPackageUpdates(installCmd)
>>>           stop("Updating loaded packages")
>>>       }
>>>       .rs.addRToolsToPath()
>>>       on.exit({
>>>           .rs.updatePackageEvents()
>>>           .Call("rs_packageLibraryMutated", PACKAGE = "(embedding)")
>>>           .rs.restorePreviousPath()
>>>       })
>>>       original(pkgs, lib, repos, ...)
>>> }
>>> <environment: 0x7fe7db925588>
>>>
>>> The .rs.callAs function just substitutes the call to "hook" for the call
>>> to the original install.packages.  So you can see that they do the
>>> following:
>>>    - they allow a way to disable installing packages,
>>>    - they support "packrat" (a system for installing particular versions
>>> of packages, see https://github.com/rstudio/packrat),
>>>    - they add RTools to the path (presumably only on Windows)
>>>    - they call the original function, and at the end update internal
>>> variables so they can show the library in the Packages pane.
>>>
>>> So there is no reason not to do it in R.
>>>
>>> By the way, saying that this is a "modified version of R" is like saying
>>> every single user who defines a variable creates a modified version of
>>> R.  If you type "x" in the plain R console, you see "Error: object 'x'
>>> not found".  If you "modify" R by assigning a value to x, you'll see
>>> something different.  Very scary!
>>
>> I can't recall ever disagreeing with something you said on the R-help,
>> but this seems to me to be off-base. While what you say is technically
>> correct, silently masking a standard R function, in this case, I
>> believe, by messing with the namespace of the utils package, seems
>> inadvisable to me.
>>
>> As has been noted, cryptic problems have arisen with install.packages()
>> in RStudio -- BTW, I use it regularly and haven't personally experienced
>> any issues. One could concoct truly scary examples, such as redefining
>> isTRUE().
>>
>> Best,
>>    John
>>
>>>
>>> Duncan Murdoch
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list