Installing r-cran-xxx packages for deployment

I am using rpy2 to wrap R in python. I was able to get R deployed with a packages.txt that contained r-base and requirements.txt that has the rpy2 package.

However the installed R packages on my local system are not found when deployed - I’ve tried adding r-cran-xxx in my packages.txt but most are somehow not found. how do I install these properly? Is there a way to launch a custom install.R script where I can run install.packages("xxx") instead?

I’ve added in the main streamlit app:
subprocess.run(["Rscript", "install.R"], check=True)

and in the install.R package:

packages <- c("xxx")
install_if_not_installed <- function(package) {
  if (!require(package, character.only = TRUE)) {
    install.packages(package, repos = "http://cran.rstudio.com/", lib="/usr/lib/R/library")
  }

# Install the packages
sapply(packages, install_if_not_installed)
}

Getting the error:

Loading required package: xxx

Warning in install.packages(package, repos = "http://cran.rstudio.com/",  :

  'lib = "/usr/lib/R/library"' is not writable

Error in install.packages(package, repos = "http://cran.rstudio.com/",  : 

  unable to install packages

Calls: sapply -> lapply -> FUN -> install.packages

In addition: Warning message:

In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :

  there is no package called ‘xxx’

Execution halted

I’ve chosen that libpath since the R libraries for the deployed app are in /usr/lib/R/library. What path should I pass that I can write to? or how do I gain permission to write in it?

Hi @hud

Instead of installing via subprocess.run, you can have the necessary R packages specified in packages.txt as demonstrated in a tutorial video I’ve created earlier:

r-base
r-base-dev
r-cran-ggplot2
r-cran-cowplot

To run the R code, specify this in the Streamlit app:

process2 = subprocess.Popen(["Rscript", "plot.R"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
result2 = process2.communicate()
image = Image.open('plot.png')
st.image(image)

The GitHub repo with the code for this Streamlit app that runs R code is available at:

Try out the demo app here:

The tutorial video is at:

Hope this helps!

Yes this is the first thing I tried but some of my packages are not found from cran, e.g. E: Unable to locate package r-cran-mlr3 - how do you deal with this?

Could Docker be the only solution?

Hi @hud
In regards to the issue with locating the package, have you tried r2u

Please see the solution post below for details:

Yes! I am only now just seeing this - but it seems like some modifications need to be done at the root level. Alternatively there is a docker container GitHub - rocker-org/r2u: Rocker for use with r2u repository but I think there’s no way to use deploy with Docker containers with Streamlit cloud

Is it possible to modify at root level when deploying on streamlit community cloud share.streamlit.io ?

Hi @hud

You could explore using the command line via the subprocess module such as using the Popen method.

If using docker here is a Docs page on this:

Hope this helps!

im trying to use sn package from R and its getting error

  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'error'
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [12 lines of output]
      /home/adminuser/venv/lib/python3.11/site-packages/setuptools/__init__.py:80: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
      !!
      
              ********************************************************************************
              Requirements should be satisfied by a PEP 517 installer.
              If you are using pip, you can try `pip install --use-pep517`.
              ********************************************************************************
      
      !!
        dist.fetch_build_eggs(dist.setup_requires)
      Error: rpy2 in API mode cannot be built without R in the PATH or R_HOME defined. Correct this or force ABI mode-only by defining the environment variable RPY2_CFFI_MODE=ABI
      cffi mode: CFFI_MODE.ANY
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
WARNING: You are using pip version 22.0.3; however, version 23.3.1 is available.
You should consider upgrading via the '/home/adminuser/venv/bin/python -m pip install --upgrade pip' command.
Checking if Streamlit is installed

im using windows and rpy2==3.3.6 and still get the error pls help

Hi @candika

According to the following error, it is expecting R to be pre-installed:

Can you ensure that R is installed before pip installing rpy2:

https://cran.r-project.org/bin/windows/base/

Hope this helps!