8

I have some unexpected behaviours running the same script from Bash and from within RStudio.

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"

and if I call it again will produce a different random number

>> Loading required package: RJSONIO
>> [1] 7.211103
>> [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

For some reason when I call the script from RStudio, the Python interpreter can't locate the module nltk (it seems to be the same with other pip installed modules) but has no problem importing random.

CptNemo
  • 6,455
  • 16
  • 58
  • 107
  • Are you using a virtualenv or any other complex install/library path setup? I see [this previous question](http://stackoverflow.com/questions/20337202/using-python-virtual-env-in-r)... – BrenBarn Jun 21 '14 at 23:25
  • No, as far as I know. – CptNemo Jun 21 '14 at 23:27
  • Does the behavior of `number` and `string` still happen even if you run the code in a fresh R session? – BrenBarn Jun 21 '14 at 23:40
  • @BrenBarn Good point if I restart the session I don't get any output, just errors. I updated my question – CptNemo Jun 21 '14 at 23:46
  • I can't really debug this since I don't have rPython (it doesn't work on Windows), but I'd suggest you try to narrow down the cause by playing around with a simple .py file. Try looking at `sys.path` to see if it's the same as what you get when running the program externally, etc. – BrenBarn Jun 21 '14 at 23:50
  • Yep, it seems that R from bash and R from Rstudio create different environments [here](http://stackoverflow.com/a/9212961/1707938), I think the different behaviour depends from this. Yet I don't get how to write a script that both is executed in bash and Rstudio. – CptNemo Jun 21 '14 at 23:57
  • I realise this was asked years ago but any solution on this? – sachinruk May 25 '16 at 04:53

1 Answers1

6

I had this problem, too. The issue was that my bash terminal seems to be calling a different python than the one Rstudio is. I also learned that if you're only trying to call Python.load() from rPython, you're probably better off with system() from the base R library.

  1. Figure out which python your bash terminal is calling. Go to your bash terminal and run which python. For me (OS X 10.11.5) it was /usr/local/bin/python. Now that we know the full path, we can call it explicitly and prevent R from choosing another version that might be installed in some corner of your machine.
  2. Use system() to call bash commands from R instead of python.load(), and use the full path to your script. Using your example script name, and my example python path, it would be system('/usr/local/bin/python /path/to/file/test.py1')

Hope that helps!

Kevin M Walsh
  • 61
  • 1
  • 3
  • How do you call the python you want explicitly using rPython? – Melanie Nov 30 '16 at 20:42
  • This question/answer should help: http://stackoverflow.com/questions/25383030/rpython-using-wrong-python-installation-on-mac-osx – Kevin M Walsh Dec 07 '16 at 00:17
  • I think your answer should have suggested a comparison of the bash result to the R result from: `system("python --version")`. Then describe how to resolve the discrepancies. – IRTFM Jun 09 '17 at 01:41