We will likely add similar information to our official documentation as well in future, but here is an informal post to aid folks who would like to use technologies such as rpy2 or reticulate in Connect Cloud.
Connect Cloud can create an environment for your content that includes both Python and R. Currently, these methods of interaction between the two languages are officially supported:
- R from Python using rpy2
- R from Python by execing
Rfrom Python code. (If R is not found, verify your manifest.json includes a desired R version.) - Python from R by using reticulate
Fundamentally, to cause Connect Cloud to set up both Python and R, you must provide Connect Cloud with both a requirements.txt file specifying your Python dependencies, and a manifest.json file specifying the desired R version and R packages your content requires. How you achieve this varies depending on whether you are publishing from the Publisher extension or from GitHub.
Publishing Python-centric projects that use R
Via Positron & VSCode Publisher Extension
- Begin the Publisher workflow to deploy your content as usual.
- During the deployment workflow, the extension will open a .toml configuration file in an editor tab. In this case, we may need to manually edit and save this file before finishing the deployment.
If the file does not already contain an[r]section, add this line to the end of the file:[r] - If you have created a
_dependencies.Rfile as described below, be sure it is checked in the list of files to be deployed. - Finish the deployment workflow as usual.
Via GitHub
We need to directly generate a manifest.json file that tells Connect Cloud the R version and R packages to provide. We recommend this procedure that uses renv.
-
In your project’s directory, start an R console. (The version of R you use here will be the version used by Connect Cloud as well.)
-
Initialize an renv environment by running
install.packages(“renv”) renv::init() -
Optionally, confirm that an
renv.lockfile has been generated including your dependencies. The manifest.json will be derived from the dependencies renv has detected, so it is helpful to check the results at this stage. If you do not see a package you expected, see the “Accurate capture of R dependencies” section below. -
Restart R if prompted by renv’s output
-
At the R console (not in your source files), install the
rsconnectpackage, which writes manifest.json files:install.packages(“rsconnect”) library(rsconnect) -
Ask rsconnect to write the manifest.json file:
rsconnect::writeManifest(appMode="shiny")The appMode parameter is required to prevent rsconnect from attempting and failing to detect an R application framework in your project, but the specific value will be ignored by Connect Cloud.
-
Optionally, confirm that a manifest.json file has been generated including a
platformlisting the R version on your computer, and including the packages your project requires in thepackageslist.
You can now proceed to publish your application as with any content via GitHub, being sure that the manifest.json is included in the repository.
Accurate capture of R dependencies from a python-centric project
Generally, our publishing software is good at detecting the R packages a project will require by inspecting the source files and specific packages in your local environment. However, use of R directly from Python can confuse it, causing it to miss R packages when writing the manifest.
If you find that your Python-based project cannot find R packages it requires when it runs in Connect Cloud, these step by step instructions are our recommended way to accurately convey your required R environment to Connect Cloud when your application is primarily written in Python.
-
Create an R file in your project’s root directory that uses the
libraryfunction to load each R package your content uses. In this guide, we will call this file _dependencies.R, but you could choose another name.For example, if your content uses functionality from the base64enc and ggplot2 packages, create a _dependencies.R file like the below:
library(base64enc) library(ggplot2) -
In your project’s directory, start an R console. (The version of R you use here will be the version used by Connect Cloud as well.)
-
Initialize an renv environment by running
install.packages(“renv”) renv::init() -
Optionally, confirm that an renv.lock file has been generated including your dependencies. The manifest.json file Connect Cloud ultimately consumes will be derived from the dependencies renv has detected, so it is helpful to check the results at this stage. If you do not see a package you expected, consult renv’s documentation.
-
Restart R if prompted by renv’s output