Get startedGet started for free

Packages

1. Packages

Packages are part of what make R special.

2. Packages

A package is a group of functions that all work together to perform related tasks. For example, a package named ggplot2 is incredibly popular for creating beautiful graphics in R.

3. Packages

Another example is the quantmod package, which contains a number of functions related to financial analysis. Because R is open-source, packages have been written by people all over the world, and have been submitted to CRAN, the Comprehensive R Archive Network, so that other individuals like you and I can use their code. Right now there are more than 10000 packages on CRAN for just about any type of analysis you can imagine.

4. CRAN

So, what is a function? You have used dozens of functions in R already, mean, plot, and ncol are all examples of them, but how were they created and what are they doing? Essentially, good functions are lines of code written to do a few key things. They accept arguments, the input to the function or the options that can be controlled by them. They have a body where the code is executed, and that body performs one thing well. By that, I mean that a single function does not try and do an entire analysis, but is instead a piece of the analysis that works together with other functions. They also return a value, and the class of that returned value is predictable and stable. Defined in this way, functions are building blocks for analysis. In fact, every operation in R is at some level a function call. After this chapter, you will feel comfortable with learning to use new functions, and write your own simple functions for financial analysis. For a more in depth explanation of functions, check out the DataCamp course Writing Functions in R, by Hadley Wickham.

5. Installing packages

To download a package from CRAN, you use a function in R called install-dot-packages. For example, if you wanted to download the quantmod package, you would type, install-dot-packages, then quantmod in quotes. This downloads all of the functions, documentation, and any example datasets for the quantmod package from CRAN and places them onto your local machine. A separate step from downloading the packages is actually loading them into your current R session so that you can access their functions. The easiest way to do this is through the library function. Typing library, quantmod, will load all of the quantmod functions into your session for use. Each time you start up R, you will have to library the packages you want to use.

6. quantmod functionality

For an example of what packages can do, you can use the getSymbols function from quantmod to download stock data from Yahoo finance. Let's use it to download IBM stock data. getSymbols automatically saves the data into your workspace by default. Check out the first few rows using head. Pretty cool right? You can even plot the data for nice looking charts. Packages are your way of using and building off the work that thousands of other developers have already put out there.

7. Let's practice!

In the next exercise, you will explore another financial package, tidyquant, to see another way to download and manipulate financial data.