Skip to content

Commit

Permalink
Merge pull request cgross95#3 from billspat/2-plotting
Browse files Browse the repository at this point in the history
closes cgross95#2
  • Loading branch information
billspat authored Jun 30, 2023
2 parents 917937c + 2e394fa commit 7c5cd2b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions episodes/r-command-line.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exercises: 2
:::::::::::::::::::::::::::::::::::::: questions

- How do you run R on the HPCC through the command line?
- How can I create plots when I'm not using a graphical interface?

::::::::::::::::::::::::::::::::::::::::::::::::

Expand Down Expand Up @@ -292,13 +293,80 @@ for(i in 1:n) {

::::::::::::::::::::::::::::::::::::::::::

## Plotting with the command line

You may wonder how you might run a plot using the terminal interface? For most terminals, if you plot with the command line version of R, either nothing happens or there is an error message. It may work on some terminals, if X11 Linux graphical interface is installed and the terminal is an X11-capable (for example on MacOS is 'Quartz' is installed, a new windows will appear). However when running in 'batch' mode using the cluster (described in the next session), there is no interface at all.

There are the following techniques for handling plots when using R on the command line on HPCC

- split your code into computation and presentation sections: run computation section using CLI/Batch on HPC, and after the computation is complete, save the output to be read into visualization code that you run on a machine with a graphic user interface (OnDemand Rstudio, or even your laptop)
- capture all output to a file using commands like PDF()
- as part of your script, create an RMarkdown file that includes plotting (or other output), and use the [render](https://rmarkdown.rstudio.com/docs/reference/render.html) command in Rmarkdown to PDF or other format to be review later

We'll describe the method to capture output into a PDF here.

A sample script that uses the `pdf` function to capture plots looks like this:

```r
plotfile = 'testplots.pdf'
pdf(plotfile)

plot(iris$Petal.Length, iris$Petal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)], main="Edgar Anderson's Iris Data")

dev.off()
```

For much nore details about using this techinque, see
[Chapter 14 Output for Presentation](https://r-graphics.org/chapter-output) of Winston Chang's **R Graphics Cookbook**

Once you run the script and save the PDFs, the next challenge is to view them because, again, the terminal does not have the GUI to view PDFs.

You could

- download the PDF to your computer from the terminal using OnDemand file browser (or the MobaXterm client's file browser)
- open with the OneDemand Rstudio.



:::::::::::::::::::::::::::::::: challenge

One of the challenges with running scripts repeatedly is that it will overwrite the plot file with the same name. Modify the plotting script above that accepts a command line parameter for the the name of the PDF file. BONUS: how would you handle the case where there was no command line argument sent?

:::::::::::::::::::::::: solution

```r
args <- commandArgs(trailingOnly = TRUE)

# check if there was at least 1 arg
if length(args) >= 1 {

#assume the arg is a PDF file name, and use that to capture plots
plotfile = args[1]
pdf(plotfile)

}

# if not argument is sent, PDF capture is not enabled and the plot will display

plot(iris$Petal.Length, iris$Petal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)], main="Edgar Anderson's Iris Data")

dev.off()

```
:::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::



::::::::::::::::::::::::::::::::::::: keypoints

- Use `module spider R/<version>` to learn how to load a version of R on the HPCC
- Run `R` from the command line to start an interactive R console
- Use the `--vanilla` option to ignore extra configuration files
- Run `Rscript` to run an R script
- Use `commandArgs` to parse command line arguments in an R script
- Use `pdf()` to capture plotting into a PDF file

::::::::::::::::::::::::::::::::::::::::::::::::

0 comments on commit 7c5cd2b

Please sign in to comment.