@@ -55,7 +55,11 @@ situations however where you may want to play with other notebooks.
...
@@ -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
4. Open the freshly uploaded notebook through the Jupyter file
manager.
manager.
** Running R and Python in the same notebook
** 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=:
1. Loading =rpy2=:
#+begin_src python :results output :exports both
#+begin_src python :results output :exports both
%load_ext rpy2.ipython
%load_ext rpy2.ipython
...
@@ -74,6 +78,9 @@ situations however where you may want to play with other notebooks.
...
@@ -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
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
an other possibility is to use =%R= to have a single line of R within a
python cell.
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
** Other languages
Jupyter is not limited to Python and R. Many other languages are available:
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
[[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
...
@@ -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
you proceeded otherwise and only have Python available in Jupyter, you
#+begin_src R :results output :session *R* :exports both
- Install the =IRkernel= package:
devtools::install_github('IRkernel/IRkernel')
#+begin_src R :results output :session *R* :exports both
IRkernel::installspec() # to register the kernel in the current R installation
devtools::install_github('IRkernel/IRkernel')
#+end_src
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)
*** • 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
#+begin_src shell :results output :exports both
sudo apt-get install python3-rpy2 python3-tzlocal
sudo apt-get install python3-rpy2 python3-tzlocal
#+end_src
#+end_src
...
@@ -167,7 +204,7 @@ pip3 install rpy2
...
@@ -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.
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:
** Exécuter du code R et du code Python dans le même notebook
** 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
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.
faut tout d'abord ouvrir un notebook python.
1. Chargez la bibliothèque =rpy2=. Le =%load_ext= est une commande magique
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
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
...
@@ -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
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
l'afficher correctement. Il est également possible d'utiliser =%R= pour
avoir une seule ligne de R au sein d'une cellule python.
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
** Autres langages que Python et R
Jupyter tire son nom des langages Julia, Python, et R. Il ne se limite
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
donc pas aux langages Python et R. De nombreux autres langages de
...
@@ -168,7 +172,24 @@ certainement suivre les étapes suivantes
...
@@ -168,7 +172,24 @@ certainement suivre les étapes suivantes
*** • Installation de [[https://github.com/IRkernel/IRkernel][IRKernel]] (R package)
*** • Installation de [[https://github.com/IRkernel/IRkernel][IRKernel]] (R package)
IRKernel vous permettra de faire des notebooks utilisant le langage R
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à
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=:
- Installer la bibliothèque =devtools=:
#+begin_src R :results output :session *R* :exports both
#+begin_src R :results output :session *R* :exports both
install.packages('devtools',dep=TRUE)
install.packages('devtools',dep=TRUE)
...
@@ -185,11 +206,22 @@ installé et fonctionnel sur votre ordinateur. Dans une console R:
...
@@ -185,11 +206,22 @@ installé et fonctionnel sur votre ordinateur. Dans une console R:
devtools::install_github('IRkernel/IRkernel')
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R installation
IRkernel::installspec() # to register the kernel in the current R installation
#+end_src
#+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)
*** • Installation de rpy2 (Python package)
La bibliothèque =rpy2= permet à Python d'appeler R et donc de mélanger
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
les deux langages dans le même notebook.
général disponible sur les distributions récentes. Par exemple sous
debian ou ubuntu:
*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
#+begin_src shell :results output :exports both
sudo apt-get install python3-rpy2 python3-tzlocal
sudo apt-get install python3-rpy2 python3-tzlocal
#+end_src
#+end_src
...
@@ -205,15 +237,24 @@ pip3 install rpy2
...
@@ -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
Téléchargez le [[https://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2][fichier binaire]] =rpy2= en choisissant le bon système
d'exploitation.
d'exploitation.
Ouvrez une console DOS et tapez les commandes suivantes:
Ouvrez une console DOS, placez vous dans le dossier de téléchargement,