3) Set a Working Directory

This Blog entry is from the Getting Started in R section in Learn R.

A working directory is where R will look for files during a session.  The files may be the R session, or in subsequent Blog entries it will be data to be imported and data saved as the result of processing. 

In Blog entry Issue commands to the R Console, it was observed that there was a failure when saving the R history, owing to the working directory not being set (rather set incorrectly).  It follows that the working directory need be set.

Start by executing Blog Entry Navigate to and launch the R command line to load the R console.

the-r-command-line.png

To identify the current working directory use the getwd() function, type the script line:

getwd()

find-the-working-directory-in-r.png

Execute the command by pressing the Enter key:

the-working-directory-has-been-written-out-in-r.png

The current working directory, which is the directory containing the executable, is returned.  Saving files to the same directory as the R software is not desirable, quite beyond it causing errors, and as such, this should be changed to an appropriate directory.

Create a directory to be used throughput these Blog entries.  In this case the files will be saved to the d:\ in a directory called R.

a-empty-windows-explorer-window-no-files-for-r.png

To set this as the working directory in R use the setwd() function with the directory in quotation marks, type:

setwd("d:/R")
setting-a-working-directory-in-r.png

  Press the Enter key to process the line of script:

having-set-a-working-directory-in-r.png

The absence of any error message confirms that the working directory has been changed, although this can be affirmed by executing the getwd() function:

checking-the-working-directory-is-properly-set-in-r.png

The working directory is now set to d:\r.

If R is exited, and y is selected to save, it can be observed that there were no errors:

ext-r-after-setting-working-directory.png

Furthermore, it can be seen that the .RHistory file has been saved to the working directory:

the-r-workspace-has-been-saved.png

2) Issue commands to the R Console.

This Blog entry is from the Getting Started in R section in Learn R.

R is an interpreted language for mathematical and statistical computing. R processes as script, line by line.  In this example the sum of 1 + 1 will be returned, which will of course be 2.  To perform such a calculation type:

type-1-plus-1-in-r-console.png

Press the Enter key to commit and execute the line of script:

result-of-1-plus-1-in-r-console.png

It can be seen that a line has been returned showing [1] 2, where [1] is the position in the result vector, where 2 is the actual value returned from the line of script.  The mathematical operators (in this case +) are much the same as Excel:

·         + Addition.

·         - Subtract

·         / Divide

·         * Multiply

This Blog entry has shown a simple line of script being written, executed and returned by R.  Although rudimentary, it is an R program.

To exit the R console, hold down the CTRL key and the D key:

saving-r-workspace-on-exit.png

There are three options presented when exiting the R console:

·         y: Save the workspace image for reloading.  This will keep everything in the current session.

·         n: Clear the workspace so that the next time r is loaded it will be afresh.

·         c: Cancel and go back to the workspace.

In this example, type:

y
typing-y-to-save-the-r-workspace-on-exit.png

Press the Enter key to commit the command:

r-has-exited-after-saving-the-workspace.png

Notice that an error was returned ‘Unable to open .Rhistory’.  The error is created as the operating system will not allow the user to write to the same directory as R is running, which introduces the concept of working directories, as follows.