{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Installating R on WinPython (version of 2019-08-25)\n", "\n", "### Warning: as of 2019-08-25, the R installation is not supposed to support a move of Winpython library\n", "\n", "see https://richpauloo.github.io/2018-05-16-Installing-the-R-kernel-in-Jupyter-Lab/\n", "\n", "#### This procedure applys for Winpython (Version of December 2015 and after) \n", "### 1 - Downloading R binary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import io\n", "\n", "# downloading R may takes a few minutes (80Mo)\n", "try:\n", " import urllib.request as urllib2 # Python 3\n", "except:\n", " import urllib2 # Python 2\n", "\n", "# specify R binary and (md5, sha1) hash\n", "# R-3.6.1:\n", "r_url = \"https://cran.r-project.org/bin/windows/base/old/3.6.1/R-3.6.1-win.exe\"\n", "hashes=(\"f6ca2ecfc66a10a196991b6b6c4e91f6\",\"df4ad3c36e193423ebf2d698186feded15777da1\")\n", "\n", "# specify target location\n", "# tweak change in recent winpython\n", "tool_base_directory=os.environ[\"WINPYDIR\"]+\"\\\\..\\\\t\\\\\"\n", "if not os.path.isdir(tool_base_directory):\n", " tool_base_directory=os.environ[\"WINPYDIR\"]+\"\\\\..\\\\tools\\\\\"\n", "\n", "\n", "\n", "\n", "r_installer = tool_base_directory+os.path.basename(r_url)\n", "os.environ[\"r_installer\"] = r_installer\n", "\n", "# Download\n", "g = urllib2.urlopen(r_url) \n", "with io.open(r_installer, 'wb') as f:\n", " f.write(g.read())\n", "g.close\n", "g = None\n", "\n", "#checking it's there\n", "!dir %r_installer%" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### 2 - checking and Installing R binary in the right place" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# checking it's the official R\n", "import hashlib\n", "def give_hash(of_file, with_this):\n", " with io.open(r_installer, 'rb') as f:\n", " return with_this(f.read()).hexdigest() \n", "print (\" \"*12+\"MD5\"+\" \"*(32-12-3)+\" \"+\" \"*15+\"SHA-1\"+\" \"*(40-15-5)+\"\\n\"+\"-\"*32+\" \"+\"-\"*40)\n", "print (\"%s %s %s\" % (give_hash(r_installer, hashlib.md5) , give_hash(r_installer, hashlib.sha1),r_installer))\n", "if give_hash(r_installer, hashlib.md5) == hashes[0] and give_hash(r_installer, hashlib.sha1) == hashes[1]:\n", " print(\"looks good!\")\n", "else:\n", " print(\"problem ! please check\")\n", " assert give_hash(r_installer, hashlib.md5) == hashes[0]\n", " assert give_hash(r_installer, hashlib.sha1) == hashes[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# preparing Dos variables\n", "os.environ[\"R_HOME\"] = tool_base_directory+ \"R\\\\\" \n", "os.environ[\"R_HOMEbin\"]=os.environ[\"R_HOME\"] + \"bin\" \n", "\n", "# for installation we need this\n", "os.environ[\"tmp_Rbase\"]=os.path.join(os.path.split(os.environ[\"WINPYDIR\"])[0] , 't','R' ) \n", "if 'amd64' in sys.version.lower():\n", " r_comp ='/COMPONENTS=\"main,x64,translations'\n", "else:\n", " r_comp ='/COMPONENTS=\"main,i386,translations'\n", "os.environ[\"tmp_R_comp\"]=r_comp\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# let's install it, if hashes do match\n", "assert give_hash(r_installer, hashlib.md5) == hashes[0]\n", "assert give_hash(r_installer, hashlib.sha1) == hashes[1]\n", "# If you are \"USB life style\", or multi-winpython\n", "# ==> CLICK the OPTION \"Don't create a StartMenuFolder' <== (when it will show up)\n", "\n", "!start cmd /C %r_installer% /DIR=%tmp_Rbase% %tmp_R_comp%" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## During Installation (if you wan't to move the R installation after)\n", "\n", "Choose non default option \"Yes (customized startup\"\n", "\n", "then after 3 screens, Select \"Don't create a Start Menu Folder\"\n", "\n", "Un-select \"Create a desktop icon\"\n", "\n", "Un-select \"Save version number in registery\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3 - create a R_launcher and install irkernel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import io\n", "# let's create a R launcher \n", "r_launcher = r\"\"\"\n", "@echo off\n", "call %~dp0env.bat\n", "rscript %*\n", "\"\"\"\n", "r_launcher_bat = os.environ[\"WINPYDIR\"]+\"\\\\..\\\\scripts\\\\R_launcher.bat\"\n", "\n", "# let's create a R init script\n", "# in manual command line, you can use repos = c('http://irkernel.github.io/', getOption('repos'))\n", "r_initialization = r\"\"\"\n", "install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest', 'stringr'), repos = c('http://cran.rstudio.com/', 'http://cran.rstudio.com/'))\n", "devtools::install_github('IRkernel/IRkernel')\n", "library('pbdZMQ')\n", "library('repr')\n", "library('IRkernel')\n", "library('IRdisplay')\n", "library('crayon')\n", "library('stringr')\n", "IRkernel::installspec()\n", "\"\"\"\n", "# IRkernel::installspec() # install for the current user: \n", "# IRkernel::installspec(user = FALSE) # install system-wide\n", "r_initialization_r = os.path.normpath(os.environ[\"WINPYDIR\"]+\"\\\\..\\\\scripts\\\\R_initialization.r\")\n", "\n", "\n", "for i in [(r_launcher,r_launcher_bat), (r_initialization, r_initialization_r)]:\n", " with io.open(i[1], 'w', encoding = sys.getdefaultencoding() ) as f:\n", " for line in i[0].splitlines():\n", " f.write('%s\\n' % line )\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#check what we are going to do \n", "print (\"!start cmd /C %WINPYDIR%\\\\..\\\\scripts\\\\R_launcher.bat --no-restore --no-save \" + r_initialization_r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Launch Rkernel setup\n", "os.environ[\"r_initialization_r\"] = r_initialization_r\n", "!start cmd /C %WINPYDIR%\\\\..\\\\scripts\\\\R_launcher.bat --no-restore --no-save %r_initialization_r% " ] }, { "cell_type": "raw", "metadata": {}, "source": [ "#2019-08-26: this cell is no more valid, so we don't know how to make it movable\n", "# make RKernel a movable installation with the rest of WinPython \n", "from winpython import utils\n", "base_winpython = os.path.dirname(os.path.normpath(os.environ[\"WINPYDIR\"]))\n", "rkernel_json=(base_winpython+\"\\\\settings\\\\kernels\\\\ir\\\\kernel.json\")\n", "\n", "# so we get \"argv\": [\"{prefix}/../tools/R/bin/x64/R\"\n", "utils.patch_sourcefile(rkernel_json, base_winpython.replace(\"\\\\\",\"/\"), r'{prefix}/..', silent_mode=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4- Install a R package via a IPython Kernel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext rpy2.ipython\n", "\n", "#vitals: 'dplyr', 'R.utils', 'nycflights13'\n", "# installation takes 2 minutes\n", "%R install.packages(c('dplyr','R.utils', 'nycflights13'), repos='http://cran.rstudio.com/') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5- Small demo via R magic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!echo %R_HOME%" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext rpy2.ipython" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# avoid some pandas deprecation warning\n", "import warnings\n", "warnings.filterwarnings(\"ignore\", category=DeprecationWarning)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%R\n", "library('dplyr')\n", "library('nycflights13') \n", "write.csv(flights, \"flights.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%R head(flights) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%R airports %>% mutate(dest = faa) %>% semi_join(flights) %>% head" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6 - Installing the very best of R pakages (optional, you will start to get a really big directory)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# essentials: 'tidyr', 'shiny', 'ggplot2', 'caret' , 'nnet' \n", "# remaining of Hadley Wickahm \"stack\" (https://github.com/rstudio)\n", "%R install.packages(c('tidyr', 'ggplot2', 'shiny','caret' , 'nnet'), repos='https://cran.rstudio.com/') \n", "%R install.packages(c('knitr', 'purrr', 'readr', 'readxl'), repos='https://cran.rstudio.com/')\n", "%R install.packages(c('rvest', 'lubridate', 'ggvis', 'readr','base64enc'), repos='https://cran.rstudio.com/')\n", "\n", "# TRAINING = online training book http://r4ds.had.co.nz/ (or https://github.com/hadley/r4ds)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7 - Relaunch Jupyter Notebook to get a R kernel option\n", "launch a new notebook of \"R\" type, and type in it:\n", " \n", "library('dplyr')\n", "\n", "library('nycflights13') \n", "\n", "head(flights)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9 - To Un-install / Re-install R (or other trouble-shooting)\n", "\n", "- launch winpython**\\t\\R\\unins000.exe (was formerly winpython**\\tools\\R\\unins000.exe)\n", "\n", "- delete the directory winpython**\\t\\R (was formerly winpython**\\tools\\R)\n", "\n", "- re-install\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%R install.packages(c('bindrccp'), repos='http://cran.rstudio.com/') " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }