Ho alcuni comportamenti imprevisti che eseguono lo stesso script da Bash e da RStudio.RStudio non carica tutti i moduli Python tramite la chiamata rPython
Si prega di considerare quanto segue. Ho una cartella "~/rpython"
contenente due script:
# test1.R
library(rPython)
setwd("~/rpython")
python.load("test1.py")
number <- python.get("number")
string <- python.get("string")
print(sqrt(number))
print(string)
e
# test1.py
import random, nltk
number = random.randint(1, 1000)
string = nltk.word_tokenize('home sweet home')
posso chiamare il mio script R da Bash con Rscript test1.R
, che restituisce come previsto
>> Loading required package: RJSONIO
>> [1] 13.0384
>> [1] "home" "sweet" "home"
e se chiamo di nuovo produrrà un numero casuale diverso
>> Loading required package: RJSONIO
>> [1] 7.211103
>> [1] "home" "sweet" "home"
Ma quando eseguo lo stesso script (test1.R
) da RStudio le cose si fanno strane. Qui l'uscita
# 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
Per qualche ragione quando chiamo lo script da RStudio, l'interprete Python non può individuare il modulo nltk
(che sembra essere lo stesso con altri pip
moduli installati), ma non ha alcun problema importando random
.
Si sta utilizzando un virtualenv o qualsiasi altra installazione complessa del percorso di installazione/libreria? Vedo [questa domanda precedente] (http://stackoverflow.com/questions/20337202/using-python-virtual-env-in-r) ... – BrenBarn
No, per quanto ne so. – CptNemo
Il comportamento di 'number' e' string' si verifica ancora anche se si esegue il codice in una nuova sessione R? – BrenBarn