Interactive Exercise
This exercise explores running R interactively in two ways: issuing commands that run immediately at the R prompt and creating a script of these commands to run once the script has been saved.
Get an interactive node with idev. Remember to include the -A option if you have more than one account.
After your idev job starts, a command prompt will appear, and you can begin your interactive development session. Job status is reported every 4 seconds: (PD=pending, R=running).
Once you have obtained a node, if you did not do so on the login node, load the R and RstatsPackages (after loading the module dependencies) and then start R:
Now enter some data and do something with it. The following is just an example:
Interactive mode allows for data exploration and checking elements of code, with computational resources exceeding those which may be available to you locally, but in many cases, you will want to submit scripts as batch jobs.
To complete this exercise, you will have to create an R script with these same commands embedded and source it. This will execute all of the lines in the file you source and behave in a similar fashion to the way it will when you run it as a script. In particular, printing has to be done differently than when you are just typing commands at the command line. Quit R (quit()
), exit the interactive session to return to the login node, create a file called with the commands you entered interactively (you do not necessarily need to leave the interactive session to do this, but any time spent will count against your allocation). Substitute
,
and
for the lines
,
and
. Now get back into an interactive session, start R, and source the script file (using here the assumption that you saved the script file in your home directory):
Do you get the same output as when you typed in the first set of commands? You should! Do note that the R interpreter will not be able to resolve $HOME to a file path, so you should include the full path to the file.