1.3 Install and load packages - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
Video transcript
While R by itself is an amazing tool, it's really the packaged ecosystem that gives it so much power. There are thousands of packages written by people ranging from professors, to corporations, to individuals that extend the functionality of R by leaps and bounds. Now, using R Studio, we can come to the Packages pane, and see all the packages we already have installed. So the easiest way to install packages in our studio is to click the Install Packages button. (mouse clicks) In here, you can type the name of the packages you want. For instance, ggplot2. (keyboard taps) And coefplot. (keyboard taps) Now notice, if you want to install multiple packages, just separate them by spaces. Now both ggplot2 and coefplot depend on other packages. That is, in order to use ggplot2, you need to also have the plier package installed and the reshape2 package installed. Fortunately, R is smart, and installs those automatically when you install these packages. So you can go ahead and click Install. And that sends a command to the console saying, "install.packages," and "ggplot2" and "coefplot." Now, don't worry about these warning messages. They are just warning messages. It means the computer's having a bit of trouble finding the right server, but it gets it. And also, do not worry about the red text. The red text is just the command being run. It does not indicate an error or anything like that. So when installation is done, it tells you the package was successfully unpacked, and they're now ready to be used. Now just because they're installed doesn't mean they're available to you at that moment. In order to use a package, you need to load it. And if you want to use your mouse, you can scroll down until you find the package you want, such as ggplot, and click the little check box next to it. As you can see it enter the command, library("ggplot2", and now that package is usable. (mouse clicks) Let's say we want to type it. (keyboard taps) So, we could, as it showed, type in library(ggplot2. Notice I don't have quotation marks. You could either put the package name in quotation marks, or without them, it works either way. (keyboard taps) Another command which I actually prefer, is require, and we'll say coefplot. Require does the same exact thing as library. It just has a different name. Both work equally well. There are subtle differences in what value they return, but that really shouldn't make too much of a difference. (keyboard taps) Now that ggplot2 and coefplot have both been installed, the functions that they contain are ready to be used like any other function. For instance, in ggplot2, the main function is ggplot. And then, you hit Control + Enter of just the function name. It brings up the code that makes up that function. So now, as we go forward, anytime I start using a new package, you can easily install it using the Install.Packages command, or you can type in install.packages, then in quotes, the name of the package, such as useful. (keyboard taps) And hit Control + Enter, and it installs it. Now, sometimes when you install packages in R, there's a great R Studio feature. They suggest that you should restart R before installing the package, and it will do that all for you automatically. It's just a way of making sure things are clean. So we go ahead and click yes. R restarts, and the package is installed again from CRAN. Again, don't worry about these warning messages. You may or may not see them. It just has to do with the way that R is looking for its mirror, and right now we're seeing a progress bar, which we didn't see earlier, and that's probably because we're on a slower internet connection. It's installed and ready to use, and we can go ahead and call it by typing in require(useful). (keyboard taps) Just like we did before, and that loads up the packages. Now I've noticed this. When I required useful, it loaded up useful, and it also loaded up ggplot2. That's because useful depends on ggplot2, so it loads both. And we can confirm this by coming over to the packages, and seeing that ggplot2 was loaded. And remember, we just did a restart. And every time you restart R, all the packages that were loaded are no longer loaded. So to see this, I'll come to Session, Restart R. (mouse clicks) And we see ggplot2 is no longer loaded. And that's important to remember. Anytime you want to use a package, if R has been restarted, you need to load that package again. So going forward, anytime we want to use a package, we can install it, and once it's installed, just type in require, the name of the package, and we're good to go.