Python Reticulate not working in Rstudio Cloud - python

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...

Related

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.

How to use raster package in python with rpy2

Working with R in Python using rpy2 on windows 7.
I need to open some rasters as RasterLayer using the function raster() from the raster package. I manage to install the package, but not to use its function.
I install the packages that I need (rgdal, sp, raster, lidR, io) using
utils.install_packages(StrVector(names_to_install))
names_to_install is a list of the packages that are still not installed. This works fine.
I know how to try the "basic" functions, like sum, and it works:
import rpy2.robjects as robjects
function_sum = robjects.r['sum']
But the same doesn't seem to work with the raster function from the raster package:
function_raster = robjects.r['raster']
since I get the error:
LookupError: 'raster' not found
I also tried the following:
raster_package = importr('raster')
with the intention to be able to run the next and load my raster file:
raster_package.raster(my_raster_file)
but the first line (import('raster')) causes the crash of python and I get the error:
Process finished with exit code -1073741819 (0xC0000005)
This doesn't happen with other loaded packages like rgdal, but with the raster package and with the lidR package I get the error.
I looked up this error, seems to be access violation, but I don't know what I can do about it and why it only happens with certain packages.
I expect to be able to call the raster function from the package raster.
Edit
I tried it on a computer with windows 10 and the error doesn't show anymore when running
raster_package = importr('raster')
Still would be nice to know what is the problem with Windows 7 and if there is any solution.
rpy2 does not currently have Windows support. This is not a final situation, most of what is likely needed is contributions to finalize this: https://github.com/rpy2/rpy2/blob/master/rpy2/rinterface_lib/embedded_mswin.py

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:

MLWIC: Machine Learning for Wildlife Image Classification in R Issues with Python

