Wednesday, October 21, 2015

How to view all functions and other details of a package in R

While we are working with some packages in R, we sometimes want to know other functions from that package that may be available at our disposal.

Note: Make sure that the package is installed. Else, you may get an error that the package was not found. 

If you get that error, make sure to install the package. For installation, refer to the post: Installing and loading packages

In order to bring up information on any given package, use the following command:

search(help = package_name)

For example:

search(help = xgboost)

If you are using R (and not R Studio), this command will open a new window that would provide high level documentation about the package. The information is mainly divided into two sections: Description and Index.

Description provides general information about the package. Index provides names of all the functions that are available in the package.

Once we have the list of functions available, we can find more information about a particular function using the following command:

?function_name

For example:
?xgb.save()

This command will open up web page that provides detailed documentation about the function we need help with.

Now that we know how to install and view details of any given package, we would learn about some housekeeping in R next in the series.


Tuesday, October 20, 2015

Installing and loading packages

Packages in R are libraries of built in functions and possibly some data sets. Packages are helpful since we do not have to write all functions from scratch as they are already written by some author for use.

In order to install packages in R, we use the following command:

install.packages('package_name') / install.packages("package_name")

Remember to put the name of the package to be installed within quotes

For ex:

install.packages('randomForest') / install.packages("randomForest")

If you are using R, the R software will prompt you to select a CRAN mirror. You can choose any one and R will automatically download and install the package for you provided that package exists in the CRAN library. 

Sometimes, source file does not exist in CRAN library. In that case, you can try to find the zip / tar file of the relevant package and download it on your machine. Then, you can install the package using following command:

install.packages('path_to_file', repos = NULL, type = 'source')

For example, if I have downloaded random forest package file named randomForest.tar.gz from the web, I can install random forest package as follows:

install.packages('/Users/admin/RPackages/randomForest.tar.gz', repos = NULL, type = 'source')

Once the package has been installed using via CRAN network / source, we need to 'load' the library to make sure that all the functions and data sets of the package are available to work with. For this, we use the following command:

load(packageName) 

Remember that we do not enclose package name within quotes while loading the package. We only do that while installing the package. 

For example:
library(randomForest)

Next in the series, we will see how to call datasets from packages in order to work with them.