Reticulate - Running python chunks in Rmarkdown - python

Maybe I'm missing something, but if the following code is the content of my Rmd file
```{r}
library(reticulate)
use_virtualenv("r-reticulate")
py_available(TRUE)
```
```{python}
a = 7
print(a)
```
```{r}
py$a
```
when I Knit the file, the output for the last chunk is 7 (as expected). On the other hand, clicking the run all button in Rstudio (or running chunks one by one), results on NULL for the last chunk.
Comparing with the R notebook example it seems like assigning something to flights in the python chunk should make py$flights available for R, but that doesn't seem the case.
Questions:
Is there a way to access from R a variable created in a Python chunk previously ran (not knit)? How to "export" to R a variable created within a python chunk?
What is a good reference to understand what happens when I click the run button in a python chunk of a Rmarkdown file?
EDIT: Ok so after seeing the first answers here, I did update both knitr and rmarkdown to the latest version, but still had the same problem.
I added py_available(TRUE) to my file to make sure it was initialized, still, last chunk results in 7 when knitted, but running chunks one-by-one results in
> py$a
Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'module' object has no attribute 'a'
The problem is: Assigning a value to a in the python chunk isn't doing anything to py$a in the R environment. Maybe this "shared" environment between R and python isn't how the package is supposed to work? Also, for some extra information
> py_config()
python: /usr/bin/python
libpython: /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome: /usr:/usr
version: 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]
numpy: /usr/lib/python2.7/dist-packages/numpy
numpy_version: 1.12.1
python versions found:
/usr/bin/python
/usr/bin/python3

Rmarkdown / knitr:
Running the chunks:
Running the chunks without knitting the document is not supported so far. See here: https://github.com/yihui/knitr/issues/1440 or Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown.
Edit: Workaround by Freguglia:
"Workaround is to turn python chunks into R chunks and just wrap the whole content in the py_run_string() function, so whatever you assign in that piece of code is accessible from R by py$variable_name."
Knitting the document:
One way is to upgrade knitr as suggested above, but you dont have to and you also dont need RStudio daily build.
If you have a version of knitr prior to 1.18, you can include:
```{r setup, include = FALSE}
knitr::knit_engines$set(python = reticulate::eng_python)
```
, see here: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup.
Python:
If it doesnt work ensure the python connection is running outside of rmarmdown/knitr:
py_run_string("x = 10"); py$x.
In case that also doesnt work, you should check:
py_available() and py_numpy_available().
If it returns FALSE: Try to initialize it with: py_available(TRUE).
If that´s still a no - check your config:
py_config()
It will give you further hints on the problem:
Examples for me were: different bit versions of R and python (32 vs 64) or somehow i ran into trouble having installed both Python2.7 and seperately Anaconda.

This is fixed in current RStudio Desktop e.g. 1.2.1114. But if you are like me stuck with RStudio Server Pro 1.1.456 a better workaround than using py_run_string might be to use reticulate::repl_python() which gives you a Python console within the R console and allows you to run your python chunks by copy pasting them into the console.
workaround:
working:

You must use the Rstudio daily build (source) and upgrade knitr, rmarkdown to the latest version.
> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’

Related

Error - cannot install Miniconda with RStudio

I have written a R script where I use some python lines through the reticulate package.
I need to share it with some colleagues who don't know about programming and I've created a batch file so I can run it without them even opening R.
However, I tried using the install_miniconda() function to silently install python to run the code without them knowing (I guess people are reluctant to installing a couple of programs) but R throws an error:
> reticulate::install_miniconda(path="C:/")
# * Installing Miniconda -- please wait a moment ...
# * Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...
# trying URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'
# Content type 'application/octet-stream' length 74687656 bytes (71.2 MB)
# downloaded 71.2 MB
# Error: Miniconda installation failed [unknown reason]
I tried without passing any path but my computer user has spaces on it so I cannot use it, that's why I resorted to supplying it with the root path "C:/"
Can someone help me understand what is happening?
Note: I am using R 4.2.1 on Windows 11 (also tried on Windows 10 with the same result)
Try installing rminiconda from the github like this:
remotes::install_github("hafen/rminiconda")
rminiconda::install_miniconda(name='your_name')
After that you can specify the installation using reticulate like this:
py <- rminiconda::find_miniconda_python("your_name")
reticulate::use_python(py, required = TRUE)

