Surviving in the R Environment

R is not only a programming language but also a programming shell with read-eval-print loop (REPL). The shell is how most people use R. But when you drill deeper, knowing more about what’s working behind the scenes is handy. In this post, you will learn:

  • How to manage variables in R
  • How to manage packages in R

Let’s get started.

Surviving in the R Environment.
Photo by Claudio Schwarz. Some rights reserved.

Overview

This post is divided into three parts; they are:

  • The REPL in R
  • Getting Help in R
  • Package Management in R

The REPL in R

“R” is a command that gives you the REPL shell. If you put the following into a file, say, inverse.R:

and in the command line, run Rscript inverse.R, you will see the following printed into the screen:

The command “Rscript” (with the uppercase R) will run the R commands in a file line by line. If you run “R”, you will launch the shell, which you will see the prompt “>”. Similarly, you can also run RStudio, which it will run the “R” command on your behalf.

The prompt is for you to type the R command. If your command is incomplete by the end of the line, R will expect you to continue. Hence you will see the prompt “+” in such case:

In R, you can create variables in your current environment. For example:

then you will have variables “a” and “b” created. It would be easy for you to check what variables are defined in R if you’re using the RStudio. If you only have the REPL shell, you can check the list of variables defined using “ls()”:

Assigning new data to a variable will overwrite it, as in many other programming languages. If you have a variable that you don’t want, you can simply ignore it. But if you want to reclaim the unwanted variable’s memory, you must remove it using:

Getting Help in REPL

The syntax of R is sometimes obscure but it is not difficult to look for help from the Internet. For example, Stack Overflow has a dedicated tag for R related questions:

But if you’re in the REPL, you can also get online help, with either of the following:

Either will show you a full-screen help on a particular command (“ls” in this case). But if you forget what the command is, you can search with pattern:

Either of the above will show you a long list of all the functions (and a one-line description) that are related to “regression”. Note that if you’re using “help.search()”, you need to provide a quoted string, but “??” already assumed whatever follows it are strings.

The help in the above is very detailed but sometimes not helpful enough. You may see some examples of a particular function, by using:

The examples in this case can help you quickly get started. But if you need more details to understand every bits and pieces of a function, you must reference to the full documentation provided by the help() function.

Package Management in R

R is an extensible language. A lot of packages are written for R to provide new functions. Because the R community has a disproportionate number of researchers in mathematics and statistics, you may also find quite many research-oriented packages or implementations of cutting-edge statistical algorithms.

Like the CPAN for Perl and PyPI for Python, the packages in R are registered in the online repository CRAN. There are around 20,000 packages in CRAN. You can browse by their name at:

Given you know the name of the package, you can install them with install.packages("name"). To remove that package, you can use remove.packages("name"). Note that you need to provide the packge name as string in these two functions.

To find the list of all installed packages would be easy in RStudio. But in the REPL, you can use installed.packages().

To update the installed packages, you simply type update.packages() and R will ask you to confirm about the upgrade.

Note that having a package installed doesn’t mean R has to use it. To use a package, you need to load it or import it. In R, the syntax is:

which the name of the package is not quoted in calling the library() function. You can also see what functions are provided by a package using:

Summary

In this post, you learned how to interact with R. Specifically, you now know:

  • How to manage variables in R
  • How to get help on a function in R
  • How to install, remove, and update packages in R

No comments yet.

Leave a Reply