I am a wildlife PhD researcher manually identifying ~1.5 million game camera photos by species. A machine learning package in R has recently come out of a research project and I've been trying to get the script to run in R for about 12 hours and can't seem to get it right (I have used R and python a lot, but I am no expert and this is the first question I have asked on here so forgive me if I haven't done this correctly).
The ReadMe (To understand what I am trying to do you will probably have to read this, I apologize) for the package downloaded on Github is located at: https://github.com/mikeyEcology/MLWIC/blob/master/README.md
Unfortunately for me, the package was developed on a Macintosh platform and I have Windows.
I followed the steps in the ReadMe as follows:
1: Installed the MLWIC package using the code:
devtools::install_github("mikeyEcology/MLWIC")
library(MLWIC)
2: Followed the instructions to install "pip", python, and "TensorFlow" at
https://www.tensorflow.org/install/pip
3: Downloaded the L1 folder
4: I ran a different code than outlined in the ReadMe, it is as follows:
setup(python_loc = "I used this location I got from running "where python" in Anaconda")
After this initial setup, I ran the code for the "classify function":
library(MLWIC)
setup(python_loc = "C:/ProgramData/Anaconda3", conda_loc = "auto", r_reticulate = FALSE)
setwd("C:/Users/werdel/Desktop/MachineLearning")
help("classify")
classify(path_prefix = "C:/Users/werdel/Desktop/MachineLearning/images",# this is the absolute path to the images.
data_info = "C:/Users/werdel/Desktop/MachineLearning/image_labels.csv", # this is the location of the csv containing image information. It has Unix linebreaks and no headers.
model_dir = "C:/Users/werdel/Desktop/MachineLearning", # assuming this is where you stored the L1 folder in Step 3 of the instructions: github.com/mikeyEcology/MLWIC/blob/master/README
python_loc = "C:/ProgramData/Anaconda3/python.exe", # the location of Python on your computer.
save_predictions = "model_predictions.txt" # this is the default and you should use it unless you have reason otherwise.)
This is where the problem seemed to arise. It seems to run fine, with the output showing a file created in my working directory, but when I check, there is no file. I have tried changing python location, downloading new and old versions of anaconda, messing with environments, but nothing has changed the fact that there is no file created in my working directory:
> library(MLWIC)
> setup(python_loc = "C:/ProgramData/Anaconda3", conda_loc = "auto", r_reticulate = FALSE)
Remove all packages in environment C:\PROGRA~3\ANACON~1\envs\r-reticulate:
## Package Plan ##
environment location: C:\PROGRA~3\ANACON~1\envs\r-reticulate
The following packages will be REMOVED:
ca-certificates: 2018.03.07-0
certifi: 2018.10.15-py37_0
openssl: 1.1.1a-he774522_0
pip: 18.1-py37_0
python: 3.7.1-he44a216_5
setuptools: 40.6.2-py37_0
vc: 14.1-h0510ff6_4
vs2015_runtime: 14.15.26706-h3a45250_0
wheel: 0.32.3-py37_0
wincertstore: 0.2-py37_0
Solving environment: ...working... done
## Package Plan ##
environment location: C:\PROGRA~3\ANACON~1\envs\r-reticulate
added / updated specs:
- python
The following NEW packages will be INSTALLED:
ca-certificates: 2018.03.07-0
certifi: 2018.10.15-py37_0
openssl: 1.1.1a-he774522_0
pip: 18.1-py37_0
python: 3.7.1-he44a216_5
setuptools: 40.6.2-py37_0
vc: 14.1-h0510ff6_4
vs2015_runtime: 14.15.26706-h3a45250_0
wheel: 0.32.3-py37_0
wincertstore: 0.2-py37_0
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
#
# To activate this environment, use:
# > activate r-reticulate
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#
Solving environment: ...working... failed
UnsatisfiableError: The following specifications were found to be in conflict:
- argparse
- tensorflow
Use "conda info <package>" to see the dependencies for each package.
Error: Error 1 occurred installing packages into conda environment r-reticulate
> classify(path_prefix = "C:/Users/werdel/Desktop/MachineLearning/images", # this is
the absolute path to the images.
+ data_info = "C:/Users/werdel/Desktop/MachineLearning/image_labels.csv", #
this is the location of the csv containing image information. It has Unix linebreaks
and no headers.
+ model_dir = "C:/Users/werdel/Desktop/MachineLearning", # assuming this is
where you stored the L1 folder in Step 3 of the instructions:
github.com/mikeyEcology/MLWIC/blob/master/README
+ python_loc = "C:/ProgramData/Anaconda3/python.exe", # the location of Python
on your computer.
+ save_predictions = "model_predictions.txt" # this is the default and you
should use it unless you have reason otherwise.
+ )
[1] "evaluation of images took 0.000504970550537109 secs. The results are stored in
C:/Users/werdel/Desktop/MachineLearning/L1/model_predictions.txt. To view the results
in a viewer-friendly format, please use the function make_output"
So my final question is, does it seem like I set something up wrong while downloading pip, tensorflow, anaconda, and python, is it something with the way I am coding, etc.?
If I am not mistaken there is a small bug in their code that ignores the "data_info" path. Try renaming your "image_labels.csv" to "data_info.csv" and put the file inside the model_dir. This solved the problem for me. Also, use "C:/ProgramData/Anaconda3/" instead of "C:/ProgramData/Anaconda3/python.exe"
I was able to get MLWIC to run relatively pain-free on Mac. Getting it running on windows was a little more frustrating. I followed issue threads #6 and #13 on the MLWIC github page. On a machine running Windows 10 I downloaded Anaconda 3 5.2.0 which includes python 3.6.5. After many hours of troubleshooting I got it to work.
Once Anaconda is installed I used Anaconda Navigator to install tensorflow 1.11.0, cuddn 6.0, setuptools 40.6.2. Note this is the only place that I installed tensorflow. I did not bother with the pip install as mentioned on the tensorflow web page.
From there when first opening R I installed the following packages MLWIC, keras, cloudml, Rcpp, and reticulate. Then ran them with the library function. Also as Faisal Ahmed stated above rename image_labels.csv to data_info.csv, and move that file to the L1 folder. From here I was able to get the MLWIC provided code to run on windows. When I first ran the setup function it looked like this setup(python_loc = "C:/Users/kvanatta/Anaconda3/python")
Note: I was originally trying to get MLWIC to run with the files saved on the server in my office (S: drive) and could not get it to work. Upon transferring the files to a 1tb external hard drive (F: drive) everything worked fine from the new location.
I also downloaded the program notepad ++ to be able to convert the data_info.csv file to unix(lf) linebreaks. I believe since windows does not use these linebreaks, opening the .csv file in excel will change the line breaks. You can use notepad ++ to change them back.
Hope this helps ease some frustrations.
Cheers
I really struggled to get MLWIC working on my Windows computer and followed a number of the issues on the MLWIC github page until I came up with a solution that worked for me.
I wrote up a guide for windows users and it's been added to the MLWIC_examples repo. You can find it here. It is a step-by-step guide.
As as aside, I found a way to use Unix-type linebreaks without leaving the R environment:
# Write to UNIX type doc (required)
output.file <- file("L1\\data_info.csv", "wb")
write.table(data_info,
row.names = FALSE,
col.names = FALSE,
file = output.file,
quote = FALSE,
append = TRUE,
sep = ",")
close(output.file)
rm(output.file)

Problem with calling an R package in Python

For context: My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3). Right now I am trying to make it work in Windows.
I believe I have successfully called the R interpreter using python to run my R file (run.R). In this file I call the R package 'seqinr'. However when I call the R interpreter from python I obtain this:
I run this:
os.system('C:/"Program Files"/R/R-3.6.1/bin/Rscript run.R')
and I obtain this:
Error in library(seqinr) : there is no package called 'seqinr'
Calls: source -> withVisible -> eval -> eval -> library
Execution halted
However, when I run the 'C:/"Program Files"/R/R-3.6.1/bin/Rscript run.R' command in the Command Prompt, it works perfectly and I have no problems.
I also checked where my package is installed and it's in the default directory that R uses to install all the packages. I also do not have any other R versions installed in my machine.
I have no clue what is happening now, so I would appreciate any help.
Thank you!
Although probably not satisfactory, I'd recommend changing your "run.R" file to include the following lines:
if (!("seqinr" %in% installed.packages())) {
install.packages('seqinr', repos = "https://cloud.r-project.org")
}
library(seqinr)
This will install the package if it isn't available to wherever R is looking for packages when being called from Python. Additionally, you could include print(rownames(installed.packages())) in your "run.R" file to see what packages are currently available to your R installation at run time when calling from Python. Inclusion of the line print(.libPaths()) will tell you where R is looking for installed packages at run time as well.

Categories