-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_rPythonExample.r
40 lines (33 loc) · 1.69 KB
/
simple_rPythonExample.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# install.packages("rPython", repos = "http://www.datanalytics.com/R")
# if this doesn't work, go to cran site and download an older version as tar.gz and install it through rstudio
# from local drive
# ... this will install RJSONIO if it hasn't been installed already
# as JSON is the facilitator of communication between R & Python
library(rPython)
# simple example
x <- python.get("1+2")
# another simple example, creating a function, notice the escapes
python.exec("def happyBirthdayToYou(): print(\"happy birthday to you!\")")
python.exec("happyBirthdayToYou()")
# unfortunately, rPython does not work on Wintel systems. However, despair not, you can escape to the OS with system()
##system('python -c "def happyBirthdayToYou(): print(\"happy birthday to you!\")"')
##system('python -c "happyBirthdayToYou()"')
#### NOT WORKING!!! OUTTA LUCK w\ Windows!
# use python.exec when you only need to execute or evaluate an expression within Python
# use python.get to retrieve an object post-execution
# now for a more advanced example
python.exec("import nltk")
python.exec("import nltk.corpus")
python.exec("from nltk.corpus import PlaintextCorpusReader")
python.exec("from urllib import urlopen")
python.exec("import numpy")
python.exec("url = '/amanuensis/data/raw_data/corpora/fedchairs/gReenspanCorpus/greenspan_19990506.txt'")
python.exec("raw = open(url).read()")
python.exec("ttt = nltk.tokenize.TextTilingTokenizer(w=7, k=3, smoothing_width=6, smoothing_rounds=10)")
python.exec("tiles = ttt.tokenize(raw)")
python.exec("print(tiles[1])")
firstTextTile <- python.get("tiles[1]")
firstTextTile
#text <- paste("espeak -v+whisper -k20 -p 79 -s 225 \"", firstTextTile, "\"", sep="")
#text
#system(text)