Tải bản đầy đủ - 0 (trang)
Chapter 2. The R User Interface

Chapter 2. The R User Interface

Tải bản đầy đủ - 0trang

Windows

By default, R is installed into %ProgramFiles%R (which is usually C:\Program Files

\R) and installed into the Start menu under the group R. When you launch R in

Windows, you’ll see something like the user interface shown in Figure 2-1.1 Inside

the R GUI window, there is a menu bar, a toolbar, and the R console.



Figure 2-1. R user interface on Windows XP



Mac OS X

The default R installer will add an application called R to your Applications folder

that you can run like any other application on your Mac. When you launch the R

application on Mac OS X systems, you’ll see something like the screen shown in

Figure 2-2. Like the Windows system, there is a menu bar, a toolbar with common

functions, and an R console window.

On a Mac OS system, you can also run R from the terminal without using the GUI.

To do this, first open a terminal window. (The terminal program is located in the

Utilities folder inside the Applications folder.) Then enter the command “R” on the

command line to start R.



1. Yes, these are old screen shots. R has not changed very much, so we kept these the same in

the second edition.



8 | Chapter 2: The R User Interface



www.it-ebooks.info



R User Interface



Figure 2-2. R user interface on Mac OS X



Linux and Unix

On Linux systems, you can start R from the command line by typing:

$ R



Notice that it’s a capital “R”; filenames on Linux are case sensitive. (And don’t type

the “$” character; that’s just the Unix prompt.)

Unlike the default applications for Mac OS and Windows, this will start an interactive R session on the command line itself. If you prefer, you can launch R in an

application window similar to the user interface on other platforms. To do this, use

the following command:

$ R -g Tk &



This will launch R in the background running in its own window, as shown in

Figure 2-3. Like the other platforms, there is a menu bar with some common functions, but unlike the other platforms, there is no toolbar. The main window acts as

the R console.



The R Graphical User Interface | 9



www.it-ebooks.info



Figure 2-3. The interface for R on Fedora



Additional R GUIs

If you’re a typical desktop computer user, you might find it surprising to discover

how little functionality is implemented in the standard R GUI. The standard R

GUI implements only very rudimentary functionality through menus: reading

help, managing multiple graphics windows, editing some source and data files,

and some other basic functionality. There are no menu items, buttons, or palettes

for loading data, transforming data, plotting data, building models, or doing any

interesting work with data. Commercial applications like SAS, SPSS, and S-PLUS

include UIs with much more functionality.

Several projects are aiming to build an easier-to-use GUI for R:

Rcmdr

The Rcmdr project is an R package that provides an alternative GUI for R.

You can install it as an R package. It provides some buttons for loading data

and menu items for many common R functions.

Rkward

Rkward is a slick GUI front end for R. It provides a palette and menu-driven

UI for analysis, data-editing tools, and an IDE for R code development. It’s

still a young project and currently works best on Linux platforms (though

Windows builds are available). It is available from http://sourceforge.net/apps/

mediawiki/rkward/.



10 | Chapter 2: The R User Interface



www.it-ebooks.info



R Productivity Environment

Revolution Computing recently introduced a new IDE called the R Productivity Environment. This IDE provides many features for analyzing data: a

script editor, object browser, visual debugger, and more. The R Productivity

Environment is currently available only for Windows, as part of Revolution

R Enterprise.



You can find a list of additional projects at http://www.sciviews.org/_rgui/. This

book does not cover any of these projects in detail. However, you should still be

able to use this book as a reference for all of these packages because they all use

(and expose) R functions.



The R 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 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.15.1 (2012-06-22) -- "Roasted Marshmallows"

Copyright (C) 2012 The R Foundation for Statistical Computing

ISBN 3-900051-07-0

Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.



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.



The R Console | 11



www.it-ebooks.info



R User Interface



RStudio

RStudio is a popular, open source IDE for working with R. To learn more,

see “RStudio” on page 15.



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.52 (6188) x86_64-apple-darwin9.8.0]

[History restored from /Users/jadler/.Rapp.history]



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.

For example, suppose that you typed 17 + 3 on the console. You would see something similar to this:

> 17 + 3

[1] 20



This means:

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

• The computer responded by writing “[1] 20” (I’ll explain what that means in

Chapter 3).

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. In this

book, I will show text that I have typed in boldface. So, when you see an entry like

this in the book:

> 17 + 3

[1] 20



that means that I typed “17 + 3” into the console but that all the other text was

generated by R. (Your terminal probably won’t display text you have entered in

bold.)

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



12 | Chapter 2: The R User Interface



www.it-ebooks.info



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

Command prompt



User input



R output



Mac OS X



Purple



Blue



Black



Microsoft Windows



Red



Red



Blue



Linux



Black



Black



Black



R User Interface



Platform



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

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 a filename.



Batch Mode

R’s interactive mode is convenient for most ad hoc analyses, but typing in every

command can be inconvenient for some tasks. Suppose that you wanted to do the

same thing with R multiple times. (For example, you may want to load data from

an experiment, transform it, generate three plots as Portable Document Format

[PDF] files, and then quit.) R provides a way to run a large set of commands in

sequence and save the results to a file. This is called batch mode.

One way to run R in batch mode is from the system command line (not the R console). By running R from the system command line, it’s possible to run a set of

commands without starting R. This makes it easier to automate analyses, as you can

change a couple of variables and rerun an analysis. For example, to load a set of

commands from the file generate_graphs.R, you would use a command like this:



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 is 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.



