Running Batch Jobs in LSF with R and R Markdown: A Step-by-Step Guide to Knitting Documents

Running Batch Jobs in LSF with R and R Markdown

LSF (Lattice Systems Facility) clusters provide a powerful platform for running batch jobs, particularly for data-intensive tasks such as scientific simulations and data analysis. However, running scripts or R Markdown documents within these environments can be challenging. In this article, we’ll explore the process of submitting batch jobs that knit R Markdown documents using an LSF cluster.

Overview of LSF Clusters

Before diving into the details, it’s essential to understand how LSF clusters work. A LSF cluster is a shared resource managed by the Lattice Systems Facility. It provides a pool of compute resources, such as CPUs and memory, that can be allocated to users for batch job execution.

To submit a batch job on an LSF cluster, you typically use the bsub command-line tool or the qsub interface provided by the cluster’s graphical user interface (GUI). The bsub command allows you to specify various options, such as queue selection, resource allocation, and job output parameters.

Batch Job Basics

A batch job is a script that is executed on a remote computer. In an LSF cluster, batch jobs are typically submitted using the bsub or qsub command. The job submission process involves several key components:

  • Job name: A unique identifier for the job.
  • Queue: The specific resource pool that the job will run on.
  • Resource allocation: The amount of memory, CPUs, and other resources allocated to the job.
  • Output files: The locations where job output will be written.

Running R Scripts with LSF

To run an R script on an LSF cluster, you can use the bsub command as shown in the example:

bsub -q normal -E 'test -e /nfs/users/nfs_c/username' -R "select[mem>20000] rusage[mem=20000]" -M20000 -o DOWNLOAD.o -e DOWNLOAD.e -J DOWNLOAD Rscript -e "rmarkdown::render('code/12downloadSeq.Rmd')"

This command specifies the following:

  • -q normal: Submit the job to the normal queue, which is typically a high-priority queue.
  • -E 'test -e /nfs/users/nfs_c/username': Execute the test command on the specified directory and check if it exists.
  • -R "select[mem>20000] rusage[mem=20000]": Allocate resources based on the following conditions:
    • select[mem>20000]: Select nodes with at least 20,000 MB of memory.
    • rusage[mem=20000]: Limit the job’s memory usage to 20,000 MB.
  • -M20000: Allocate 20,000 CPUs for the job.
  • -o DOWNLOAD.o -e DOWNLOAD.e: Specify output files with the same name as the input script.
  • -J DOWNLOAD: Set the job ID to DOWNLOAD.
  • Rscript -e "rmarkdown::render('code/12downloadSeq.Rmd')": Run the R script using Rscript and render the specified R Markdown document.

However, this command doesn’t work as expected. The error message indicates that there’s an issue with the quotation marks. Let’s dive deeper into why quotation marks are problematic in batch job scripts.

Quotation Marks in Batch Job Scripts

In batch job scripts, quotation marks (") can cause problems when specifying command-line arguments or executing external commands. There are a few reasons for this:

  • Space: Quotation marks introduce spaces around the argument value, which can make it difficult to distinguish between quoted and non-quoted values.
  • Escaping: In some shells, quotation marks need to be escaped using backslashes (\) or other special characters.

To avoid these issues, you can use other quoting mechanisms like double quotes (") or single quotes (') in combination with escaping. Let’s explore the correct way to specify command-line arguments for Rscript.

Correcting Quotation Marks in R Script Submission

The provided example uses single quotation marks (') around the R script name, but this can still cause issues. A better approach is to use double quotes (") instead.

bsub -q normal -E 'test -e /nfs/users/nfs_c/username' -R "select[mem>20000] rusage[mem=20000]" -M20000 -o DOWNLOAD.o -e DOWNLOAD.e -J DOWNLOAD Rscript -e "rmarkdown::render('code/12downloadSeq.Rmd')"

This syntax allows you to specify the double quotes around the script name correctly.

Running R Markdown Documents with LSF

Now that we’ve discussed how to run an R script, let’s explore how to knit an R Markdown document using Rscript.

The key difference between running an R script and a markdown document is the rendering process. When you run Rscript without specifying any output files, it will execute the code in the script but not render the Markdown document.

To fix this, you need to specify the output file for the rendered document using the -e option followed by a command that calls the rmarkdown::render() function.

bsub -q normal -E 'test -e /nfs/users/nfs_c/username' -R "select[mem>20000] rusage[mem=20000]" -M20000 -o DOWNLOAD.o -e "rmarkdown::render('code/12downloadSeq.Rmd') -o RMD" -J DOWNLOAD Rscript

In this revised command:

  • -e: Specifies a separate output file.
  • "rmarkdown::render('code/12downloadSeq.Rmd')": Calls the rmarkdown::render() function and renders the specified R Markdown document to the specified output file (RMD).
  • -o: Specifies the output file name for the job.

With this corrected command, you should be able to knit an R Markdown document using Rscript on an LSF cluster.


Last modified on 2023-05-27