rstudio doesn't load all python modules via rpython call

  • Last Update :
  • Techknowledgy :

Please consider the following. I have a folder "~/rpython" containing two scripts:

# test1.R

library(rPython)

setwd("~/rpython")

python.load("test1.py")

number < -python.get("number")
string < -python.get("string")

print(sqrt(number))
print(string)

and

# test1.py

import random, nltk

number = random.randint(1, 1000)

string = nltk.word_tokenize('home sweet home')

I can call my R script from Bash with Rscript test1.R, which returns as expected

>> Loading required package: RJSONIO >>
   [1] 13.0384 >>
   [1]
"home"
"sweet"
"home"

But when I run the very same script (test1.R) from RStudio things get weird. Here the output

# test1.R
>
> library(rPython)
Loading required package: RJSONIO
>
> setwd("~/rpython")
>
> python.load("test1.py")
Error in python.exec(code, get.exception) : No module named nltk
>
> number <- python.get("number") Traceback (most recent call last): File "<string>" , line 1, in <module>
   NameError: name 'number' is not defined
   Error in python.get("number") : Variable not found
   > string <- python.get("string") Traceback (most recent call last): File "<string>" , line 1, in <module>
      NameError: name 'string' is not defined
      Error in python.get("string") : Variable not found
      >
      > print(sqrt(number))
      Error in print(sqrt(number)) : object 'number' not found
      > print(string)
      Error in print(string) : object 'string' not found

Suggestion : 2

Posted on January 13, 2014 by bryan in R bloggers | 0 Comments

First create a Python script that imports the praw module and does the first data call:

import praw

# Set the user agent information
# IMPORTANT: Change this
if you borrow this code.Reddit has very strong
# guidelines about how to report user agent information
r = praw.Reddit('Check New Articles script based on code by ProgrammingR.com')

# Create a(lazy) generator that will get the data when we call it below
new_subs = r.get_new(limit = 100)

# Get the data and put it into a usable format
new_subs = [str(x) for x in new_subs]

Since the Python session is persistent, we can also create a shorter Python script that we can use to fetch updated data without reimporting the praw module

# Create a(lazy) generator that will get the data when we call it below
new_subs = r.get_new(limit = 100)

# Get the data and create a list of strings
new_subs = [str(x) for x in new_subs]

Finally, some R code that calls the Python script and gets the data from the Python variables we create:

library(rPython)

# Load / run the main Python script
python.load("GetNewRedditSubmissions.py")

# Get the variable
new_subs_data < -python.get("new_subs")

# Load / run re - fecth script
python.load("RefreshNewSubs.py")

# Get the updated variable
new_subs_data < -python.get("new_subs")

head(new_subs_data)

Suggestion : 3

Any Python package you install from PyPI or Conda can be used from R with reticulate.,When installing Python packages it’s typically a good practice to isolate them within a Python environment (a named Python installation that exists for a specific project or purpose). This provides a measure of isolation, so that updating a Python package for one project doesn’t impact other projects.,Each version of Python on your system has its own set of packages and reticulate will automatically find a version of Python that contains the first package that you import from R. If need be you can also configure reticulate to use a specific version of Python.,The reticulate package includes a py_install() function that can be used to install one or more Python packages. The packages will be by default be installed within a virtualenv or Conda environment named “r-reticulate”. For example:

library(reticulate)
py_install("pandas")
library(reticulate)

# create a new environment
conda_create("r-reticulate")

# install SciPy
conda_install("r-reticulate", "scipy")

#
import SciPy(it will be automatically discovered in "r-reticulate")
scipy < -import("scipy")
library(reticulate)

# indicate that we want to use a specific condaenv
use_condaenv("r-reticulate")

#
import SciPy(will use "r-reticulate"
   as per call to use_condaenv)
scipy < -import("scipy")
library(reticulate)

# create a new environment
virtualenv_create("r-reticulate")

# install SciPy
virtualenv_install("r-reticulate", "scipy")

#
import SciPy(it will be automatically discovered in "r-reticulate")
scipy < -import("scipy")
library(reticulate)

# indicate that we want to use a specific virtualenv
use_virtualenv("r-reticulate")

#
import SciPy(will use "r-reticulate"
   as per call to use_virtualenv)
scipy < -import("scipy")
# install into system level Python
$ sudo pip install SciPy

# install into active Conda environment
$ conda install SciPy

Suggestion : 4

The reticulate package provides an R interface to Python modules, classes, and functions. For example, this code imports the Python os module and calls some functions within it:,You can install any required Python packages using standard shell tools like pip and conda. Alternately, reticulate includes a set of functions for managing and installing packages within virtualenvs and Conda environments. See the article on Installing Python Packages for additional details.,The main module is generally useful if you have executed Python code from a file or string and want to get access to its results (see the section below for more details).,Whereas if you are calling a method in Python via reticulate that takes an index you would write this to address the first item:

library(reticulate)
os < -import("os")
os$listdir(".")
1._
library(reticulate)
os < -import("os")
os$listdir(".")
2._
 [1]
 ".git"
 ".gitignore"
 ".Rbuildignore"
 ".RData" [5]
 ".Rhistory"
 ".Rproj.user"
 ".travis.yml"
 "appveyor.yml" [9]
 "DESCRIPTION"
 "docs"
 "external"
 "index.html" [13]
 "index.Rmd"
 "inst"
 "issues"
 "LICENSE" [17]
 "man"
 "NAMESPACE"
 "NEWS.md"
 "pkgdown" [21]
 "R"
 "README.md"
 "reticulate.Rproj"
 "src" [25]
 "tests"
 "vignettes"
library(reticulate)
use_python("/usr/local/bin/python")
library(reticulate)
use_virtualenv("myenv")
difflib < -import("difflib")
difflib$ndiff(foo, bar)

filecmp < -import("filecmp")
filecmp$cmp(dir1, dir2)
main < -import_main()

builtins < -import_builtins()
builtins$print('foo')