Getting Started with R: Packages, Help & Workspace (Part 2)

In the last article we learnt about downloading, installing and getting started with R. We will now learn about R packages, using the help system and managing your workspace.

Packages in R

The base R or the core R installation contains a plethora of functionality relating to common statistical analysis and visualization of data. These functions or commands are part of the core R platform and are available to you right after the installation.

In spite of providing most of the common functionality in the base or core R, the developers of R realized that there should be a way of plugging in specialized code into the existing R installation. To allow for plugging-in of extra code or data, R has a concept of packages, also called as libraries.

What is a R package?

A R package is an optional module that a user can download and install. A R package typically contains R functions, data and compiled code and enhances the functionality of an existing installation.

The directory where packages are stored is called the library. The function .libPaths() displays the location of directory on your device containing the packages. .libPaths() function is also used to change the location of your library.

R comes with a standard set of packages –

Managing R Packages

R packages enhance the functionality of the core R. They import new functions, datasets and objects in your workspace. Therefore, you need to learn how to manage R packages to be able to control the artefacts visible to your code. Typical actions with R packages are – searching, installing, updating and deleting.

Examining R Packages

You can use the  installed.packages() command to get details about all packages currently installed in your environment.

Note that just because a package is installed does not necessarily mean that it is  loaded and available. To get a list of all currently attached packages use the search() command.  You can use the .packages() command to get information about availability of packages on your search path.

Installing R Packages

The act of installing R packages implies downloading the contents of the package to the library directory on your computer.

You can install a package using the install.packages() command. install.packages() can be run with or without arguments. If you just execute install.packages() command you will be asked to choose a CRAN mirror as shown in the image below –

Once you choose a mirror, you will be asked to choose the package to install –

If you already know the name of the package that you require you can pass it as an argument to install.packages() as shown below –

Users on Windows and OS X platforms can also use the ‘Packages’ menu to install, update and load packages.

Loading R Packages

Installing a package downloads the package contents from the CRAN repository and places it in the library directory of your computer. If you want to use the installed package you have to load it in the current session. Once an installed package is loaded, its contents become available for using. An installed package can be loaded by issuing the command as follows –

If something goes wrong, R will raise an error –

Updating Packages

Every once in a while the developers of packages fix bugs etc. and update the contents of their packages. If you want to get to the latest version of a particular command, you can use the update.packages() command as shown below –

If you want to update all your packages in one go you can issue the following command –

You can get a list of all obsolete packages using the old.packages command as shown below –

Removing or Unloading Packages

If you have loaded some packages and want to remove one, perhaps to prevent shadowing of a particular command, you can use the detach() command as shown below –

For example, if you want to unload the Rstem package from the current session, you can say –

Once detached, a package is not totally deleted. It can be loaded again using library() or require() command.

Knowing More About a Package

Loading a package introduces new functions and datasets in your session. To be able to use these new artefacts effectively, you can access the help system. To get help on a particular package you can issue the command such as –

For example, you can look at the help for package Rstem by issuing the following command –

Getting Help in R

R comes with a pretty sophisticated help system as a part of the installation. You can access the help file for a particular command or function using the help function. The general syntax is:

For example, to know more about the mean function you say

There is a handy shortcut for the help function and you can use a ‘?‘ instead of help.

To know more about the arguments that a function expects use the following format of command –

For example, you can quickly check the arguments of the mean function as follows –

You can look at the sample usage of a R object using example function as follows –

To search for a particular string across all the help files you can use help.search(“string”) function. For example, to know which functions have capability to perform logistic regression, you can issue the following command –

The way ‘?‘ is a shorthand for help function, ‘??‘ is a shorthand for the help.search function. Therefore, you can equivalently say –

You can search for part name of a function using the apropos command as shown below –

The apropos command has listed all R objects that contain “norm” in their name. In case there is a no-match, R returns character(0) to signify an unsuccessful search –

The function help.start() opens a browser window with deault help’s home page.

Managing Your Workspace

As discussed in Getting Started with R, R is an interaction-oriented programming language. It allows and even encourages running short chunks of code, gathering the output and processing it further. There are a handful of commands that you should know to be able to better manage your workspace.

Getting and Setting Working Directory

Your working directory holds all artefacts pertaining to your current project. When you change your project you want to change your working directory. The getwd() and setwd(“Abs or rel path”) functions allow you to get and set the value of current working directory.

Consider the example below –

Note that R uses only forward slash(/) as path separator and this applies even for Windows. As far as R is concerned back slash(\) is used to escape characters. If you want to use back slash, you ought to use two of them as shown in the example above.

Windows and OS X users can use the menus in RGui window.

On Windows select File → Change dir

On Mac select Misc → Change Working Directory

Saving Workspace

You can save the current state of your workspace (barring the graphs) using the save.image() command. The snapshot of contents of your workspace gets saved in .RData file inside the working directory. This .RData file is used to restore the workspace at a later point in time.

Accessing Result of Previous Command

R has a special variable by the name .Last.value , this variable stores the value of last command as shown below –

Note that .Last.value stores the value TRUE after the evaluation of expression. .Last.value can be used for comand chaining, a programming pattern where the output of one command is passed to the next for further processing.

Peeking at Entities in Workspace

At times you would want to know about the objects (functions & data) available in your current environment. This comes in especially handy when you end up with a nasty name conflict.

The ls() command lists names of all functions and other objects currently visible in the workspace. This command may take from zero upto six arguments. It might be worth your time to go through help(ls).

Removing Entities, Exiting & Going Home

If you want to remove a particular object from the workspace, you can use the rm() command. The rm command is a shorthand for remove command.

Sometimes you reach a point where you had had enough and would want to start over by erasing everything you have so far –

And of course, there are times when limits are breached, here is how to tackle that in R 🙂 –

You will be prompted for saving the workspace, do as you please and hurry home.

This concludes are two part tutorial of Getting Started with R. We will next look at the data types in R and how to use the command line effectively.

Leave a comment

Your email address will not be published. Required fields are marked *