R is a statistical programming environment frequently used with to analyze and visualize datasets on LabKey Server. This topic describes how an administrator can install and configure R.

Set Up Steps:

Install R

Website: http://www.r-project.org/

License: GNU GPL 2.0

  • On the R site, choose a CRAN mirror near you.
  • Click Download R for the OS you are using (Linux, OSX, or Windows).
  • Click the subcategory base.
  • Download the installer using the link provided for your platform, for example Download R #.#.# for Windows.
  • Install using the downloaded file.


  • You don't need to download the "contrib" folder on the Install site. It's easy to obtain additional R packages individually from within R.
  • Details of R installation/administration can be found here

OS-Specific Instructions: Windows

  • Windows. On Windows, install R in a directory whose path does not include a space character. The R FAQ warns to avoid spaces if you are building packages from sources.

OS-Specific Instructions: Linux

There are different distributions of R for different versions of Linux. Find and obtain the correct package for your version here, along with version specific instructions:

Configure LabKey Server to Work with R

Follow the instructions in this topic to configure R within LabKey Server:

Configure Authentication and Permissions

Authentication. If you wish to modify a password-protected LabKey Server database through the Rlabkey macros, you will need to set up authentication. See: Create a netrc file.

Permissions. Refer to Configure Permissions for information on how to adjust the permissions necessary to create and edit R Views. Note that only users who have the "Editor" role (or higher) plus either one of the developer roles "Platform Developer" or "Trusted Analyst" can create and edit R reports. Learn more here: Developer Roles.

Batch Mode. Scripts are executed in batch mode, so a new instance of R is started up each time a script is executed. The instance of R is run using the same privileges as the LabKey Server, so care must be taken to ensure that security settings (see above) are set accordingly. Packages must be re-loaded at the start of every script because each script is run in a new instance of R.

Install & Load Additional R Packages

You will likely need additional packages to add functionality that basic install does not include. Additional details on CRAN packages are available here:

Each package needs to be installed AND loaded. Packages only need to be installed once on your LabKey Server. However, they will need to be loaded at the start of every script when running in batch mode.

How to Install R Packages

Use the R command line or a script (including a LabKey R script) to install packages. For example, use the following to install two useful packages, "GDD" and "Cairo":

install.packages(c("GDD", "Cairo"), repos="http://cran.r-project.org" )

To install knitr:

install.packages('knitr', dependencies=TRUE)

You can also use the R GUI (Packages > Install Packages) to select and install packages.

How to Load

If the installed package is not set up as part of your native R environment (check 'R_HOME/site-library'), it needs to be loaded every time you start an R session. Typically, when running R from the LabKey interface, you will need to load (but not install) packages at the start of every script because each script is run in a new instance of R.

To load an installed package (e.g., Cairo), call:


Recommended Packages

GDD &/or Cairo: If R runs on a headless Linux server, you will likely need at least one extra graphics package. When LabKey R runs on a headless Linux server, it may not have access to the X11 device drivers (and thus fonts) required by the basic graphics functions jpeg() and png(). Installing the Cairo and/or GDD packages will allow your users to output .jpeg and .png formats without using the jpeg() and png() functions. More details on these packages are provided on the Determine Available Graphing Functions page.

Lattice: Optional. This package is the commonly used, sophisticated graphing package for R. It is particularly useful for creating Participant Charts.

Related Topics

Was this content helpful?

Log in or register an account to provide feedback

expand allcollapse all