Coding with R beyond the command line
Working in R exclusively through the command line is rarely appropriate in practice, except when running short and simple commands (such as those introduced in this chapter) or when experimenting with new functions. For more complicated operations, we will save our code to a file in order to have the capability, for example, to work on it on several instances or to share it with other users. This section introduces approaches to editing and saving R code.
Approaches to editing R code
Typing the expression 5+5
into the command line was easy enough. However, if we perform more complicated operations, we'll have to edit and save our code for later use. There are three main approaches to edit R code:
- Using R's built-in editor
- Using a text editor
- Using an Integrated Development Environment (IDE)
Using R's built-in editor is the simplest way to edit R code. In this case, you don't need to use any software other than R. To open the code editor, simply navigate to File | New script from R's menu. A blank text window will open. You can type code in this window and execute it by clicking on Ctrl + R, either after selecting the code section that you want to execute (the selected section will be sent to the interpreter) or by placing the cursor on the line that you want to execute (that line will be sent to the interpreter).
The following screenshot shows the way RGui appears with both a command-line window and a code editor window:
You can save the R code that you have written to a file at any time (File | Save as...) in order to be able to work on it another day. An R code file is a plain text file, usually with the suffix .R
. To open an existing R code file, simply select it after navigating to the File | Open script... menu.
It is sometimes easier to use other text editors since they provide more options than R's basic text editor. For example, one can edit R code in the all-purpose Notepad++ text editor, which is available for free at http://notepad-plus-plus.org/. Notepad++ can be customized to edit code written in different programming languages (including R). By selecting the appropriate language, the specific function names and operators of that language will be highlighted in different colors for easier interpretation by the user.
The following screenshot shows Notepad++ with the menus used to select the R programming language:
A code section can be transferred to the R interpreter simply by copying it from the text editor and then pasting into the R command line. To automatically pass code into the R interpreter (such as by clicking Ctrl + R), it might be necessary to install an add-on component such as the NppToR software for Notepad++ (which is freely available at http://sourceforge.net/projects/npptor/), or use a text editor such as Tinn-R (which is freely available at http://sourceforge.net/projects/tinn-r/) that has this capability built in.
The most sophisticated way of editing R code is to use an IDE, where an advanced text editor and the R interpreter portions are combined within a single window (much like in RGui itself), in addition to many other advanced functions that may be of help in programming and are not found in RGui. These can include automatic code completion, listings of libraries and functions, automatic syntax highlighting (to read code and output more easily), debugging tools, and much more.
Note
Note that word processors such as Microsoft Word or OpenOffice Writer are not appropriate to edit computer code. The reason is that they include many styles and symbols that will not be recognized by R (or by any programming language for that matter), and this may cause problems. For example, the quote symbol (into which the word processor may automatically convert to the symbol ") will not be recognized by R, resulting in an error.
Installation of RStudio
RStudio is an IDE designed specifically for R, and it is the recommended way of editing R code. You will quickly discover that even without using any of the advanced options, code editing in RStudio is more convenient than the previously mentioned alternatives. RStudio can be freely downloaded from www.rstudio.com.
Using RStudio
When you open RStudio, you will see the R command-line window and several additional utility panes that can display the code editor, help files, graphic output, and so on, during the course of working in R. To open a new R code file, navigate to File | New File | R Script. A code editing window, such as the one shown in the following screenshot, will appear:
In RStudio, code can be sent from the editor window into the command-line window in the same way as in RGui, that is, by pressing Ctrl + R either on a code section or on a single line of code. You can quickly switch to the code editor or to the command-line pane by clicking on it with the mouse or by pressing Ctrl + 1 or Ctrl + 2, respectively. You can also have several R code files open in different tabs within the code editor pane.
More details can be found on the RStudio website (www.rstudio.com) or in other resources such as Mark P.J. van der Loo and Edwin de Jonge's book Learning RStudio for R Statistical Computing, Packt Publishing (2012).
Note
All references mentioned in this book are collectively provided in Appendix B, Cited References.