Working With R

Working With R

This section describes basic activities that you perform in the R statistical application as it is implemented in the RCE. It includes information about how to perform the following:

  • Launch and exit from the menus and from the command prompt
  • Access help
  • Install new components

See Implementation Details for general information about the implementation of R in the RCE, including where to find out more about the tool.

For information about how to access the RCE, refer to Connecting to the RCE Summer 2009.

Launching and Exiting R

Launching and Exiting R

You can launch R in the RCE by using the menus or by typing a command. As an alternative, if you have a series of similar jobs to run, you can submit them as a batch job.

Launching R

You can launch R in several different ways:

  • From a command line - Type R at a command prompt.

  • From a menu - Select the Applications menu Mathematics submenu, and then choose the option R.

  • From an XEmacs text editor - To access R in Emacs Speaks Statistics (ESS) mode:

    1. In the RCE, select the Applications menu Programming submenu, and then choose the option XEmacs Text Editor.

    2. In XEmacs, press the key sequence Alt+X R, and then press Return.

To launch the R Commander interface, type R at a command prompt, and then type the command library(Rcmdr).

Exiting R

To exit the R application, type q() on the command line.

Launching R in BATCH Mode

Our for submitting R batch jobs is documented in Getting Started with Batch Processing Summer 2009.

Accessing Built-in Help in R

Accessing Built-in Help in R

There are several options for accessing Help in R.

To use Help files that are displayed in HTML, type help.start() in your R environment.

Using the Help Commands

Using the Help Commands

To access the built-in Help system in R, type help() on the command line, or type a question mark, ?.

To access Help for a specific command, include the command name in the parenthesis when you type the help command. For example, type help(quit) or ?quit to view the man page for the quit() command.

To page through the contents of Help, use the Up arrow and Down arrow keys on your keyboard.

To close the Help window and return to the command prompt, type q.

For example:

>help()
help package:utils R Documentation

Documentation

Description:

These functions provide access to documentation. Documentation on
a topic with name 'name' (typically, an R object or a data set)
can be printed with either 'help(name)' or '?name'.

Usage:

help(topic, offline = FALSE, package = NULL,
lib.loc = NULL, verbose = getOption("verbose"),
try.all.packages = getOption("help.try.all.packages"),
chmhelp = getOption("chmhelp"),
htmlhelp = getOption("htmlhelp"),
pager = getOption("pager"))

?topic

type?topic

:q
Using Search in Help Commands

Using Search in Help Commands

Other Help options, which use the search function, include the following:

  • Type help.search("<text>") to search for functions related to that text.

  • Type apropos("<text>") to find object names that include that text.

  • Type RSiteSearch("<text>") to search the R web site for documents that include that text.

Listing and Installing Packages in R

Listing and Installing Packages in R

This section describes how to view the list of installed packages in R, and how to install a new package for you to use locally.

A package is a collection or group of objects that R can use. A package might contain functions, data frames, or other objects, such as dynamically loaded libraries (DLLs) of compiled code. Often, the terms package and library are used interchangeably.

Listing R Libraries and Packages

Listing R Libraries and Packages

You can list the libraries or packages that are available in your R environment.

Listing Libraries

To view an alphabetical list of libraries available in R, type library().

For example:

> library()
Packages in library '/usr/lib64/R/library':

aaMI Mutual information for protein sequence
alignments
abind Combine multi-dimensional arrays
accuracyTools for testing and improving accuracy of
statistical results.
acepack ace() and avas() for selecting regression
transformations
actuar Actuarial functions
adabag Applies Adaboost.M1 and Bagging
adapt adapt -- multidimensional numerical integration
AdaptFitAdaptive Semiparametic Regression
ade4 Analysis of Environmental Data : Exploratory
and Euclidean methods in Environmental sciences
:q

To view documentation for a particular library or package type one of the following commands:

> library(help=<package name>)
> help(package=<package name>)

Listing Packages

To view an alphabetical list of packages available in R, type search().

For example:

> search()
[1] ".GlobalEnv" "package:methods" "package:stats"
[4] "package:graphics""package:grDevices" "package:utils"
[7] "package:datasets" "Autoloads""package:base"
>
Listing R Data Sets

Listing R Data Sets

To view an alphabetical list of the default data sets available in R, type data(). To view a list of data sets within a specific package, first load the package by using the library(<package>) command, and use the view data set command data().

For example:

> data()
Data sets in package 'datasets':

AirPassengersMonthly Airline Passenger Numbers 1949-1960
BJsalesSales Data with Leading Indicator
BJsales.lead (Bjsales)Sales Data with Leading Indicator
BODBiochemical Oxygen Demand
CO2Carbon Dioxide uptake in grass plants
ChickWeightWeight versus age of chicks on different diets
DNaseElisa assay of DNase
EuStockMarketsDaily Closing Prices of Major European Stock
Indices, 1991-1998
:q
Installing an R Package

Installing an R Package

The RCE provides almost all stable libraries maintained in the Comprehensive R Archive Network (CRAN) repository. The HMDC mirror site for CRAN is:

http://mirror.hmdc.harvard.edu/CRAN/

If you locate an R library that is not available in the RCE, contact us and request we install that library. When the we install a library, it is available for use by all RCE users.

To install a library on your local system:

  1. In R, type library(<package_name>).

    For example, to install R Commander, type the following:

    > library(Rcmdr)

    R prompts you with a warning if the package that you chose to install uses other packages that are not installed already.

  2. To install missing packages on which your target package depends:

    1. Click Yes to continue.

      The Install Missing Packages window is displayed.

    2. Click OK to continue.

      R prompts you to select a mirror site from which to download the packages' sources.

  3. Select a site from which to download the sources, and then click OK.

    The dependent packages and your target package are now installed. If it is an executable, the function is executed.

The sequence of activity that occurs in R by performing this installation is:

> library(Rcmdr)
Loading required package: tcltk
Loading Tcl/Tk interface ... done
Loading required package: car
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.us.r-project.org/src/contrib/RODBC_1.1-7.tar.gz'
Content type 'application/x-tar' length 79624 bytes
opened URL

downloaded 77Kb

* Installing *source* package 'RODBC' ...
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
...

The downloaded packages are in
/tmp/RtmpjJMJkz/downloaded_packages

Rcmdr Version 1.2-0
Locating R Directories

Locating R Directories

The following is the location of installed libraries and packages for the R application:

/usr/lib64/R/library

To identify the default directory from which you read input and to which you write output, type the command getwd().

For example:

> getwd()
[1] "/nfs/fs1/home/S/sspade"

You can write to a specific directory by using the following command:

> sink(<path and file name>)
IQSS