Hello, we wanted to deploy a Shiny-for-python app that in the background executes R-code through rpy2. Is it possibly to run both languages on Connect Cloud ? How is the installation of the dependencies handled then?
Thanks for your help.
Hi @Andreas_Lindauer
Great question! At this time we have unofficial support for rpy2, which is to say it currently works, but we have not officially announced it and may remove or make changes to how it is configured.
To use the existing functionality, you'll need to provide Connect Cloud with an rsconnect manifest.json file capturing the dependencies and R version you wish to use, similar to if you were publishing a shiny for R app. We will configure both R and Python environments, with the dependencies specified by the python requirements.txt and R manifest.json.
Generally this means running rsconnect::writeManifest() from the local R environment (and directory with your R source code) that uses your desired dependencies. I would also recommend publishing from GitHub and not Positron for this, as it is easier to see and directly control the manifest.json that we will use to construct the R environment.
Note that rsconnect-python has a similar write manifest command -- this is not the one you should use for this purpose, use the R rsconnect package to generate the manifest.
Hi again @Andreas_Lindauer
My apologies, I've been investigating this further and at the moment, while we do support simultaneous configuration of R and python dependencies using a method like I describe above, we do not support rpy2. So you could for example execute "R" from Python and have access to the dependencies in your manifest.json, but I recognize that's a substantially less capable way to interoperate between python and R.
I'll keep this post updated as our capability in this area improves.
Thanks!
Mike
Thank you very much Mike for your reply. I got the app.py published, however, calls to execute R from python using:
subprocess.run(["R", "-e", "1+1"])
or
subprocess.run(["Rscript", "-e", "1+1"])
Doesn't work. It doesn't seem to find the R executable. The following returns an empty string.
def where_is_rscript():
if platform.system() == "Windows":
cmd = ["where", "Rscript"]
else:
cmd = ["which", "Rscript"] # Linux / macOS
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout.strip()
Would R's plumber package work ? Appreciate any suggestions. Andreas.
Curious. I set up my own small application to test this but was unable to reproduce it. Does the log after you publish show Collecting your code's R dependencies...? That would be an indicator that Connect Cloud recognized a manifest.json and added an R interpreter to the runtime environment.
Thanks again Mike. Indeed there was a mistake in our manifest.json file. Now R is recognized!
Need to do some further testing with plumber and all the R-packages we need, but the main hurdle is taken. Thank you!
Just to follow up on this, we have corrected the issue that prevented rpy2 from working correctly. You should be able to use that now. It is necessary that both a requirements.txt and a manifest.json (identifying the R version and R dependencies) are included in your content.
thanks
sam
Now more comprehensively documented at Publishing content that uses R and Python to Connect Cloud. Thanks @Andreas_Lindauer for motivating us to improve this use case.