Publishing a shiny app code that uses python through reticulate on RStudio Connect

Hi,

I was trying to follow this guide on how to publish a shiny app code that uses python through reticulate on RStudio Connect.

What I did is create a virtualenv in the same directory where the app.R script is, which has both the server and ui parts as well as code that uses reticulate in order to import and use several python packages, using the command:

conda create -p .venv python=3.7

I then installed all the python packages that app.R is importing with reticulate: numpy, pandas. scipy, and scanpy.

I then followed this post and created a .RProfile file in the same directory where app.R and .venv are, which has:

Sys.setenv(RETICULATE_PYTHON = "~/.venv/bin/python3.7")

Then, in app.R I have these lines:

reticulate::virtualenv_create(".venv", python = "python3.7")
reticulate::use_virtualenv('.venv',required = TRUE)

I'm also including in the publishing bundle a requirements.txt file with these lines:

numpy==1.18.1
pandas==1.0.4
scipy==1.4.1
scanpy==1.5.1

When I publish, at deployment on the RStudio Connect server I get these messages:

2020/06/11 12:41:44.556541714 Using Packrat dir /opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.6.3
2020/06/11 12:41:45.352030580 Error in value[[3L]](cond) : error in running command
2020/06/11 12:41:45.352053668 Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
2020/06/11 12:41:45.352119182 Execution halted

The error above is preceded in the log by these messages:

06/11 13:45:10.237

warning: using reticulate but python was not specified; will use python at /usr/bin/python3

06/11 13:45:10.237

Did you forget to set the RETICULATE_PYTHON environment variable in your .Rprofile before publishing?

Any idea what's wrong?

Hi ndr,

Many of the things you've tried may be in conflict with one another. For example, conda create, does not create a virtualenv, it create a conda environment. And virtualenv_create in your R script would attempt to create a new virtualenv everytime your app runs! Our recommendation would be:

  1. Create a virtualenv by running reticulate::virtualenv_create(...) in the R Console!
  2. Install the packages you need into that virtualenv using reticulate.
  3. Set the .Rprofile option
  4. Restart your R session

These 4 steps are a one-time thing for your project, your virtualenv is now created and ready to go.

Now, inside your application code, do not have ANY reticulate code that interacts with the environment. Simply use library(reticulate) and your import commands. The .Rprofile will ensure the correct virtualenv you created for this project is used.

When you deploy to Connect, you do not need a requirements.txt file. In fact, you don't need to do anything. Just click publish.

1 Like

Thanks a lot slopp,

Now I'm getting an error in the Uploading bundle for application step but I'll start a new topic for it