Skip to content

The HPC is changing

We will soon be switching to a new High Performance Cluster, called Double Helix. This will mean that some of the commands you use to connect to the HPC and call modules will change. We will inform you by email when you are switching over, allowing you to make the necessary changes to your scripts. Please check our HPC changeover notes for more details on what will change.

Plotting in R on the HPCΒΆ

When R is run on the HPC as a module, it will not be able to output plots due to the absence of a Graphical User Interface within the HPC. You might see errors such as: Unable to start device PNG or Unable to open connection to X11 display.

This can be solved by using an X Virtual Frame Buffer to run R in. The below is an example of how to do this:

module load lang/R/<version>
xvfb-run -a R

If, however, you do not want to write xvfb-run R each time, then you can set up an alias in your .bashrc file that will do this for you. Add the following line to your .bashrc file:

alias R='xvfb-run -a R'

Within your R script there are a number of possible ways of saving plots. The following example uses base R:

1
2
3
png("plot.png")
plot(1)
dev.off()

This will create a .png file called plot.png in the current working directory with your data plotted.

Alternatively, the tidyverse ggplot2 package can be used to save the plots to a variable which can be saved with the built-in ggsave() function. You should be able to save the generated plots bypassing the need to display them by using commands such as the following example within your RSCRIPT:

1
2
3
library(ggplot2)
fig_1 <- ggplot(cars, aes(x = speed, y = dist)) + geom_point()
ggsave(fig_1, type = "cairo", file="fig_1.<extension>")

Note that tidyverse and base R can at times interfere with each other so we would recommend selecting one strategy for your plotting needs.

If you will always be loading the same version of R you will be able to create a bash function that will help you access the the functionality with a single command:

1
2
3
4
5
function r_headless(){
module purge
module load lang/R/<version>
xvfb-run -a Rscript $1
}

The function can then be called on an R script as follows:

r_headless script.R

We would recommend that you save the function to your .bashrc file so that the function is globally available.