R packages and the environment

Overview

Teaching: 10 min
Exercises: 10 min
Learning Objectives
  • Be able to identify installed packages and install new packages in R

  • Distinguish between namespaces and packages

R is designed to allow you to work with data, but there are thousands of functions and objects available. How do you prevent conflicts? How do you limit yourself to just the ones you want.

Your working environment

Open up an R console and type ls()Enter. Examine the results. Now type search()Enter. Examine those results.

Solution

ls()
[1] "args"          "dest_md"       "missing_pkgs"  "required_pkgs"
[5] "src_rmd"      
search()
 [1] ".GlobalEnv"           "package:knitr"        "package:requirements"
 [4] "package:remotes"      "package:stats"        "package:graphics"    
 [7] "package:grDevices"    "package:utils"        "package:datasets"    
[10] "package:methods"      "Autoloads"            "package:base"        

Your results will likely differ from mine.

The ls() function, like ls in bash, lists the contents of the curent environment, probably the “global environment” . But your working environments is only part of what you have access to as an R programmer. There are other environments attached to your session, and you can see those environments by the results of search().

What happens when you type ls()

If you get help on this function with ?ls, you will see that it’s listed as belonging to the base namespace. Look back at the results of your search and you’ll see a namespace called base. When you type ls(), the R interpreter searches along the list of environments, from the working environment down to the end of the list, for the first function called ls(). It then executes that function.

Find out!

Look at the output of search() again, and note the position of the base package. Now use that number in the pos argument of ls, e.g., ls(pos=n).

Solution

search()
 [1] ".GlobalEnv"           "package:knitr"        "package:requirements"
 [4] "package:remotes"      "package:stats"        "package:graphics"    
 [7] "package:grDevices"    "package:utils"        "package:datasets"    
[10] "package:methods"      "Autoloads"            "package:base"        
ls(pos=12)[680:690]
 [1] "log1p"             "log2"              "logb"             
 [4] "logical"           "lower.tri"         "ls"               
 [7] "make.names"        "make.unique"       "makeActiveBinding"
[10] "Map"               "mapply"           

The base package contains a lot of objects; I’ve shortened my results for display. But what you can see is that the 12th item in the search() list (the “base” package) contains a function called “ls”. When you type “ls()” this is the function that runs.

Loading R packages into your namespaces

R objects and functions come in packages. To add an installed package to your namespace, you must use the function library(). Try adding the lattice package to your namespace and see where it resides.

library(lattice)
search()
 [1] ".GlobalEnv"           "package:lattice"      "package:knitr"       
 [4] "package:requirements" "package:remotes"      "package:stats"       
 [7] "package:graphics"     "package:grDevices"    "package:utils"       
[10] "package:datasets"     "package:methods"      "Autoloads"           
[13] "package:base"        

By default, library() loads packages into the second position of your search path, just behind your global environment. This means that the most recent packages loaded are found earliest, and thus given priority. library() has an argument to position the newly loaded package at any position.

Installing packages

There are thousands of packages on [CRAN][cran], but until they are installed on your local computer, you can’t load them into your namespace with library(). To install a package from CRAN, use install.packages() with the name of the package as a string.

Install the package “devtools” this way.

When you have installed devtools, you can confirm using the following

"devtools" %in% rownames(installed.packages())
[1] TRUE

Fully qualified namespaces

If you have installed but not loaded an R package, you can still refer to the functions and objects available from the package. To do so, you have to use the fully qualified namespace for the object or function. For example, if you wanted to use the wireframe() function from package lattice, you could type lattice::wireframe(). So long as the package is installed it can be used this way without loading it with library(). Using fully qualified names is helpful in programming R scripts, to guarantee using the function from the correct package

l—

Key Points

  • R functions and objects are stored in packages

  • A user has access to all objects in all installed packages

  • Loading a package brings package objects into the user namespace, which is searched