R reticulate specifying python executable to use

First, I'm working on a Windows machine. I would like to specify a specific version of python to use in RStudio. I would like RStudio to use python 3 in the ArcGIS Pro folder in order to have arcpy available, along with the licensed extensions. I have reticulate installed and have tried the following methods to force RStudio to use the ArcGIS Pro version of python.
First I tried this:
library(reticulate)
use_python("C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe", required = TRUE)
The resulting error:
Error in path.expand(path) : invalid 'path' argument
Following some other tips, I tried setting the environment before loading the reticulate library.
Sys.setenv(RETICULATE_PYTHON = "c:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe")
library(reticulate)
Then I retrieve information about the the version of Python currently being used by reticulate.
py_config
Error in path.expand(path) : invalid 'path' argument
I also tried creating and editing the .Renviron by using the usethis package
usethis::edit_r_environ()
Then entering the following
RETICULATE_PYTHON="C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
And saving it, restarting R..
library (reticulate)
py_config()
Error in path.expand(path) : invalid 'path' argument
And, to confirm, here is the location...
Any ideas on why I continue to receive invalid 'path' argument
I was having a similar issue. After trying a whole assortment of things, I finally installed an archived version of reticulate (reticulate_1.22) instead of using the most up-to-date version (reticulate_1.23) and now the issue is gone. It appears that this bug has been brought to the developers' attention (https://github.com/rstudio/reticulate/issues/1189).
Try using
use_python("C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3")
Have you tried replacing Program Files with PROGRA~1 and have you maybe also checked for example a command like dir("path/to/your/env") although tbh your screenshot looks ok;
btw just in case - after editing your .Renviron file you need to restart your RStudio/R session for changes to take effect;
RStudio version 2022.02 has Python interpreter selection now available in Global Options
I ran into the same error with R version R-4.1.1, but when I switched back to the previous version R-4.0.5 everything worked as expected. It's a quick workaround but doesn't solve the underlying issue in the current version.

ggplot2 crashing (Rstudio1.4.1717) when combined with python in Rmarkdown

