diff --git a/module2/ressources/jupyter.org b/module2/ressources/jupyter.org index 4ba67825c00abc4fc6a35e5c87ccd58a1318f398..83aa7028d015f22e45e998fc5378a125e841427c 100644 --- a/module2/ressources/jupyter.org +++ b/module2/ressources/jupyter.org @@ -55,7 +55,11 @@ situations however where you may want to play with other notebooks. 4. Open the freshly uploaded notebook through the Jupyter file manager. ** Running R and Python in the same notebook -=rpy2= package allows to use both languages in the same notebook by: +It used to be impossible with earlier versions of Jupyter but it is +now very easy thanks to the the =rpy2= package (see the details of the +installation procedurer in the corresponding section below) that +allows you to use both languages in the same notebook. Simply open a +new python notebook and follow these instructions: 1. Loading =rpy2=: #+begin_src python :results output :exports both %load_ext rpy2.ipython @@ -74,6 +78,9 @@ situations however where you may want to play with other notebooks. Note that this =%%R= notation indicates that R should be used for the whole cell but an other possibility is to use =%R= to have a single line of R within a python cell. + +[[https://gitlab.inria.fr/learninglab/mooc-rr/mooc-rr-ressources/blob/master/documents/notebooks/notebook_Jupyter_Python_R.ipynb][Here]] is an notebook example using both R et Python + ** Other languages Jupyter is not limited to Python and R. Many other languages are available: [[https://github.com/jupyter/jupyter/wiki/Jupyter-kernels][https://github.com/jupyter/jupyter/wiki/Jupyter-kernels]], including @@ -130,30 +137,60 @@ The environment described in the last section should include R, but if you proceeded otherwise and only have Python available in Jupyter, you may want to read the following section. *** • Installing [[https://github.com/IRkernel/IRkernel][IRKernel]] (R package) -Do the following in R console: +IRKernel will allow you to manage notebooks using natively R rather +than python. We assume in this section that you already have a working +installation of R -Install the =devtools= package: +*Windows* +On Windows, you are unlikely to have the tools allowing you to compile +and install the latest version of IRKernel and you should rather +install the binary version. In an R console (or in Rstudio), type the +following commands: #+begin_src R :results output :session *R* :exports both -install.packages('devtools',dep=TRUE) +install.packages('IRkernel',dep=TRUE) +IRkernel::installspec() # to register the kernel in the current R installation #+end_src -Define a proxy if needed: +Right below, you will find a few explanations on how to use an R +notebook. -#+begin_src R :results output :session *R* :exports both -library(httr) -set_config(use_proxy(url="proxy", port=80, username="username", password="password")) -#+end_src +*Linux or Mac* -Install the =IRkernel= package: +To install the latest version of IRkernel, open an R console or +Rstudio. +- Install the =devtools= package: + #+begin_src R :results output :session *R* :exports both + install.packages('devtools',dep=TRUE) + #+end_src +- Define a proxy if needed (this is important if you are working in a + company which limits the access to the Internet): + #+begin_src R :results output :session *R* :exports both + library(httr) + set_config(use_proxy(url="proxy", port=80, username="username", password="password")) + #+end_src -#+begin_src R :results output :session *R* :exports both -devtools::install_github('IRkernel/IRkernel') -IRkernel::installspec() # to register the kernel in the current R installation -#+end_src +- Install the =IRkernel= package: + #+begin_src R :results output :session *R* :exports both + devtools::install_github('IRkernel/IRkernel') + IRkernel::installspec() # to register the kernel in the current R installation + #+end_src + +You will then be able to create native R notebooks: +[[file:jupyter_images/new_notebook.png]] + +[[file:jupyter_images/notebook_R.png]] +Note the R icon in the top right corner. [[https://gitlab.inria.fr/learninglab/mooc-rr/mooc-rr-ressources/blob/master/documents/notebooks/notebook_Jupyter_R.ipynb][Here is an example of R +notebook]]. *** • Installing rpy2 (Python package) -On Linux, the rpy2 package is available in standard distributions +The =rpy2= package allows python to seamlessly call R and therefore to +have both languages in the same notebook. + +*Linux or Mac* + +On Linux, the rpy2 package is available in standard distributions. For +example on debian or ubuntu: #+begin_src shell :results output :exports both sudo apt-get install python3-rpy2 python3-tzlocal #+end_src @@ -167,7 +204,7 @@ pip3 install rpy2 Download the =rpy2= [[https://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2][binary file]] by choosing the right operating system. -Open a DOS console and type the following command: +Open a DOS console, move to the /download/ directory and type the following command: #+begin_src shell :results output :exports both python -m pip install rpy2‑2.9.4‑cp37‑cp37m‑win_amd64.whl # adapt filename #+end_src @@ -176,6 +213,14 @@ Install also =tzlocal=: #+begin_src shell :results output :exports both python -m pip install tzlocal #+end_src + +If you ever run into troubles, you may want to have a look on +[[https://stackoverflow.com/questions/14882477/rpy2-install-on-windows-7][StackOverflow]] (NB : when we tried it, there has been no need to define +the =R_HOME= and =R_USER= environnement variables). + +You should be able to run Jupyter and to create a python notebook that runs +R commands by following the instructions given in the beginning of +this document (look for =rpy2=). ** Additional tips *** • Exporting a notebook Here is what we had to install on a recent Debian computer to make sure diff --git a/module2/ressources/jupyter_fr.org b/module2/ressources/jupyter_fr.org index eac87d3a12cb709cfb85c400f4f5f52cbe694110..d95411ae7cc8674e5ed62b0ce1894740d74c3c4b 100644 --- a/module2/ressources/jupyter_fr.org +++ b/module2/ressources/jupyter_fr.org @@ -66,7 +66,8 @@ ceux du MOOC. code correspondant. ** Exécuter du code R et du code Python dans le même notebook C'était impossible avec les premières versions de Jupyter mais c'est -désormais très facile grâce à la bibliothèque python =rpy2=. Il vous +désormais très facile grâce à la bibliothèque python =rpy2= (les détails +d'installation sont donnés plus bas dans ce document) Il vous faut tout d'abord ouvrir un notebook python. 1. Chargez la bibliothèque =rpy2=. Le =%load_ext= est une commande magique jupyter qui charge cette bibliothèque et vous donne accès à de @@ -91,6 +92,9 @@ python (=rpy2=) maintient une session R, lui passe le code de la cellule et récupère le résultat. Jupyter fait alors le nécessaire pour l'afficher correctement. Il est également possible d'utiliser =%R= pour avoir une seule ligne de R au sein d'une cellule python. + +Un exemple de notebook utilisant R et Python est donné [[https://gitlab.inria.fr/learninglab/mooc-rr/mooc-rr-ressources/blob/master/documents/notebooks/notebook_Jupyter_Python_R.ipynb][ici]]. + ** Autres langages que Python et R Jupyter tire son nom des langages Julia, Python, et R. Il ne se limite donc pas aux langages Python et R. De nombreux autres langages de @@ -168,7 +172,24 @@ certainement suivre les étapes suivantes *** • Installation de [[https://github.com/IRkernel/IRkernel][IRKernel]] (R package) IRKernel vous permettra de faire des notebooks utilisant le langage R plutôt que le langage Python. Nous supposons ici que R est déjà -installé et fonctionnel sur votre ordinateur. Dans une console R: +installé et fonctionnel sur votre ordinateur. + +*Windows* + +Pour windows, il ne sera probablement pas possible de compiler la +toute dernière version et il faudra installer la version binaire de la +façon suivante. Dans une console R ou bien dans Rstudio : +#+begin_src R :results output :session *R* :exports both +install.packages('IRkernel',dep=TRUE) +IRkernel::installspec() # to register the kernel in the current R installation +#+end_src + +Voir ce qui suit pour un exemple d'utilisation de notebook R. + +*Linux ou Mac* + +Pour installer la toute dernière version d'IRkernel, dans une console +R ou bien dans Rstudio : - Installer la bibliothèque =devtools=: #+begin_src R :results output :session *R* :exports both install.packages('devtools',dep=TRUE) @@ -185,11 +206,22 @@ installé et fonctionnel sur votre ordinateur. Dans une console R: devtools::install_github('IRkernel/IRkernel') IRkernel::installspec() # to register the kernel in the current R installation #+end_src + +Vous pourrez alors créer des notebooks utilisant directement R: +[[file:jupyter_images/new_notebook.png]] + +[[file:jupyter_images/notebook_R.png]] + +On remarque l'icône du logiciel R en haut à droite. Vous trouverez ici +[[https://gitlab.inria.fr/learninglab/mooc-rr/mooc-rr-ressources/blob/master/documents/notebooks/notebook_Jupyter_R.ipynb][un exemple de notebook R]]. *** • Installation de rpy2 (Python package) La bibliothèque =rpy2= permet à Python d'appeler R et donc de mélanger -les deux langages dans le même notebook. Cette bibliothèque est en -général disponible sur les distributions récentes. Par exemple sous -debian ou ubuntu: +les deux langages dans le même notebook. + +*Linux ou Mac* + +Cette bibliothèque est en général disponible sur les distributions +récentes. Par exemple sous debian ou ubuntu: #+begin_src shell :results output :exports both sudo apt-get install python3-rpy2 python3-tzlocal #+end_src @@ -205,15 +237,24 @@ pip3 install rpy2 Téléchargez le [[https://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2][fichier binaire]] =rpy2= en choisissant le bon système d'exploitation. -Ouvrez une console DOS et tapez les commandes suivantes: +Ouvrez une console DOS, placez vous dans le dossier de téléchargement, +et exécutez la commande suivante : #+begin_src shell :results output :exports both -python -m pip install rpy2‑2.9.4‑cp37‑cp37m‑win_amd64.whl # adapt filename +python -m pip install rpy2‑2.9.4‑cp37‑cp37m‑win_amd64.whl # adaptez le nom de fichier #+end_src Installez également =tzlocal=: #+begin_src shell :results output :exports both python -m pip install tzlocal #+end_src + +En cas de difficulté, vous pouvez vouloir vous référer à +[[https://stackoverflow.com/questions/14882477/rpy2-install-on-windows-7][StackOverflow]] (NB : quand nous avons essayé, nous n'avons pas eu +besoin de définir les variables d'environnement =R_HOME= et =R_USER=). + +Vous pouvez alors lancer Jupyter et créer un notebook python utilisant +R en suivant les instructions données au début de ce document +(cherchez =rpy2=). ** Conseils additionnels *** • Exporter un notebook Jupyter évolue rapidement et ces informations peuvent vite devenir diff --git a/module2/ressources/jupyter_images/new_notebook.png b/module2/ressources/jupyter_images/new_notebook.png new file mode 100755 index 0000000000000000000000000000000000000000..e8aa5d677a797aefdcb881a1327e5ae1c574fdb9 Binary files /dev/null and b/module2/ressources/jupyter_images/new_notebook.png differ diff --git a/module2/ressources/jupyter_images/notebook_R.png b/module2/ressources/jupyter_images/notebook_R.png new file mode 100755 index 0000000000000000000000000000000000000000..09c36db62592613596deef66f849a96a2e42a363 Binary files /dev/null and b/module2/ressources/jupyter_images/notebook_R.png differ