Batch Mode | 13



www.it-ebooks.info



$ R CMD BATCH generate_graphs.R



R would run the commands in the input file generate_graphs.R, generating an output

file called generate_graphs.Rout with the results. You can also specify the name of

the output file. For example, to put the output in a file labeled with today’s date (on

a Mac or Unix system), you could use a command like this:

$ R CMD BATCH generate_graphs.R generate_graphs_`date "+%y%m%d"`.log



If you’re generating graphics in batch mode, remember to specify the output device

and filenames. For more information about running R from the command line, including a list of the available options, run R from the command line with the

--help option:

$ R --help



One key disadvantage of running R using the command R CMD BATCH is that your

scripts cannot access the system’s standard input. Luckily, there is a second command for running R in batch mode: the RScript command. You can execute a script

with a command like this:

$ RScript generate_graphs.R



Additionally, you can write executable scripts using RScript. Here’s an example of

how to do this (on Linux, Mac OS, or other Unix-like systems). First, create a file

called hello_world.R with the following contents:

#! /usr/bin/env RScript

print("Hello world!");



Next, type the following command to make the script executable:

$ chmod +x hello_world.R



Now you can execute this command like any other command:

$ ./hello_world.R

[1] "Hello world!"



We will use this ability in “Hadoop Streaming” on page 568.

Finally, you can also run commands in batch mode from inside R. To do this, you

can use the source command; see the help file for source for more information.



Using R Inside Microsoft Excel

If you’re familiar with Microsoft Excel, or if you work with a lot of data files in Excel

format, you might want to run R directly from inside Excel. The RExcel software

lets you do just that (on Microsoft Windows systems). You can find information

about this software at http://rcom.univie.ac.at/. This site also includes a single installer that will install R plus all the other software you need to use RExcel.

If you already have R installed, you can install RExcel as a package from CRAN. The

following set of commands will download RExcel, configure the RCOM server, install RDCOM, and launch the RExcel installer:



14 | Chapter 2: The R User Interface



www.it-ebooks.info



install.packages("RExcelInstaller", "rcom", "rsproxy")

# configure rcom

library(rcom)

comRegisterRegistry()

library(RExcelInstaller)

# execute the following command in R to start the installer for RDCOM

installstatconnDCOM()

# execute the following command in R to start the installer for REXCEL

installRExcel()



Follow the prompts within the installer to install RExcel.

After you have installed RExcel, you will be able to access RExcel from a menu item.

If you are using Excel 2007, you will need to select the “Add-Ins” ribbon to find this

menu, as shown in Figure 2-4. To use RExcel, first select the R Start menu item. As

a simple test, try doing the following:

1. Enter a set of numeric values into a column in Excel (for example, B1:B5).

2. Select the values you entered.

3. On the RExcel menu, go to the item Put R Var → Array.

4. A dialog box will open, asking you to name the object you are creating in Excel.

Enter v and press the Enter key. This will create an array (in this case, just a

vector) in R with the values that you entered with the name v.

5. Now, select a blank cell in Excel.

6. On the RExcel menu, go to the item Get R Value → Array.

7. A dialog box will open, prompting you to enter an R expression. As an example,

try entering (v - mean(v)) / sd(v). This will rescale the contents of v, changing

the mean to 0 and the standard deviation to 1.

8. Inspect the results that have been returned within Excel.

For some more interesting examples of how to use RExcel, take a look at the Demo

Worksheets under this menu. You can use Excel functions to evaluate R expressions,

use R expressions in macros, and even plot R graphics within Excel.



RStudio

One of the most popular ways to run R has become RStudio. RStudio is a free, opensource integrated development environment (IDE) for R. A screen shot of R Studio

is shown in Figure 2-5.

Unlike the standard R GUI, RStudio tiles windows on the screen and puts different

windows in different tabs. Additionally, you can install RStudio on a Linux server

and access R from a web browser! To learn more about RStudio and download a

copy, see http://www.rstudio.org.



RStudio | 15



www.it-ebooks.info



R User Interface



>

>

>

>

>

>

>

>

>



Figure 2-4. Accessing RExcel in Microsoft Excel 2007



Figure 2-5. R Studio



16 | Chapter 2: The R User Interface



www.it-ebooks.info



Other Ways to Run R

There are several open-source projects that allow you to combine R with other

applications:



As a server

The Rserve software allows you to access R from within other applications. For

example, you can produce a Java program that uses R to perform some calculations. As the name implies, Rserve is implemented as a network server, so a

single Rserve instance can handle calculations from multiple users on different

machines. One way to use Rserve is to install it on a heavy-duty server with lots

of CPU power and memory, so that users can perform calculations that they

couldn’t easily perform on their own desktops. For more about this project, see

http://www.rforge.net/Rserve/index.html.

As we described above, you can also use R Studio to run R on a server and access

if from a web browser.

Inside Emacs

The ESS (Emacs Speaks Statistics) package is an add-on for Emacs that allows

you to run R directly within Emacs. For more on this project, see http://ess.r

-project.org/.



Other Ways to Run R | 17



www.it-ebooks.info



R User Interface



As a web application

The rApache software allows you to incorporate analyses from R into a web

application. (For example, you might want to build a server that shows sophisticated reports using R lattice graphics.) For information about this project, see

http://biostat.mc.vanderbilt.edu/rapache/.



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 2. The R User Interface

Tải bản đầy đủ ngay(0 tr)

×