R Basics

The R Kernel

The R kernel is compromised of scripts written in the R and C programming languages.  It includes a set of core function libraries, an interpreter for machine interface to run R scripts or functions, and a set of powerful graphical devices.  In total, these elements are referred to as base R.


RStudio is an IDE (integrated development environment) and user interface tool for base R.  It delivers a range of utilities and functionality to maximize data and programming control, project work flow management, and document creation.  RStudio is also extensible for C-coding and for integrating thousands of package objects.  RStudio can be downloaded for free here.


click image to enlarge

The RStudio interface, displayed above, has 4 component panes:

  • Text Editor for code development and execution (upper left in image),
  • Console for interactive data analysis (lower left),
  • Workspace Manager for data object control (among other uses; upper right); and
  • Utility Pane for graphics, R package management, R help (lower right).

R Source Code: Running Scripts

The Text Editor in RStudio is where basic scripting (or source code development) takes place.    The Editor is also the place to execute R source code.  Code can be run on a line-by-line basis, the last source execution can be repeated, or the entire file can be sourced.  

To create a new script file, go to File>New>RScript on the RStudio menu.  The following code can be typed into the file or copied to the clipboard by double clicking on the code box:

The entire code block is executed by hitting the Source button.  Line-by-line execution is accomplished by placing the cursor at the top of the script file and using the Run button.  Multiple lines of interest can be run by painting the target lines and again hitting the Run button. The last run command can be repeated by hitting the Re-run button. 

Code results can be found in the console or plot panes.  The above code generates a plot image in the plot pane.  Hitting the Zoom button will launch a new window, where the details of the pairwise scatter plots can be viewed more clearly.   The Plot pane also has interactive features for exporting the graph.

Source code is saved by returning the editor pane and using standard save features on the RStudio menu.

The R Console

The RStudio console is for interactive data management.  The console is the interactive command line interface for base R and the computer’s operating system.

Getting Help in R

R offers an interactive HTML help system in one of the window panes of RStudio.  Additionally, HTML help can started from the Console with:

The Console can be used to provides help on any function, causing the appropriate HTML help page to appear in the Help pane.  The examples below show two methods to control HTML help from the console, one for the mean() and one for the spline() function:

 help() is for specific topics and help.search() is for general topics.  It employs a fuzzy criteria to find all related functions to an inquiry.  For example:

The results appear in the HTML Help pane.

Finally, the args() function can be used to quickly see all the argument inputs for any function.  The example below defines the inputs for the linear (regression) model function lm():

R Command Syntax

The console supports interactive data use, manipulations, and calculations.  Elementary commands consist of expressions or assignments with input (blue) and output (pink):

 Variable assignments using the <- or = operators will evaluate an expression, and pass the value to a stored data object in the .Data folder without printing.  Subsequently typing the data object name, results in value printing:

Some basic syntax rules are listed below:

  • R case is sensitive: object X is not the same as object x.
  • Multiple console commands can be input before evaluation or interpretation if separated by a semi-colon (;);
  • Console commands can be grouped into one expression by braces {} and the value returned is the last expression;
  • Comments start with a hash mark # and continue to the end of the line;
  • Console commands are completed by pressing return; if the statement is not complete, a continuation prompt “+” appears; either complete the command syntax as requested or hit ESC to terminate an existing process;
  • All commands entered into the Console appear in the History Log. See Help documentation on the history() and again() functions for details.

R Command History

The history() function can be used to display and select one of the last 25 executed commands.  Function argument options add additional flexibility, as shown below:

 The entire command history can be displayed by setting the max.shows argument to INF.  The short sequence Ctrl+Up (Linux and Windows) or Cmd+Up (Mac) will trigger a pop-up history command.  History logs can be quickly copied for re-sue or to facilitate scripting of  complex commands.

Data Object Management

Data object management begins by assessing the working directory where R stores objects in a .Data file.  The storage directory and all search paths used by the current session of R are found by typing:

R will look through each directory in the search path in the order profiled to find a data object. New data objects are always written to the directory occupying position 1, the current working directory.  The current working directory is displayed in the top frame of the Console or can be obtained by typing getwd().

To change the position 1 directory, use attach() as follows, pointing to an already existing directory.  To remove a directory from the search list, use detach().  For example:

The ls() and object() functions lists all objects in the current or attached directories:

To find an object regardless of type or search path locations type:

find() requires a specific object name, where apropos() is more general in its object search.  For example:

To remove an object or function from your .Data file in the primary working directory, use the rm() or remove() functions. For example:

In practice, long work sessions will clutter the working directory .Data folder with many objects.  The following command can be entered to purge the working directory of all objects:

 Saving and Loading Data

Every R object can be stored into and restored from a file with the functions save() and load(). The functions use the XDR (external data representation) standard of Sun Microsystems and others, and is platform-independent so data is portable between MS-Windows, Unix, Mac.

RStudio: Keyboard Shortcuts

Keyboard shortcuts increase interface efficiency.  The following shortcuts apply to the Console:

DescriptionLinux & WindowsMac
Move cursor to ConsoleCtrl+2Ctrl+2
Clear ConsoleCtrl+LCmd+L
Move to beginning of lineHomeCmd+Left
Move to end of lineEndCmd+Right
Navigate HistoryUp/DownUp/Down
"Pop Up" Command HistoryCtrl+UpCmd+Up
Halt executionEscEsc
Change Working DirectoryCtrl+Shift+KCtrl+Shift+K

The following shortcuts apply to the Text Editor:

DescriptionLinux & WindowsMac
Go to File/FunctionCtrl+Ctrl+
Move cursor to EditorCtrl+1Ctrl+1
New documentCtrl+Shift+NCmd+Shift+N
Open documentCtrl+OCmd+O
Save document Ctrl+SCmd+S
Close active documentCtrl+WCmd+W
Close all open documentsCtrl+Shift+WCmd+Shift+W
Preview HTMLCtrl+Shift+YCmd+Shift+Y
Knit to HTMLCtrl+Shift+HCmd+Shift+H
Compile PDF (TeX and Sweave)Ctrl+Shift+ICmd+Shift+I
Insert chunkCtrl+Alt+ICmd+Option+I
Insert code sectionCtrl+Shift+RCmd+Shift+R
Run current line/selectionCtrl+EnterCmd+Enter
Re-run previous regionCtrl+Alt+PCmd+Option+P
Run current document Ctrl+Alt+RCmd+Option+R
Run from beginning to current lineCtrl+Alt+BCmd+Option+B
Run from current line to endCtrl+Alt+ECmd+Option+E
Run the current functionCtrl+Alt+FCmd+Option+F
Run the current chunkCtrl+Alt+CCmd+Option+C
Run the next chunkCtrl+Alt+NCmd+Option+N
Source a fileCtrl+Shift+OCmd+Shift+O
Source current documentCtrl+Shift+SCmd+Shift+S
Fold selectedAlt+LCmd+Option+L
Unfold selectedShift+Alt+LCmd+Shift+Option+L
Fold allAlt+0Cmd+Option+0
Unfold allShift+Alt+)Cmd+Shift+Option+0
Go to lineShift+Alt+GCmd+Shift+Option+G
Jump toShift+Alt+JCmd+Shift+Option+J

Back | Next