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:
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:
Within your R script there are a number of possible ways of saving plots. The following example uses base R:
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:
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:
The function can then be called on an R script as follows:
We would recommend that you save the function to your .bashrc
file so that the function is globally available.