Jump to content

Introducing the R Console

0
  adfm's Photo
Posted Jan 08 2010 05:26 PM

This excerpt from Joseph Adler's R in a Nutshell introduces the R console. If you use R, you'll spend most of your time interacting with the console.


The R console is the most important tool for using R. The R console is a tool that allows you to type commands into R and see how the R system responds. The commands that you type into the console are called expressions. A part of the R system called the interpreter will read the expressions and respond with a result or an error message. Sometimes, you can also enter an expression into R through the menus.

If you’ve used a command line before (for example, the cmd.exe program on Windows) or a language with an interactive interpreter such as LISP, this should look familiar.[2] If not: don’t worry. Command-line interfaces aren’t as scary as they look. R provides a few tools to save you extra typing, to help you find the tools you’re looking for, and to spot common mistakes. Besides, you have a whole reference book on R that will help you figure out how to do what you want.

Personally, I think that a command-line interface is the best way to analyze data. After I finish working on a problem, I want a record of every step that I took. (I want to know how I loaded the data, if I took a random sample, how I took the sample, whether I created any new variables, what parameters I used in my models, etc.) A command-line interface makes it very easy to keep a record of everything I do and then re-create it later if I need to.

When you launch R, you will see a window with the R console. Inside the console, you will see a message like this:

R version 2.9.2 (2009-08-24)

Copyright © 2009 The R Foundation for Statistical Computing

ISBN 3-900051-07-0



R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.



 Natural language support but running in an English locale



R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.



Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.



[R.app GUI 1.29 (5464) i386-apple-darwin8.11.1]



[Workspace restored from /Users/josephadler/.RData]



>

This window displays some basic information about R: the version of R you’re running, some license information, quick reminders about how to get help, and a command prompt.

By default, R will display a greater-than sign (“>”) in the console (at the beginning of a line, when nothing else is shown) when R is waiting for you to enter a command into the console. R is prompting you to type something, so this is called a prompt. This book includes many examples of expressions that I entered into R (and that you can enter into R) and the responses from the R system. In each of these cases, I have shown the prompt from R as a way to differentiate between the commands I entered into R and the responses from the R system.

What this means is that you should not type a command prompt (“>”) if you see one at the beginning of a line. If you want to duplicate my results, type whatever appears after the prompt. For example, I might include a snippet that looks like this:

> 17 + 3

[1] 20

This means:

  • I entered “17 + 3” into the R command prompt.

  • The computer responded by writing “[1] 20”.

If you would like to try this yourself, then type “17 + 3” at the command prompt and press the Enter key. You should see a response like the one shown above.

Sometimes, an R command doesn’t fit on a single line. If you enter an incomplete command on one line, the R prompt will change to a plus sign (“+”). Here’s a simple example:

> 1 * 2 * 3 * 4 * 5 *

+ 6 * 7 * 8 * 9 * 10

[1] 3628800

This could cause confusion in some cases (such as in long expressions that contain sums or inequalities). On most platforms, command prompts, user-entered text, and R responses are displayed in different colors to help clarify the differences. Table 2-1 presents a summary of the default colors.

Table 2.1. Text colors in R interactive mode

PlatformCommand promptUser inputR output
Mac OS XPurpleBlueBlack
Microsoft WindowsRedRedBlue
LinuxBlackBlackBlack

Command-Line Editing

On most platforms, R provides tools for looking through previous commands.[3] You will probably find the most important line edit commands are the up and down arrow keys. By placing the cursor at the end of the line, you can scroll through previous commands by pressing the up arrow or the down arrow. The up arrow lets you look at earlier commands, and the down arrow lets you look at later commands. If you would like to repeat a previous command with a minor change (such as a different parameter), or if you need to correct a mistake (such as a missing parenthesis), you can do this easily.

You can also type history() to get a list of previously typed commands.[4]

R also includes automatic completions for function names and filenames. Type the “tab” key to see a list of possible completions for a function or filenames.



[2] Incidentally, R has quite a bit in common with LISP: both languages allow you to compute expressions on the language itself, both languages use similar internal structures to hold data, and both languages use lots of parentheses.

[3] On Linux and Mac OS X systems, the command line uses the GNU readline library and includes a large set of editing commands. On Windows platforms, a smaller number of editing commands are available.

[4] As of this writing, the history command does not work completely correctly on Mac OS X. The history command will display the last saved history, not the history for the current session.

Cover of R in a Nutshell
Learn more about this topic from R in a Nutshell. 

R is rapidly becoming the standard for developing statistical software, and R in a Nutshell provides a quick and practical way to learn this increasingly popular open source language and environment. You'll not only learn how to program in R, but also how to find the right user-contributed R packages for statistical modeling, visualization, and bioinformatics.

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies