[R] install.packages() R vs RStudio

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Aug 17 15:03:34 CEST 2020


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!

Duncan Murdoch



More information about the R-help mailing list