See, I told you that was the title.
R Packages
As I mentioned previously, after you download R, it does a fair amount of stuff. However, as you begin to want to do more, you need to install various packages, created by R users (many times, very prominent people), to make the software do what you want.
A package is kind of like a SAS macro, packaged into a file that integrates into the R DNA (its like a wonderful retrovirus). Packages can do one function, or can have the ability to do many, many different things (this is the case most of the time).
For example, if you want to export some high resolution figures as a PDF file (a good vector format for publication quality charts), you can install the package "Cairo".
To install packages, type the following into the R console (then hit enter) or into the R script viewer (then hit command+enter -- on mac):
install.packages("Cairo")
Yes, you need the quotes around the word Cairo.
Yes. You may have just realized that R is indeed case sensitive. This is a critical concept that is not the same in all software packages. R sees the word Cairo as different than cairo. If you use the lower case "c", you will get an error. If you dont use the quotes, you will get an error.
When you run that function, you will see a bunch of crap starting to show in the console window -- it is installing the package. Some packages are not available for certain versions of R. I find it critical to not update R to the latest version (this is why I said to avoid version 3 in my first post -- all of the packages need to be re-written in order for them to work on this version -- this is a lot of work for people that create packages so it may take some time to get certain packages to work in new versions). I am a chronic updater who always wants the latest version of everything, so it is difficult for me to wait... it is critical to do this though. You only have to install the package once on your computer.
Once you install the package, you need to open the package to allow R to use the functions within the package.
To open the package, type the following in the console or R script viewer:
library(Cairo)
This time, dont use quotes, but keep the capitalization for Cairo. You can also type the following (it works the same.. Im not sure why they both work or what the difference is):
require (Cairo)
You only have to open the library once per R session. Once you close R and open it back up, you will need to re-open the library with one of the above scripts.
Many packages have their own help files. Once you install the package, try to type the following (change the word Cairo to whatever the package name you want the help for):
help(Cairo)
You will see in the console that there is no specific help file for Cairo. You can then revert to the ??Cairo, as mentioned in the prior post.
help(base) does work, however. This is the help file for the base R application.
There are tons of packages for R, which allow R to do pretty much whatever you want. If it doesn't do what you want, you are probably just doing something wrong. Once you get skilled (ok Napoleon), if a particular package still doesn't do what you want, you can email the package creator -- you can usually find their email address by looking for the help file on google, as outlined in the prior post (the PDF file that I showed). I have done this before with very good results. Please don't overly bother package creators though. They spend a lot of time making these things, and don't need to be harassed by people who may or may not know what they are doing.
The most important part of this blog is to make sure that you always cite the packages you use in any manuscript or whatever you are publishing. To get the appropriate citation for the packages you are using, type the following in the R console (swapping out Cairo with the name of the package you want the citation for):
citation("Cairo")
Yup, this time you need the quotes. Confusing huh? Yeah, I still haven't quite figured out why quotes are needed sometimes and not others. Oh well. You can't 'em all - if you win any of them, be pleased.
Ok - I promise - next post is about reading data into R.
Arrivederci.
No comments:
Post a Comment