As noted before R is a command line tool. Typically we refer to it as the console. While this can be daunting for new users, this is actually one of the benefits of working with R. While GUIs (Graphical User Interfaces) may be easier to learn initially, it is difficult to repeat analysis with them, and it is also nearly impossible to share how you did them with others. This unit will introduce you to working on the command line and also show a fairly simple workflow to help you easily record your analysis for future use.

Basic Console

The console is the primary way you will interact with R and fundamentally it is very simple. You type something into it the console after the “>” and R does something. The simplest, and least useful thing to do, is to just type in a value.


> 4
[1] 4

You can also do math this way.


> (4 + 4)/3
[1] 2.666667

This is all fine for very simple things, but to really make it useful we should assign the value to a variable.


> y <- (4 + 4)/3  # This is the R way of assigning to a variable, 
                  # notice there is no response

> x = (4 + 4)/3   # You can use an equals sign too. 

The somewhat idiosyncratic notation of <- for assignment is largely due to R’s evolution from S. Back then, there was no == for testing for equivalence in S. Also, many keyboards at the time had an actual <- key. As other programming languages developed, R has evolved to be more similar to them in some ways, but still retains the <- assignment operator. R style guides recommond using <- for readability so we’ll use that here.

If you look on the upper right panel in the Environment tab, you will see the value has been stored under the variable x. We can now use this for future analyses.


> x <- (4 + 4)/3
> a <- 27
> a^x   # The carat "^" means to the power of. 2^2 = 4
[1] 6561

Sometimes you may want to remove something from your environment. You can use the rm() function for this.


> z <- "oops"
> z
[1] "oops"
> rm(z)
> z
Error: object 'z' not found

Building a Script

Open up a new script by clicking the new script button The new file button at the top right of your window. When building up a complex analysis, it often takes trial and error to get what you want. A typical workflow is to use the console to perfect your workflow then copy the commands over to your script. One way to do this is to use the History in the top right panel. Go there and you will see your commands listed. Select the line(s) you want then click To Source and the command will be moved to your script. Do this and make your script look like this.


y <- 27
x <- (4 + 4)/3
print(y^x)   # The print function sends the result to your console.

Now on the Environment tab click the broom. This clears the your environment. Now put the cursor on your script to the first line and click the Run button. Notice the y value is put back into memory. Click Run two more times and notice the results.

Clear your environment again (click the broom) and now click Source on your script. This runs the entire script. Both are useful. Run is especially handy for stepping through your script to debug them.

This method of building up scripts via the command line is very common. The technique of clearing your environment is also very important. It is actually pretty easy to create a script that “works” because you have a variable in your environment that the script needs, then fails when that variable is no longer accidentally available. By clearing the environment you can detect these problems and correct them.