In [4]:
import os
import sys
import io
In [2]:
os.environ["tmp_Rdirectory"]= "R"
os.environ["R_HOME"] = os.environ["WINPYDIR"]+ "\\..\\tools\\R\\"
os.environ["R_HOMEbin"]=os.environ["R_HOME"] + "bin"
# for installation we need this
os.environ["tmp_Rbase"]=os.path.join(os.path.split(os.environ["WINPYDIR"])[0] , 'tools','R' )
In [7]:
# downloading R-3.1.2 (55Mo, may take 2/3 minutes)
try:
import urllib.request as urllib2 # Python 3
except:
import urllib2 # Python 2
if 'amd64' in sys.version.lower():
r_comp ='/COMPONENTS="main,x64,translations'
r_binary="R-3.1.2-win.exe"
r_url="http://cran.r-project.org/bin/windows/base/R-3.1.2-win.exe"
hashes=("9e3c0cd6311355e0d5f8e1085b288361", "954499b243aef3856ca66132311dfd5e70d9f7e4" )
else:
r_comp ='/COMPONENTS="main,i386,translations'
r_binary="R-3.1.2-win.exe"
r_url="http://cran.r-project.org/bin/windows/base/R-3.1.2-win.exe"
hashes=("9e3c0cd6311355e0d5f8e1085b288361", "954499b243aef3856ca66132311dfd5e70d9f7e4" )
r_installer=os.environ["WINPYDIR"]+"\\..\\tools\\"+r_binary
os.environ["r_installer"]=r_installer
g = urllib2.urlopen(r_url)
with io.open(r_installer, 'wb') as f:
f.write(g.read())
g.close
g = None
In [8]:
#checking it's there
!dir %r_installer%
In [9]:
# checking it's the official R-3.1.2
import hashlib
def give_hash(of_file, with_this):
with io.open(r_installer, 'rb') as f:
return with_this(f.read()).hexdigest()
print (" "*12+"MD5"+" "*(32-12-3)+" "+" "*15+"SHA-1"+" "*(40-15-5)+"\n"+"-"*32+" "+"-"*40)
print ("%s %s %s" % (give_hash(r_installer, hashlib.md5) , give_hash(r_installer, hashlib.sha1),r_installer))
assert give_hash(r_installer, hashlib.md5) == hashes[0]
assert give_hash(r_installer, hashlib.sha1) == hashes[1]
In [10]:
# let's install it
# If you are "USB life style", or multi-winpython
# ==> CLICK the OPTION "Don't create a StartMenuFolder' <== (when it will show up)
os.environ["tmp_R_comp"]=r_comp
!start cmd /C %r_installer% /DIR=%tmp_Rbase% %tmp_R_comp%
In [5]:
# let's create a Ipython with "%load_ext rmagic" launcher
bat_text = r"""
@echo off
call %~dp0env.bat
set R_HOME=%WINPYDIR%\..\tools\R\
set R_HOMEbin=%WINPYDIR%\..\tools\R\bin
set PATH=%PATH%;%R_HOMEbin%
Ipython notebook --matplotlib=inline
"""
ipython_with_R_launcher_bat=os.environ["WINPYDIR"]+"\\..\\scripts\\ipython_with_R_launcher.bat"
if sys.version_info[0] == 3:
with io.open(ipython_with_R_launcher_bat, 'w', encoding = sys.getdefaultencoding() ) as f:
for line in bat_text.splitlines():
f.write('%s\n' % line )
else:
with io.open(ipython_with_R_launcher_bat, 'wb' ) as f:
for line in bat_text.splitlines():
f.write('%s\r\n' % line.encode(sys.getdefaultencoding()) )
Click on your usual "ipython notebook icon"
Click on the.bat you have just created winpython-*\scripts\ipython_with_R_launcher.bat
or launch the option below which will start a new Ipython Notebook server
In [7]:
# Nota : this not needed for winpython versions after 2014-12-29 !
!start cmd /C %WINPYDIR%\\..\\scripts\\ipython_with_R_launcher.bat
And now, you should see a IPython notebook starting next to this IPython Notebook
Check the start of this example http://nbviewer.ipython.org/github/winpython/winpython_afterdoc/blob/master/docs/FlavorR/dplyr_pandas.ipynb
or below 2 lines
%load_ext rpy2.ipython
%R install.packages('dplyr')
In [ ]: