Convert between R markdown and Jupyter notebook
This table lists the difference between exporting Jupyter notebook and knitting R markdown.
Description | Jupyter notebook | R markdown | Knit |
---|---|---|---|
Support options command.It is mainly used to suppress warnings and adjust figure size, e.g. {r warning=FALSE} , {r fig.width=5,fig.size=5} . |
✗ | ✓ | ✓ |
Preview graphs created by grid library.In R Studio, write grid.newpage() at the beginning of each chunk to fix it. |
✓ | ✗ | ✓ |
-
Create a python virtual environment. Denote the location is
$venv
. -
Run
pip install jupyterlab
in Python environment to install Jupyter Lab. Reference There should be an executive file$venv\Scripts\jupyter-lab.exe
. -
Run the following script in R terminal, where
$venv
should be absolute path. (i.e.C:\Users\Administrator\PycharmProjects\R\venv
) It should only use slash/
and avoid backslash\
. Reference# Python environment. Use slash '/' only. setwd('$venv/Scripts') install.packages("IRkernel") IRkernel::installspec()
-
Create a directory to save your projects. Denote the location is
$JupyterLab
. -
Run the following script to start Jupyter lab.
netstat -ano | findstr :8888 cd $JupyterLab $venv\Scripts\jupyter-lab.exe --port 8888
-
Run the following command to install R 4.x.
sudo apt install --no-install-recommends software-properties-common dirmngr wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" sudo apt install --no-install-recommends r-base
-
If you have
conda
, run the following command.conda create -n R python=3.11 pip install jupyterlab cd ~/.conda/envs/R/bin/ R
Otherwise, please use
pip install jupyterlab
in Python environment and locate tojupyter-lab
executive file. You can usewhich python
to locate Python installation path (denoted as$venv
) and browse to$venv/bin/
. -
In R command line, run the following command to install R kernel for Jupyter lab.
install.packages("IRkernel") IRkernel::installspec() quit()
-
If you access Ubuntu machine by SSH, run the following command to start Jupyter lab.
cd ~ jupyter-lab --port 6007 --no-browser --ip 0.0.0.0
You can customize the port
6007
, but should not modify IP address0.0.0.0
.If you use Ubuntu machine and access Jupyter lab locally (without SSH), run the following command to start Jupyter lab.
cd ~ jupyter-lab --port 6007
In this case, you have done all the steps.
-
Start a SSH tunnel:
Local port forwarding Remote server: 0.0.0.0 Remote port: 6007 Forwarded port: 6007
SSH server is different according to how you connect to Ubuntu machine.
- Export customized key bindings setting by copying the files in
%APPDATA%/RStudio/keybindings
.
We use rmd2jupyter to convert *.Rmd
to *.ipynb
. The script is modified.