-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl_py2r.R
More file actions
52 lines (38 loc) · 1.18 KB
/
Copy pathrepl_py2r.R
File metadata and controls
52 lines (38 loc) · 1.18 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library(reticulate)
#library(assertthat)
# can install library assertthat for testing
# shell commands
# pip install pandas
# opens up python REPL in R with Reticulate
# https://rstudio.github.io/reticulate/reference/repl_python.html
repl_python()
import pandas as pd
# create a dictionary in Python
fruit = dict({"first fruit" : "apple", "second fruit " : "pear", "third fruit" : "pineapple"})
# referencing this stack overflow post
# https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe
d = {u'2012-06-08': 388,
u'2012-06-09': 388,
u'2012-06-10': 388,
u'2012-06-11': 389,
u'2012-06-12': 389,
u'2012-06-13': 389,
u'2012-06-14': 389,
u'2012-06-15': 389,
u'2012-06-16': 389,
u'2012-06-17': 389,
u'2012-06-18': 390,
u'2012-06-19': 390,
u'2012-06-20': 390}
data = pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
exit
# you can call python objects after exiting by
# py$[OBJECT SAVED IN PY REPL ]
# calling python object in R
# converts to list
# need to figure order of lists in python aren't the same (bug?)
fruit.list <- py$fruit
fruit.list[["first fruit"]]
date <- py$data
date
#assertthat::are_equal(str(date), str(py$data))