Installing packages and getting help in R
If you are a new user and have never launched R, you must definitely start the learning process by understanding the use of install.packages()
, library()
, and getting help in R. R comes loaded with some basic packages, but the R community is rapidly growing and active R users are constantly developing new packages for R.
As you read through this cookbook, you will observe that we have used a lot of packages to create different visualizations. So the question now is, how do we know what packages are available in R? In order to keep myself up-to-date with all the changes that are happening in the R community, I diligently follow these blogs:
- Rblogger
- Rstudio blog
There are many blogs, websites, and posts that I will refer to as we go through the book. We can view a list of all the packages available in R by going to http://cran.r-project.org/, and also http://www.inside-r.org/packages provides a list as well as a short description of all the packages.
Getting ready
We can start by powering up our R studio, which is an Integrated Development Environment (IDE) for R. If you have not downloaded Rstudio, then I would highly recommend going to http://www.rstudio.com/ and downloading it.
How to do it…
To install a package in R, we will use the install.packages()
function. Once we install a package, we will have to load the package in our active R session; if not, we will get an error. The library()
function allows us to load the package in R.
How it works…
The install.packages()
function comes with some additional arguments but, for the purpose of this book, we will only use the first argument, that is, the name of the package. We can also load multiple packages by using install.packages(c("plotrix", "RColorBrewer"))
. The name of the package is the only argument we will use in the library()
function. Note that you can only load one package at a time with the library()
function unlike the install.packages()
function.
There's more…
It is hard to remember all the functions and their arguments in R, unless we use them all the time, and we are bound to get errors and warning messages. The best way to learn R is to use the active R community and the help manual available in R.
To understand any function in R or to learn about the various arguments, we can type ?<name of the function>
. For example, I can learn about all the arguments related to the plot()
function by simply typing ?plot
or ?plot()
in the R console window. You will now view the help page on the right side of the screen. We can also learn more about the behavior of the function using some of the examples at the bottom of the help page.
If we are still unable to understand the function or its use and implementation, we could go to Google and type the question or use the Stack Overflow website. I am always able to resolve my errors by searching on the Internet. Remember, every problem has a solution, and the possibilities with R are endless.
See also
- Flowing Data (http://flowingdata.com/): This is a good resource to learn visualization tools and R. The tutorials are based on an annual subscription.
- Stack Overflow (http://stackoverflow.com/): This is a great place to get help regarding R functions.
- Inside-R (http://www.inside-r.org/): This lists all the packages along with a small description.
- Rblogger (http://www.r-bloggers.com/): This is a great webpage to learn about new R packages, books, tutorials, data scientists, and other data-related jobs.
- R forge (https://r-forge.r-project.org/).
- R journal (http://journal.r-project.org/archive/2014-1/).