Created
June 4, 2014 21:14
-
-
Save mittenchops/99ed8bd99f51c66e4742 to your computer and use it in GitHub Desktop.
Testing rpy2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import rpy2 | |
import rpy2.robjects as robjects | |
from rpy2.robjects.packages import importr | |
from pprint import pprint | |
import requests | |
req = requests.get('http://www.google.com') | |
pprint(req) | |
# Here is how imports work. | |
stats = importr('stats') | |
base = importr('base') | |
amelia = importr('Amelia') # Make sure you have this installed. | |
# Here is how to grab a constant | |
pi = robjects.r['pi'] | |
print(pi[0]) | |
# Or a whole data frame | |
dat = robjects.r['iris'] | |
# Here is how you take an R function and put it in python | |
rhead = robjects.r['head'] | |
rhead2 = lambda x : map(rhead,x) | |
print(rhead(dat)[0]) | |
pprint(rhead2(dat)) | |
# Here's another R function used here: | |
rnorm = stats.rnorm | |
print(rnorm(100)) | |
# So important: | |
summary = robjects.r['summary'] | |
print(summary(rnorm(100))) | |
print(summary(dat)) # wow! It works on the object I brought in! | |
from datetime import datetime | |
import pandas.rpy.common as com | |
# generate some fake tick data with 1 million observations | |
n = 10 | |
df = pd.DataFrame({ | |
"timestamp": [datetime.now() for t in range(n)], | |
"value": np.random.uniform(-1, 1, n) | |
}) | |
# similar dataframe operations to R | |
df.head() | |
df.describe() | |
df.count() | |
r_dataframe = com.convert_to_r_dataframe(df) | |
print(r_dataframe) | |
# GRAPHICS DEMO | |
from rpy2 import robjects | |
from rpy2.robjects import Formula, Environment | |
from rpy2.robjects.vectors import IntVector, FloatVector | |
from rpy2.robjects.lib import grid | |
from rpy2.robjects.packages import importr, data | |
from rpy2.rinterface import RRuntimeError | |
import warnings | |
# The R 'print' function | |
rprint = robjects.globalenv.get("print") | |
#stats = importr('stats') | |
grdevices = importr('grDevices') | |
#base = importr('base') | |
datasets = importr('datasets') | |
grid.activate() | |
lattice = importr('lattice') | |
xyplot = lattice.xyplot | |
datasets = importr('datasets') | |
mtcars = data(datasets).fetch('mtcars')['mtcars'] | |
formula = Formula('mpg ~ wt') | |
formula.getenvironment()['mpg'] = mtcars.rx2('mpg') | |
formula.getenvironment()['wt'] = mtcars.rx2('wt') | |
p = lattice.xyplot(formula) | |
#rprint(p) | |
# GRAPHICS GGPLOT | |
import math, datetime | |
import rpy2.robjects.lib.ggplot2 as ggplot2 | |
import rpy2.robjects as ro | |
from rpy2.robjects.packages import importr | |
#base = importr('base') | |
mtcars = data(datasets).fetch('mtcars')['mtcars'] | |
rnorm = stats.rnorm | |
dataf_rnorm = robjects.DataFrame({'value': rnorm(300, mean=0) + rnorm(100, mean=3), | |
'other_value': rnorm(300, mean=0) + rnorm(100, mean=3), | |
'mean': IntVector([0, ]*300 + [3, ] * 100)}) | |
gp = ggplot2.ggplot(mtcars) | |
pp = gp + \ | |
ggplot2.aes_string(x='wt', y='mpg') + \ | |
ggplot2.geom_point() + \ | |
ggplot2.theme_bw() | |
pp.plot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment