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 chunk options1 specified in the code chunk header of Markdown code blocks. | ✗ | ✓ | ✓ |
Preview graphs created by grid library. |
✓ | ✗2 | ✓ |
-
Create a Python virtual environment. Denote its absolute path is
$venv
. -
Run
pip install jupyterlab
in Python environment to install Jupyter Lab.3 It will create a file$venv\Scripts\jupyter-lab.exe
. -
Run the following script in R terminal, replacing all backslash
\
with/
.4# Python environment. Use slash '/' only. setwd('$venv/Scripts') install.packages("IRkernel") IRkernel::installspec()
-
Create a directory to save your projects. Denote the path is
$JupyterLab
. -
Run the following script to start Jupyter lab.
netstat -ano | findstr :8888 cd $JupyterLab $venv\Scripts\jupyter-lab.exe --port 8888
-
Install Jupyter Lab according to the Python environment.
-
If using "miniconda" or alternatives, run the following command, where
$env_name
is a customized string.conda create -n $env_name python=3.11 pip install jupyterlab cd ~/.conda/envs/$env_name/bin/ R
-
If using virtual environments, run the following command.
venv=$(which python) cd $venv/bin/ R
-
-
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
-
[Optional] Start a SSH tunnel, therefore notebooks can be visited remotely. In Windows client, start MobaXTerm and config as follows.
Local port forwarding
Remote server: 0.0.0.0
Remote port: 6007
Forwarded port: 6007
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.