This topic describes how to build reports in the R statistical programming environment to analyze and visualize datasets on LabKey Server. The results of R scripts can be displayed in LabKey reports that reflect live data updated every time the script is run.
: Creating R Reports requires that the user have both the "Editor" role (or higher) and developer access (one of the roles "Platform Developer" or "Trusted Analyst") in the container. Learn more here: Developer Roles
Create an R Report from a Data Grid
R reports are ordinarily associated with individual data grids. Choose the dataset of interest and further filter the grid as needed. Only the portion of the dataset visible within this data grid become part of the analyzed dataset.
To use the sample dataset we describe in this tutorial, please Tutorial: Set Up a New Study
if you have not already done so. Alternately, you may simply add the PhysicalExam.xls
demo dataset to an existing study for completing the tutorial. You may also work with your own dataset, in which case steps and screencaps will differ.
- View the "Physical Exam" dataset in a LabKey study.
- If you want to filter the dataset and thus select a subset or rearrangement of fields, select or create a custom grid view.
- Select (Charts/Reports) > Create R Report.
If you do not see the "Create R Report" menu, check to see that R is installed and configured on your LabKey Server. You also need to have the correct permissions to create R Reports. See Configure Scripting Engines
for more information.
Create an R Report Independent of any Data Grid
R reports do not necessarily need to be associated with individual data grids. You can also create an R report that is independent of any grid:
- Select (Admin) > Manage Views.
- Select Add Report > R Report.
R reports associated with a grid automatically load the grid data into the object "labkey.data". R reports created independently of grids do not have access to labkey.data objects. R reports that pull data from additional tables (other than the associated grid) must use the Rlabkey API to access the other table(s). For details on using Rlabkey, see Rlabkey Package
. By default, R reports not associated with a grid are listed under the Uncategorized
heading in the list on the Manage Views
Review the R Report Builder
The R report builder opens on the Source
tab which looks like this. Enter the R script for execution or editing into the Script Source
box. Notice the options
available below the source entry panel, describe below
When you select the Report
tab, you'll see the resulting graphics and console output for your R report. If the pipeline option is not selected, the script will be run in batch mode on the server.
Select the data tab to see the data on which your R report is based. This can be a helpful resource as you write or refine your script.
When your script is complete and report is satisfactory, return to the Source
tab, scroll down, and click Save
to save both the script and the report you generated.
A saved report will look similar to the results in the design view tab, minus the help text. Reports are saved on the LabKey Server, not on your local file system. They can be accessed through the Reports
drop-down menu on the grid view of you dataset, or directly from the Data Views
The script used to create a saved report becomes available to source() in future scripts. Saved scripts are listed under the “Shared Scripts” section of the LabKey R report builder.
This Syntax Reference
list provides a quick summary of the substitution parameters for LabKey R. See Input/Output Substitutions Reference
for further details.
On the Source Tab
you can expand additional option sections. Not all options are available to all users, based on permission roles granted.Options
- Make this report available to all users: Enables other users to see your R report and source() its associated script if they have sufficient permissions. Only those with read privileges to the dataset can see your new report based on it.
- Show source tab to all users: This option is available if the report itself is shared.
- Make this report available in child folders: Make your report available in data grids in child folders where the schema and table are the same as this data grid.
- Run this report in the background as a pipeline job: Execute your script asynchronously using LabKey’s Pipeline module. If you have a big job, running it on a background thread will allow you to continue interacting with your server during execution.
If you choose the asynchronous option, you can see the status of your R report in the pipeline. Once you save your R report, you will be returned to the original data grid. From the Reports
drop-down menu, select the report you just saved. This will bring up a page that shows the status of all pending pipeline jobs. Once your report finishes processing, you can click on “COMPLETE” next to your job. On the next page you’ll see "Job Status." Click on Data
to see your report.
Note that reports are always generated from live data by re-running their associated scripts. This makes it particularly important to run computationally intensive scripts as pipeline jobs when their associated reports are regenerated often.
- Select None, HTML, or Markdown processing of HTML source
- For Markdown, you can also opt to Use advanced rmarkdown output_options.
- Check the box to provide customized output_options to be used.
- If unchecked, rmarkdown will use the default output format:
html_document(keep_md=TRUE, self_contained=FALSE, fig_caption=TRUE, theme=NULL, css=NULL, smart=TRUE, highlight='default')
- Choose to auto-generate a default thumbnail if desired. You can later edit the thumbnail or attach a custom image. See Manage Views.
- Once you save an R report, its associated script becomes available to execute using source(“<Script Name>.R”) in future scripts.
- Check the box next to the appropriate script to make it available for execution in this script.
- Participant Chart: A participant chart shows measures for only one participant at a time. Select the participant chart checkbox if you would like this chart to be available for review participant-by-participant.
- Automatically cache this report for faster reloading: Check to enable.
to save settings, or Save As
to save without disturbing the original saved report.
Regardless of where you have accessed the R report builder, you can create a first R report which is data independent. This sample was adapted from the R help files.
- Paste the following into the Source tab of the R report builder.
# Execute 100 Bernoulli trials;
coin_flip_results = sample(c(0,1), 100, replace = TRUE);
- Click the Report tab to run the source and see your results, in this case the coin flip outcomes.
Add or Suppress Console Output
The options covered below can be included directly in your R report. There are also options related to console output in the scripting configuration for your R engine
Echo to Console
By default, most R commands do not generate output to the console as part of your script. To enable output to console, use the following line at the start of your scripts:
Note that when the results of functions are assigned, they are also not printed to the console. To see the output of a function, assign the output to a variable, then just call the variable. For further details, please see the FAQs for LabKey R Reports
Suppress Console Output
To suppress output to the console, hiding it from users viewing the script, first remove the echo statement shown above. You can also include sink
to redirect any outputs to 'nowhere' for all or part of your script.
To suppress output, on Linux/Mac/Unix, use:
On Windows use:
When you want to restart output to the console within the script, use sink
again with no argument: