Some options and hacks
When you can’t install R packages from a repository online, what can you do? I won’t discuss any solution deeply; instead I’ll discuss a number of alternatives briefly, and share links for you to explore.
Often the reason why you can’t access online repositories is because your computer is part of a larger network that someone else manages and limits. Find that person and ask them if they can install in your computer the R environment you need. If that isn’t possible, keep reading but keep in mind that the solutions I describe may take some time and effort; and any hack may leave your system in bad shape. Even if you have to face red tape or wait some time, it may be best to find the right person for this job.
Before you bother installing anything, you may want to first try the packages you think you want. At http://rstudio.cloud/ you can get an online instance of RStudio and install in it whatever packages you want. If what you experience is not what you want, you are done; if it is what you want, keep reading.
Docker allows you to build a mini “guest” computer – called “container” – and run it from any “host” computer. With Docker you can create the R environment you want, including a specific version of R and R packages. Explore the useful Docker images from the rocker project; they can help you create a Docker container with R, RStudio, and all the packages you need – quickly. You can run such a complete container even offline, and from a host computer that lacks R and RStudio.
You may struggle to understand, install, or use Docker; I did. It can be particularly challenging on Windows. Keep pushing. To create the portable and reproducible R environment I describe here, what you need to learn of Docker is only a little. It’s worthwhile.
Developers create R packages as a bunch human-oriented text-files and folders. But installed packages are a little different, and the source code needs to be transformed before your computer can use it. That transformation may be done by CRAN, before you even get the package. But you can do it too – if your computer is prepared to build R packages; you can check that with
# install.packages("pkgbuild") pkgbuild::check_build_tools() #> Your system is ready to build packages!
If your system is ready to build packages you have a couple of options. You can use miniCRAN to create a local CRAN repository, including a target package and all its dependencies; later you can use your local repository to build each package with
devtools::build() and install it with
If you want to share your repository with others, you may use the package drat to create an online clone and serve it on GitHub pages. Your drat repository will allow other folks to download your selection of R packages, and even use it locally offline – just as you did. Your drat repository would also allow anyone online to install any package directly from it (instead of CRAN or another repository).
If your system is not ready to build packages you may make it so, or get the pre-built version of the packages you need, and later use them in a similar system offline. This is a hack and you should try to avoid it; but if you must do it, you may abuse the package renv for this.
The package renv creates a local library of binary packages under your working directory: (1) Create a new RStudio project; (2) initialize renv with
renv::init(); and (3) install the packages you need with
renv::install(). The installed version of all packages will now be under the directory ./renv/library/. Those packages will likely work on another computer running the same platform and minor version of R. For example, if you are running Windows 10 and R 3.6.x, the packages should work on another computer running Windows 10 and 3.6.y but not R 3.5.x. When you want to use one of the hacked packages, you will need to tell
library() where to find them – with the argument
.libPaths() #>  "/home/mauro/R/x86_64-pc-linux-gnu-library/4.0" #>  "/opt/R/4.0.0/lib/R/library" all_libraries <- c(.libPaths(), "path/to/your/hacked/packages") all_libraries #>  "/home/mauro/R/x86_64-pc-linux-gnu-library/4.0" #>  "/opt/R/4.0.0/lib/R/library" #>  "path/to/your/hacked/packages"
library(somepackage, lib.loc = all_libraries)
R defaults to look for packages under the paths given by
.libPaths(); but placing your hacked packages there is a bad idea because you may leave your entire R environment in bad shape. Remember: This is a hack and you should try to avoid it.