13 Package Management

13.1 Package Installation

RStudio Connect installs the R package dependencies of Shiny applications and R Markdown documents when that content is deployed. The RStudio IDE uses the rsconnect and packrat packages to bundle the relevant source code and document its dependencies. RStudio Connect then uses packrat to duplicate those package dependencies on the server.

Packrat attempts to re-use R packages whenever possible. The shiny package, for example, should be installed only when the first Shiny application is deployed. Subsequent Shiny applications can use that package and see faster deployments as a result. Packrat also allows multiple versions of a package to exist on a system. Two Shiny applications referencing different versions of shiny will reference the correct Shiny installation and these two packages will not conflict with each other.

Resolving which packages need installing and which are already available all happens when you deploy content to RStudio Connect.

13.2 Private Repositories

Packrat records details about how a package was obtained in addition to information about its dependencies. Most public packages will come from a public CRAN mirror. Packrat lets RStudio Connect support alternate repositories in addition to CRAN.

Learn how to create your own custom repository; this directory can then be shared over HTTP or through a shared filesystem.

Here are some reasons why your organization might use an alternate/private repository.

  1. Internally developed packages are made available through a corporate repository. This is used in combination with a public CRAN mirror.

  2. All packages (private and public) are approved before use and must be obtained through the corporate repository. Public CRAN mirrors are not used.

  3. Direct access to a public CRAN mirror is not permitted. A corporate repository is used as a proxy and caches public packages to avoid external network access.

RStudio Connect supports private repositories in these situations given that the deploying instance of R is correctly configured. No adjustment to the RStudio Connect server is needed.

Repository information is configured using the repos R option. Your users will need to make sure their desktop R is configured to use your corporate repository.

RStudio IDE version 0.99.1285 or greater is needed when using repositories other than the public CRAN mirrors.

We recommend using an .Rprofile file to configure multiple repositories or non-public repositories.

The .Rprofile file should be created in a user’s home directory.

# A sample .Rprofile file with two different package repositories.
local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cran.rstudio.com/"
  r["mycompany"] <- "http://rpackages.mycompany.com/"
  options(repos = r)
})

This .Rprofile creates a custom repos option. It instructs R to attempt package installation first from "CRAN" and then from the "mycompany" repository. R installs a package from the first repository in "repos" containing that package.

With this custom repos option, you will be able to install packages from the mycompany repository. RStudio Connect will be able to install these packages as code is deployed.

For more information about the .Rprofile file, see help(Startup) in R. For details about package installation, see help(install.packages) and help(available.packages).

13.3 Private Packages

Packages available on CRAN, a private package repository, or a public GitHub repository are automatically downloaded and built when an application is deployed. RStudio Connect cannot automatically obtain packages from private GitHub repositories, but a workaround is available.

We recommend using a private repository to host internal packages when possible. See Section 13.2 for details.

The configuration option Server.SourcePackageDir can reference a directory containing additional packages that Connect would not otherwise be able to retrieve. This directory and its contents must be readable by the Applications.RunAs user. Connect will look in this directory for packages before attempting to obtain them from a remote location.

This feature has some limitations.

  • The package must be tracked in a git repository so that each distinct version has a unique commit hash associated with it.
  • The package must have been installed from the git repository using the devtools package so that the hash is contained in the DESCRIPTION file on the client machine.

If these conditions are met, you may place .tar.gz source packages into per-package subdirectories of SourcePackageDir. The proper layout of these files is <package-name>/<full-git-hash>.tar.gz.

For example, if Server.SourcePackageDir is defined as /opt/R-packages, source bundles for the MyPrivatePkg package are located at /opt/R-packages/MyPrivatePkg. A commit hash of 28547e90d17f44f3a2b0274a2aa1ca820fd35b80 needs its source bundle stored at the following path:

/opt/R-packages/MyPrivatePkg/28547e90d17f44f3a2b0274a2aa1ca820fd35b80.tar.gz

When private package source is arranged in this manner, users of RStudio Connect will be able to use those package versions in their deployed content.

Be aware that this mechanism is specific to the commit hash, so you will either need to make many git revisions of your package available in the SourcePackageDir directory hierarchy or standardize to a particular git commit of the package.