Читать книгу R For Dummies - Vries Andrie de - Страница 6
Part I
Getting Started with R Programming
Chapter 2
Exploring R
Working with a Code Editor
ОглавлениеR is many things: a programming language, a statistical processing environment, a way to solve problems, and a collection of helpful tools to make your life easier. The one thing that R is not is an application, which means that you have the freedom of selecting your own editing tools to interact with R.
In this section we discuss the Windows R interface, RGui (short for R graphical user interface). This interface also includes a very basic editor for your code. Since this standard editor is so, well, basic, we also introduce you to RStudio. RStudio offers a richer editing environment than RGui and many handy shortcuts for common tasks in R.
Alternatives to the standard R editors
Among the many freedoms that R offers you is the freedom to choose your own code editor and development environment, so you don’t have to use the standard R editors or RStudio.
These are powerful full-featured editors and development environments:
✔ Eclipse StatET (www.walware.de/goto/statet): Eclipse, another powerful integrated development environment, has an R add-in called StatET. If you’ve done software development on large projects, you may find Eclipse useful. Eclipse requires you to install Java on your computer.
✔ Emacs Speaks Statistics (http://ess.r-project.org): Emacs, a powerful text and code editor, is widely used in the Linux world and also is available for Windows. It has a statistics add-in called Emacs Speaks Statistics (ESS), which is famous for having keyboard shortcuts for just about everything you could possibly do and for its very loyal fan base. If you’re a programmer coming from the Linux world, this editor may be a good choice for you.
✔ Tinn-R (http://nbcgib.uesc.br/lec/software/editores/tinn-r/en): This editor, developed specifically for working with R, is available only for Windows. It has some nice features for setting up collections of R scripts in projects. Tinn-R is easier to install and use than either Eclipse or Emacs, but it isn’t as fully featured.
A couple of interfaces are designed as tools for special purposes:
✔ Rcommander (http://www.rcommander.com/): Rcommander provides a simple GUI for data analysis in R and contains a variety of plugins for different tasks.
✔ Rattle (http://rattle.togaware.com/): Rattle is a GUI designed for typical data mining tasks.
Exploring RGui
As part of the process of downloading and installing R, you get the standard graphical user interface (GUI), called RGui. RGui gives you some tools to manage your R environment – most important, a console window. The console is where you type instructions and generally get R to do useful things for you.
Seeing the naked R console
The standard installation process creates useful menu shortcuts (although this may not be true if you use Linux, because there is no standard GUI interface for Linux). In the menu system, look for a folder called R
, and then find an icon called R followed by a version number (for example, R 3.2.0, as shown in Figure 2-1).
Figure 2-1: Shortcut icons for RGui (R x64) and RStudio.
When you open RGui for the first time, you see the R Console screen (shown in Figure 2-2), which lists some basic information such as your version of R and the licensing conditions.
Figure 2-2: A brand-new session in RGui.
Below all this information is the R prompt, denoted by a >
symbol. The prompt indicates where you type your commands to R; you see a blinking cursor to the right of the prompt.
We explore the R console in more depth in “Navigating the Environment,” later in this chapter.
Issuing a simple command
Use the console to issue a very simple command to R. Type the following to calculate the sum of some numbers, directly after the prompt:
> 24 + 7 + 11
R responds immediately to your command, calculates and displays the total in the console:
> 24 + 7 + 11
[1] 42
The answer is 42. R gives you one other piece of information: The [1]
preceding 42
indicates that the value 42
is the first element in your answer. It is, in fact, the only element in your answer! One of the clever things about R is that it can deal with calculating many values at the same time, which is called vector operations. We talk about vectors later in this chapter – for now, all you need to know is that R can handle more than one value at a time.
Closing the console
To quit your R session, type the following code in the console, after the command prompt (>
):
> quit()
R asks you a question to make sure that you meant to quit, as shown in Figure 2-3. Click No, because you have nothing to save. This action closes your R session (as well as RGui, if you’ve been using RGui as your code editor). In fact, saving a workspace image rarely is useful.
Figure 2-3: R asks you a simple question.
Dressing up with RStudio
RStudio is a code editor and development environment with some very nice features that make code development in R easy and fun:
✔ Code highlighting that gives different colors to keywords and variables, making it easier to read
✔ Automatic bracket and parenthesis matching
✔ Code completion, so you don’t have to type out all commands in full
✔ Easy access to R Help, with some nice features for exploring functions and parameters of functions
✔ Easy exploration of variables and values
Because RStudio is available free of charge for Linux, Windows, and Apple OS X, we think it’s a good option to use with R. In fact, we like RStudio so much that we use it to illustrate the examples in this book. Throughout the book, you find some tips and tricks on how things can be done in RStudio. If you decide to use a different code editor, you can still use all the code examples and you’ll get identical results.
To open RStudio, click the RStudio icon in your menu system or on your desktop. (You can find installation instructions in this book’s appendix.)
Once RStudio starts, choose File⇒New⇒R Script to open a new script file.
Your screen should look like Figure 2-4. You have four work areas (also called panes):
✔ Source: The top-left corner of the screen contains a text editor that lets you work with source script files. Here, you can enter multiple lines of code, save your script file to disk, and perform other tasks on your script. This code editor works a bit like every other text editor you’ve ever seen, but it’s smart. It recognizes and highlights various elements of your code, for example (using different colors for different elements), and it also helps you find matching brackets in your scripts.
✔ Console: In the bottom-left corner, you find the console. The console in RStudio can be used in the same way as the console in RGui (refer to “Seeing the naked R console,” earlier in this chapter). This is where you do all the interactive work with R.
✔ Environment and History: The top-right corner is a handy overview of your environment, where you can inspect the variables you created in your session, as well as their values. (We discuss the environment in more detail later in this chapter.) This is also the area where you can see a history of the commands you’ve issued in R.
✔ Files, plots, package, help, and viewer: In the bottom-right corner, you have access to several tools:
● Files: This is where you can browse the folders and files on your computer.
● Plots: This is where R displays your plots (charts or graphs). We discuss plots in Part V.
● Packages: You can view a list of all installed packages.
A package is a self-contained set of code that adds functionality to R, similar to the way that add-ins add functionality to Microsoft Excel.
● Help: This is where you can browse R's built-in Help system.
● Viewer: This is where RStudio displays previews of some advanced features, such as dynamic web pages and presentations that you can create with R and add-on packages.
Figure 2-4: RStudio’s four work areas (panes).