This page aims to answer common questions about configuring and using the LabKey Server interface for creating R Reports
. Remember, an administrator must install and configure R on LabKey Server
before users can create and run R scripts on live datasets.
- library(), help() and data() don’t work
- plot() doesn’t work
- jpeg() and png() don’t work
- Does my report reflect live, updated data?
- Output is not printed when I source() a file or use a function
- Scripts pasted from documentation don't work in the LabKey R Script Builder
- LabKey Server becomes very, very slow when scripts execute
- Does R create security risks?
- Any good sources for advice on R scripting?
1. library(), help() and data() don’t work
LabKey Server runs R scripts in batch mode. Thus, on Windows machines it does not display the pop-up windows you would ordinarily see in R’s interpreted/interactive mode. Some functions that produce pop-ups (e.g., library()) have alternatives that output to the console. Some functions (e.g., help() and some forms of data()) do not.Windows Workaround #1: Use alternatives that output to the consolelibrary()
: The library() command has a console-output alternative. To see which packages your administrator has made available, use the following:
Windows Workaround #2: Call the function from a native R windowhelp()
: It’s usually easy to keep a separate, native R session open and call help() from there. This works better for some functions than others. Note that you must install and load packages before asking for help() with them. You can also use the web-based documentation available on CRAN
or search the R mailing list
You can also call data() from a separate, native R session for some purposes. Calling data() from such a session can tell you which datasets are available on any packages you’ve installed and loaded in that instance of R, but not your LabKey installation.
2. plot() doesn’t work
Did you open a graphics device before calling plot()?
LabKey Server executes R scripts in batch mode. Thus, LabKey R never automatically opens an appropriate graphics device for output, as would R when running in interpreted/interactive mode. You’ll need to open the appropriate device yourself. For onscreen output that becomes part of a report, use jpeg() or png() (or their alternatives, Cairo(), GDD() and bitmap()). In order to output a graphic as a separate file, use pdf() or postscript().Did you call dev.off() after plotting?
You need to call dev.off() when you’re done plotting to make sure the plot object gets printed to the open device.
3. jpeg() and png() don’t work
R is likely running in a headless Unix server. On a headless Unix server, R does not have access to the appropriate X11 drivers for the jpeg() and png() functions. Your admin can install a display buffer on your server to avoid this problem. Otherwise, in each script you will need to load the appropriate package to create these file formats via other functions (e.g., GDD or Cairo). See also: Determine Available Graphing Functions
for help getting unstuck.
4. Does my report reflect live, updated data?
Yes. In general, LabKey always re-runs your saved script before displaying its associated report. Your script operates on live, updated data, so its plots and tables reflect fresh data.
In study folders, you can set a flag for any script that prevents the script from being re-run unless changes have occurred. This flag can save time when scripts are time-intensive or datasets are large making processing slow. When this flag is set, LabKey will only re-run the R script if:
- The flag is cleared OR
- The dataset associated with the script has changed OR
- Any of the attributes associated with the script are changed (script source, options etc.)
To set the flag, check the "Automatically cache this report for faster reloading" checkbox under "Study Options" on the Source tab of the R report builder.
5. Output is not printed when I source() a file or use a function
The R FAQ
When you use… functions interactively at the command line, the result is automatically printed...In source() or inside your own functions you will need an explicit print() statement.
When a command is executed as part of a file that is sourced, the command is evaluated but its results are not ordinarily printed. For example, if you call source(scriptname.R) and scriptname.R calls installed.packages()[,0]
, the installed.packages()[,0]
command is evaluated, but its results are not ordinarily printed. The same thing would happen if you called installed.packages()[,0]
from inside a function you define in your R script.
You can force sourced scripts to print the results of the functions they call. The R FAQ
If you type `1+1' or `summary(glm(y~x+z, family=binomial))' at the command line the returned value is automatically printed (unless it is invisible()). In other circumstances, such as in a source()'ed file or inside a function, it isn't printed unless you specifically print it.
To print the value 1+1, use
or, instead, use
where "1plus1.R" is a shared, saved script that includes the line "1+1".
6. Scripts pasted from documentation don't work in the LabKey R report builder
If you receive an error like this:
Error: syntax error, unexpected SYMBOL, expecting 'n' or ';'
in "library(Cairo) labkey.data"
please check your script for missing line breaks. Line breaks are known to be unpredictably eliminated during cut/paste into the script builder. This issue can be eliminated by ensuring that all scripts have a ";" at the end of each line.
7. LabKey Server becomes very, very slow when scripts execute
You are probably running long, computationally intensive scripts. To avoid a slowdown, run your script in the background via the LabKey pipeline. See R Report Builder
for details on how to execute scripts via the pipeline.
8. Does R Create Security Risks?
Allowing the use of R scripts/reports on a server can be a security risk. A developer could write a script that could read or write any file stored in any SiteRoot, fileroot or pipeline root despite the LabKey security settings for that file.
A user must have at least the Author role and be part of the Developer group
to write a R script or report to be used on the server.
R should not be used on a "shared server", that is, a server where users with admin/developer privileges in one project do not have permissions on other projects. Running R on the server could pose a security threat if the user attempts to access the server command line directly. The main way to execute a system command in R is via the 'system(<system call>)' method that is part of the R core package. The threat is due to the permission level of a script being run by the server possibly giving unwanted elevated permissions to the user.
9. Any good sources for advice on R scripting?R Graphics Basics: Plot area, mar (margins), oma (outer margin area), mfrow, mfcol (multiple figures)
R graphics overview
- Provides good advice how to make plots look spiffy
Bioconductor course materials
- This powerpoint provides nice visuals for explaining various graphics parameters in R
Statistical R graphics overview
- Lectures and labs cover the range - from introductory R to advanced genomic analysis
- Also links to useful example figures and code from several R books
10. Graphics File Formats
If you don’t know which graphics file format to use for your plots, this link
can help you narrow down your options.
.png and .gif
Graphics shared over the web do best in png when they contain regions of monotones with hard edges (e.g., typical line graphs). The .gif format also works well in such scenarios, but it is not supported in the default R installation because of patent issues. The GDD package allows you to create gifs in R.
Pictures with gradually varying tones (e.g., photographs) are successfully packaged in the jpeg format for use on the web.
.pdf and .ps or .eps
Use pdf or postscript when you aim to output a graph that can be accessed in isolation from your R report.