R For Dummies. Vries Andrie de
Чтение книги онлайн.
Читать онлайн книгу R For Dummies - Vries Andrie de страница 6
✔ 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.
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.
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.
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.
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.
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