Enabling R Language Support
Overview
Seeq Data Lab supports the R language through a Jupyter R kernel. However, users are required to enable the R kernel either locally for a single Seeq Data Lab project or globally for all Seeq Data Lab projects. Upon enabling the R kernel, users will now see two kernel options when creating a new notebook: Python and R.
Although the R languages is supported, the SPy module is only offered in Python. In order to interact with Python objects in R, users will need to invoke SPy commands using the R reticulate
package which automatically converts Python and R objects. By loading the reticulate package, users can import SPy library, make SPy API calls, and store results as R objects for analyses using the R language.
R Version
The R package that is preinstalled in Seeq Data Lab is r-base as distributed through Debian which is currently 4.2.2.
Enabling R Kernel
To enable the R kernel only in the current Seeq Data Lab project, open a terminal and execute:
R -e "IRkernel::installspec()"
To enable the R kernel globally for all Seeq Data Lab projects for all users, open a terminal and execute:
R -e "IRkernel::installspec(prefix='/seeq/python/global-packages')"
Refresh the browser window or tab containing the Seeq Data Lab home page. To confirm successfully enabling the R kernel, try creating an R notebook.
Disabling R Kernel
To disable the R kernel, open a terminal and execute:
jupyter kernelspec uninstall ir
(type 'y' at the prompt to confirm)
To determine if the R kernel has been enabled only in the current Seeq Data Lab project or globally for all projects, open a terminal and execute:
jupyter kernelspec list
Note the path next to the kernel labeled ir
. If the path begins with /home/datalab/.local
then it is enabled for the current project only. If the path begins with /seeq/python/global-packages
then it is enabled globally for all projects.
Creating an R Notebook
To create a new R notebook, on the Seeq Data Lab home page in Classic Notebook Mode, click the New button, then choose 'R' under Notebook. In Advanced Mode, on the Launcher tab, click the R tile under the Notebook group.
Preinstalled R Packages
Just as Seeq Data Lab has various Python packages preinstalled, there are a few R packages that have been installed as well.
Reticulate: The reticulate package is responsible for interoperability between Python and R by embedding a Python session within the R session. Reticulate is imported upon the start of a new kernel.
IRDisplay: A Jupyter specific R package that offers display functionality for rendering HTML in a notebook. IRDisplay is imported upon the start of a new kernel. Its primary use is to render SPy status messages as HTML for display after executing a code cell. See the R Tutorial notebook for demonstration.
Both packages are imported automatically when an R kernel is started so users do not need to import them in their R notebook.
Tutorial Notebooks
R to Python
The R Tutorial notebook demonstrates executing SPy commands inside an R kernel through the R package reticulate.
Python to R
Users who wish to remain in a Python kernel but utilize the R language can use R magic cells in a Jupyter notebook to execute R code in a cell or as in inline command in a cell. With the R language installed in Seeq Data Lab, users can install R packages and use them from a Python kernel. The “Python to R Example” notebook demonstrates R magic cells, manipulating and passing variables from Python to R and vice-versa, as well as installing and using R packages from a Python kernel.
R Commands
R Shell
Just as you can type python
in a terminal to access a shell inside a python environment, you can similarly do so for R by typing R
in a terminal. You can then run R commands, load R libraries and have access to R objects.
Install Packages
Users can install R packages both locally (default) and globally either from the terminal or a notebook.
To install a package in the current project:
From a terminal: R -e "install.packages('<pkg>')"
From an R notebook or inside an R shell: install.packages('<pkg>')
To install a package globally for all projects:
Supply the lib
parameter with the global packages path.
For convenience, the local user and global packages paths are stored in environment variables.
Local package path: R_LIBS_USER='/home/datalab/.local/R/user-library/4.0'
Global package path: R_LIBS_GLOBAL='/seeq/python/global-packages/R/global-library/4.0'
From a terminal: R -e "install.packages('<pkg>', lib=Sys.getenv('R_LIBS_GLOBAL'))"
From an R notebook or inside an R shell: install.packages('<pkg>', lib=Sys.getenv('R_LIBS_GLOBAL'))
Update Packages
To update outdated packages:
From a terminal: R -e "old.packages()"
From an R notebook or inside an R shell: old.packages()
To update all packages:
From a terminal: R -e "update.packages()"
From an R notebook or inside an R shell: update.packages()
To update packages installed in a specific location:
Supply the lib.loc
parameter with the desired location, R_LIBS_USER
or R_LIBS_GLOBAL
From a terminal: R -e "update.packages(lib.loc=Sys.getenv('R_LIBS_USER'))"
From an R notebook or inside an R shell: update.packages(lib.loc=Sys.getenv('R_LIBS_USER'))
List Packages
To list all installed packages regardless of location (system, local user, global):
From a terminal: R -e "installed.packages()"
From an R notebook or inside an R shell: installed.packages()
To list packages installed in a specific location:
Supply the lib.loc
parameter with the desired location, R_LIBS_USER
or R_LIBS_GLOBAL
From a terminal: R -e "installed.packages(lib.loc=Sys.getenv('R_LIBS_USER'))"
From an R notebook or inside an R shell: installed.packages(lib.loc=Sys.getenv('R_LIBS_USER'))
Package Details
To confirm installation or show the location for an installed R package:
From a terminal: R -e "find.package('<pkg')"
From an R notebook or inside an R shell: find.package('<pkg>')
To get the installed version of a package:
From a terminal: R -e "packageVersion('<pkg')"
From an R notebook or inside an R shell: packageVersion('<pkg>')
Load/Import Package
To load or import an R package in a notebook execute: library('<pkg>')