Troubleshooting Package Errors with RStudio Connect Deployments


RStudio Connect deployments can fail when the deployment process cannot find a package. Frequent error messages when this situation occurs include 

Failed to retrieve package sources for myPackage from CRAN.
Unable to determine package source for [source] package.
A required R package was found in the package repository, but the specified version is not available.

When deploying, RStudio Connect finds the required packages in the CRAN and/or RStudio Package Manager repositories in your options('repos') at deployment time (when you press the publish button or run rsconnect::writeManifest).

The problem explained in this article is likely in the following situations:

  • A package has been installed from local sources and does not exist in a CRAN or RStudio Package Manager repository.
  • A package has been installed from CRAN or RStudio Package Manager using the install.packages command with the repos option specified. 
  • The server URL of your RStudio Package Manager has recently changed.
  • You installed a new version of a package from CRAN that is not yet available on RStudio Package Manager.
  • You installed an internal package from RStudio Package Manager in addition to CRAN packages from a public CRAN mirror.
  • Authors of a public package have recently moved it from BioConductor to CRAN or vice-versa.

In most of these situations, the fix is just to make sure the deployment process can find the repository with the package in it.

If the package was installed from local sources, there is no repository with the package, and the best path is to put the package in a repository where RStudio Connect can access it. This article has more on what R package repositories are and how they differ from your local library, and this article has more on managing R packages in air gapped environments.

Fix it With options('repos')

The simplest solution is to make sure RStudio Connect can find all required package versions at deployment time by adding the needed repository to options('repos') before attempting to deploy to RStudio Connect. 

The options('repos') should be configured in a permanent way. There are a variety of ways to permanently set repository options depending on whether you want them set at the project, user, or server level. For more information on configuring options('repos'), please see this support article.

It may be a good idea to re-install the package with install.packages('myPackage') with no other arguments before attempting your deployment. This both ensures that the repos option is set correctly and that the RStudio Connect deployment process will know how to find the install repository.

An example of this problem

Let's say I am developing content in RStudio and I need to install a package from an internal RStudio Package Manager repository. However, my machine is currently configured like this:

> options('repos') 
[1] TRUE

Since the package is not available on the public CRAN mirror, I might choose to install just the one package using the install.packages command from RStudio Package Manager like:

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

When I go to deploy, I will get an error. R's install.packages command does not record the repos option and I will get an error because the deployment cannot find the RStudio Package Manager repository. 

Once I add the repository to my options('repos') with

options('repos' = c(options('repos)$repos, RSPM = "")

the deployment will succeed. Depending on how the package was initially installed, it might be necessary to install.packages("myPackage") beforehand.