tl:dr Rmarkdown crashes when loading ggplot2 library in combination with using python chunks in Rstudio 1.4.1717
My Rmarkdown file is crashing after I upgraded to 1.4.something. It's the same ggplot2 crash people are talking about in other threads (e.g. ggplot Crashing system, GGplot crashes Rstudio after upgrade or suddenly geplot crashes). The fix to upgrade to 1.4.1717 does not resolve the issue for me.
If I use library(ggplot2) in the console it works, in a script or new markdown file it works. However, combined with using reticulate it crashes. Here's chunk 1
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
library(dygraphs)
library(xts)
library(ggplot2)
library(lubridate)
library(scales)
library(ggpubr)
followed by chunk 2
{python ,echo=FALSE, message = FALSE, warning = FALSE,results = FALSE}
import os
path="/Users/-----"
os.chdir(path)
os.getcwd()
Chunk 1 works without any problem. However, as soon as I use import os it crashes. If I disable the following packages #library(ggplot2) #library(ggpubr) in chunk 1, then it does not crash. If I run chunk 2 before chunk 1 it crashes as soon as I load library(ggplot2).
I'm working with Miniconda (Python 3.6.13) as interpreter in Rstudio. Everything in the markdown works except for the combination of using ggplot2 (or ggpubr, but that's because it loads ggplot2) with any python chunk (i.e. I tried just using the following chunk #3
{python}
A=2
but this also crashed Rstudio. Any other people having similar issues? Is there any solution to this?
Solved the problem. It was not Rstudio that needed an update, but I was running an old version of R itself. I am still not sure why the problem persisted on my new work computer and not on my backup computer with the same versions of R, Rstudio and python (reticulate) running, but updating R fixes the problem.

Unable to change the Python to be used for interacting with R using reticulate

I want to use a specific Python version: /Users/aviral.s/.pyenv/versions/3.5.2/bin/python. This version is not available for R.
I tried reading the documentation but following all the three steps(setting the env variable, using the API use_python() didn't help either.
With sudo, I run the following code:
library("reticulate")
py_config()
use_python("/Users/aviral.s/.pyenv/versions/3.5.2/bin/python")
py_config() # Unchanged.
I tried using any of the available ones in the py_config() which worked by setting the environment variable as in here
However, if I set the same env variable to my pyenv version, I get this error:
> library("reticulate")
> py_config()
Error in initialize_python(required_module, use_environment) :
Python shared library not found, Python bindings not loaded.
My env variable is correct:
echo $RETICULATE_PYTHON
/Users/aviral.s/.pyenv/versions/3.5.2/bin/python
I ran into the same problem a few days ago and i had to jump through all kinds of hoops to get where i wanted and i am not sure which one did it for me, but what definitely helped was using py_discover_config() instead of the regular py_config() command.
what might be another problem, is that apparently a python version with installed numpy will always be preferred by reticulate:

Python Reticulate not working in Rstudio Cloud

I am a big fan of Rstudio Cloud and would like to inter-grate R and Python by using the package Reticulate.
It looks like Rstudio Cloud is using python 2.7 (no problems with that). When I try to write Python Code in an R markdown document, nothing gets run.
---
title: "reticulate"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(reticulate)
py_config()
```
```{python}
import pandas
x = 4
```
Python code does not get run.
I am also finding that if I want to install python packages in an R script using reticulate. I have to create a virtual environment. What is the reason behind that?
library(reticulate)
virtualenv_create("r-reticulate")
virtualenv_install("r-reticulate", "scipy")
virtualenv_install("r-reticulate", "pandas")
If I use conda_install, I get an error message.
conda_create("r-reticulate")
Error: Unable to find conda binary. Is Anaconda installed?
conda_install("r-reticulate", "scipy")
Error: Unable to find conda binary. Is Anaconda installed?
The goal is to have python working in Rstudio cloud on R markdown. I can not install packages and execute code.
I just succeeded in getting Conda installed in Rstudio cloud after receiving the same error message as you1, so thought I'd share how I got this working.
I created two scripts:
to install miniconda (i think that's the step you're missing, and why Conda didn't work for you) and then restartSession for this to be accessible
to seperately store the commands for setting up Conda, with the running of this script passed as a command to the call to restartSession (because otherwise the commands are triggered before R has restarted, and they fail; sys.sleep() didn't seem to work, but this method did)
setup.R
setwd("/cloud/project") # to ensure students get required resources
install.packages("rstudioapi") # to restart R session w/ installations
install.packages("reticulate") # for python
reticulate::install_miniconda("miniconda") # for python
# Restart again to make sure all system things are loaded
# and then create a new Conda environment
rstudioapi::restartSession(command="source('nested_reticulate_setup.R')")
nested_reticulate_setup.R
reticulate::conda_create("r-reticulate")
reticulate::conda_install("r-reticulate", "scipy")
Sys.setenv(RETICULATE_PYTHON="/cloud/project/miniconda/envs/r-reticulate/bin/python")
reticulate::use_condaenv("r-reticulate")
osmnx <- reticulate::import("scipy")
Then if you make a call to scipy, eg scicpy$`__version__` , I believe it should work for you without that error you observed.
I couldn't find a solution to this issue elsewhere, so thought it worth responding to this old post in case it helps somebody some day. I am sure there are other ways of approaching this.
1 Perhaps for a different reason; i'll explain later in the post...

Categories