In [1]:
%load_ext rpy2.ipython

problem rpy2 #224 : maybe a R to python3 unicode/string problem ?


In [2]:
import numpy as np
import pylab
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
%Rpush X Y
%R lm(Y~X)$coef
%R d=resid(lm(Y~X)); e=coef(lm(Y~X))
%R -o d -o e
%Rpull e
print (e)


Unable to unlink tempfile C:\Users\famille\AppData\Local\Temp\tmpslq1uzx7
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-9bcc6f250235> in <module>()
      8 get_ipython().magic('R -o d -o e')
      9 get_ipython().magic('Rpull e')
---> 10 print (e)

D:\result_tests\WinPython-32bit-3.4.2.1_build15\python-3.4.2rc1\lib\site-packages\rpy2\robjects\robject.py in __str__(self)
     46                 if os.path.exists(tfname):
     47                     print('Unable to unlink tempfile %s' % tfname)
---> 48             s = str.join(os.linesep, s)
     49         else:
     50             rpy2.rinterface.set_writeconsole(writeconsole)

TypeError: sequence item 0: expected str instance, bytes found

problem rpy2 #223 : only last plot is shown (but they are ok if launched one by one)


In [3]:
%%R
# ggplot2 examples
library(ggplot2)

# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
   labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1),
   labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
   labels=c("4cyl","6cyl","8cyl"))

# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
   main="Distribution of Gas Milage", xlab="Miles Per Gallon",
   ylab="Density")

# Scatterplot of mpg vs. hp for each combination of gears and cylinders
# in each facet, transmittion type is represented by shape and color
qplot(hp, mpg, data=mtcars, shape=am, color=am,
   facets=gear~cyl, size=I(3),
   xlab="Horsepower", ylab="Miles per Gallon")

# Separate regressions of mpg on weight for each number of cylinders
qplot(wt, mpg, data=mtcars, geom=c("point", "smooth"),
   method="lm", formula=y~x, color=cyl,
   main="Regression of MPG on Weight",
   xlab="Weight", ylab="Miles per Gallon")

# Boxplots of mpg by number of gears
# observations (points) are overlayed and jittered
qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"),
   fill=gear, main="Mileage by Gear Number",
   xlab="", ylab="Miles per Gallon")



In [4]:
%%R
# Separate regressions of mpg on weight for each number of cylinders
qplot(wt, mpg, data=mtcars, geom=c("point", "smooth"),
   method="lm", formula=y~x, color=cyl,
   main="Regression of MPG on Weight",
   xlab="Weight", ylab="Miles per Gallon")



In [5]:
%%R

# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
   main="Distribution of Gas Milage", xlab="Miles Per Gallon",
   ylab="Density")


problem rpy2 #212 bis : with %%R the head(hgflights) output goes to the DOS (stderr?) window instead of the ipython cell


In [6]:
%%R
library(hflights)
dim(hflights)
head(hflights)

In [7]:
%R head(hflights)


Out[7]:
Year Month DayofMonth DayOfWeek DepTime ArrTime UniqueCarrier FlightNum TailNum ActualElapsedTime ... ArrDelay DepDelay Origin Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted
0 2011 1 1 6 1400 1500 AA 428 N576AA 60 ... -10 0 IAH DFW 224 7 13 0 0
1 2011 1 2 7 1401 1501 AA 428 N557AA 60 ... -9 1 IAH DFW 224 6 9 0 0
2 2011 1 3 1 1352 1502 AA 428 N541AA 70 ... -8 -8 IAH DFW 224 5 17 0 0
3 2011 1 4 2 1403 1513 AA 428 N403AA 70 ... 3 3 IAH DFW 224 9 22 0 0
4 2011 1 5 3 1405 1507 AA 428 N492AA 62 ... -3 5 IAH DFW 224 9 9 0 0
5 2011 1 6 4 1359 1503 AA 428 N262AA 64 ... -7 -1 IAH DFW 224 6 13 0 0

6 rows × 21 columns


In [7]: