How To Make Sure Installed Packages Deploy Correctly


Deploying content to RStudio Connect can fail when the deployment process cannot properly identify where the packages were installed from. One clue that this situation may be occurring is if deployment fails with an error like 

Failed to retrieve package sources for myPackage from CRAN.


Unable to determine package source for [source] package.

This particular problem is most likely when myPackage has been installed using the install.packages command, when myPackage is an internal package being served from RStudio Package Manager, or when deployment is occurring in an environment where public CRAN repositories are not available.

It may occur in other instances where the R installation cannot find the package in question at deployment time, for example if the package authors have moved it from BioConductor to CRAN or vice-versa.

The fix in this article might be relevant for packages installed directly from a git repository or from BioConductor, but that is less likely.

Fix it With options('repos')

The RStudio Connect deployment process attempts to find required package versions in the repositories enumerated in the users's options('repos'). If the package (or package version) cannot be found in those repositories, the deployment process attempts to install from a public CRAN mirror, which results in an error when CRAN is inaccessible or the package is not from CRAN.

The simplest way to avoid this problem is ensure the repositories for any installed packages (or package versions) are added to the user's options('repos') before attempting to deploy to RStudio Connect. 

This should be done in a permanent way, for example by setting options('repos') in an .Rprofile file or setting it in the RStudio menu under Tools > Global Options > Packages.

If attempting to deploy still fails, it might be necessary to re-install the package using install.packages('myPackage').

Why Is This Happening

This problem is usually a result of package installation using an inline repo, for example

install.packages("myPackage", repos = "")

In order to find the URL of the repository at deploy-time, the deployment process looks at all packages available in the repositories it knows about - those specified in options('repos'). If it can't find the package in one of those repos, the error occurs upon deployment.

When a package is installed using a repo specified in the call to install.packages, R captures only specific details on the installation repository. If the repository isn't CRAN, these details often don't include enough detail for the RStudio Connect deployment process to find it.