R Packages
1. R Packages
Because you've learned so much in this chapter by now, let us do a brief recap. First, you've learned about the different ways to use R's built-in functions. Next, you took full control by actually creating your own R functions. You can use these newly defined functions just as we use R's built-in functions like mean, list and sample, just to name a few.2. R Packages
But, wait? How come these functions are 'built in'? These functions are not in my workspace, so how on earth does R know where to find these functions mean, list and sample? Well, all of these built-in functions are part of R packages that are loaded in your R session. R packages are bundles of code, data, documentation, and tests that are easy to share with others. For example, the mean, list and sample functions are all part of the base package, which contains the basic functionality to use R. Another example package, specifically for data visualization, is the ggvis package. It contains functions such as layer_points, scale_nominal and add_axis.3. Install packages
Before you can use a package, you will first have to install it. The base package is automatically installed when you install R. The ggvis package on the other hand won't come with the bundled R installation. But fear not! You can easily install it from inside R, using the install (dot) packages function, which, by the way is a function of the utils package. This function goes to CRAN. CRAN is short for the Comprehensive R Archive Network, a repository where thousands of packages are available. The function downloads the package file and installs the package on your system. All of this is done with this single command in R, pretty cool right? We've now installed the ggvis package, but we can't use it yet.4. Load packages
To do that, we'll have to actually load the package into our current R session. When R loads a package, it actually attaches it to the search list. This is a list of packages and environments that R looks through to find the variable or function you want to use. To have a look at this list, you can use the search() function. Whenever you execute code that depends on any other variable or function, R goes through all these packages one after the other to find it. Apart from all the packages that are loaded into our R session, we also see ".GlobalEnv"; this is our own workspace, where the user-defined R objects live. You can also see that the search() path already contains a bunch of packages. When R is started, it loads 7 packages on default, among which is the base package. Others are the utils, datasets and methods package. That's why, when you start R, you can use the mean function, or the install (dot) packages function without having to explicitly load the package. The ggvis package that we've installed earlier, however, won't be loaded automatically. If we try to access the ggvis function, for example, R will return an error, telling us that the function ggvis could not be found. That's because ggvis is not yet in the search list.5. Load packages: library()
To access ggvis' functionality, we'll have to load the package using the library command. This command takes the name of the package and adds the package to the search list, right after the global environment, making all the functions, data and pre-compiled code it contains available and ready to be used. We can check this by running search() once more. Indeed, ggvis is now part of the search list. If we now execute the same command, a pretty awesome graph shows up! Now R could find the function in the list of attached packages and use it to create this nice plot.6. Load packages: require()
Before you proceed to the exercises, I want to tell you about the require function. Just like the library function, require loads packages into your R session. The only difference appears when you're trying to load a package that is not yet installed. Let's say you want to load the data (dot) table package, a package to perform data manipulation, but that this package is not yet installed. If you call library(data (dot) table). R throws an error in this case. However, when you execute require(data (dot) table), you get a warning.7. Load packages: require()
Also, the result of this require function will be FALSE if attaching the package failed. This is a good alternative when you want to avoid errors, for example when you're attaching packages dynamically inside functions.8. Wrap-up
So to wrap up: the install (dot) packages() function installs packages for you, while library() and require() load them for you. And when you load packages, you're attaching them to a search list, making them available in your current R session. Before you start writing your own functions, first do a quick search for packages that do the same thing. You can simply install and load the package, have a look at the documentation and avoid having to rewrite a bunch of code that's already been written. I'm not saying that you should never write your own functions, but for common problems such as data manipulation or visualization, there are some pretty neat packages out there that will get you up and running in no time.9. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.