R
Packages
R
is based on the functional model. In R the functional model is simple.
Everything is either a function or a storage object. Packages are used
to collect functions in a container that represents a specific purpose
or similar functionality. The R graphics package contains many of the
commonly used functions for charts and plots [https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/00Index.html].
Putting these functions into a single package makes it easier to manage
all of these functions.
The
base installation of R comes with many of the commonly used packages.
Several of these packages are loaded and are ready to use when you start
RStudio. you can view the packages installed on your computer in the
Packages panel of RStudio.
Figure
1 - RStudio Packages Panel
Figure
1 shows the list of installed packages. Those
packages with check marks by their names are loaded and ready to use.
The other packages, even though they are installed on your computer,
must be loaded into R before you can use any of the functions in a given
package.
You
can load an installed package with the command [in the command
panel]:
library(package-name)
If
you wanted to load the MASS package, you would use the command:
library(MASS)
Another
way to load a package is to simply check the box next to the package
name in the Packages panel in RStudio. When you use this method, you
will notice that RStudio enters the Library( ) command
in the Command console when you check the package box.
Once
a package is loaded, you have access to all of its functions during your
R session. If you do not load a package and try to use one of its
functions, R will return an error message telling you that it cannot
find the function.
Installing
packages in R
While
many useful packages come with the base R installation, eventually you
may want to use the functionality of a package not currently installed
on your computer. If you wish to do this, you will need to install the
package from CRAN and then load it into your R session.
You
can install a package for the command prompt in the Command console with
the command:
install.packages("package-name")
Several
things are worth noticing in the syntax of this command. The function
name contains a period dividing the two words install and packages. The
word packages is plural even though you can use this to install only one
package. The package name, inside the parentheses, is enclosed in
quotation marks. The package name is case-sensitive, so if the package
name is ClassMaster, entering "classmaster"
will not find the correct package. Yes, you can install multiple
packages with a single command, but the command syntax becomes more
complex, therefore it is better to install a set of packages one by
one.
Another
way to install packages is to use the Install Packages functionality in
RStudio. Simply click the Install button in the
toolbar in the Packages panel. When you do this, an Install Packages
dialog will open.
Figure
2 - RStudio Install Packages Dialog
Figure
2 shows what this dialog will look like. You can begin typing the name
of the package you want to install into the Packages text entry box. As
you begin typing, a completion box will appear showing the packages that
match your typed text.
Figure
3 - RStudio Install Packages Lookup
As
you can see in Figure 3, the Install Packages dialog begins to recommend
packages that begin with the letter c. One advantage of this method is
that it accounts for the correct capitalization of package names. This
makes it easier to find the desired package and install it.
Once
you find the package you want to install, select it from the
recommendation drop down. Its name will appear in the Packages entry box
and you can click the Install button. RStudio will
install the package and any other packages that this package is
dependent for functionality that are not currently installed on your
computer. Notice that RStudio enters the install.packages( )
command in the Command console when you click the Install button.
Once a package is installed, it will be visible in the list of packages
in the Packages panel of RStudio and it can be loaded into your R
session.
The
RStudio Install Packages dialog allows you to install multiple packages
at the same time. After you have selected the first package that you
want to install, simply type a comma and begin searching for the next
package you want to install. The dialog behavior is the same as
before.
The
above description discusses how to load CRAN packages. The procedure to
load non-CRAN packages will be left for a later discussion.
Updating
packages in R
R
packages, like R itself, are sometimes updated to improve functionality
or modify operation. It is advisable to occasionally update the packages
installed on your computer. The command line method to update R packages
is complex and unnecessary if you are using RStudio.
You
can update your installed R packages in RStudio by clicking the Update
button in the toolbar in the Packages panel. When you do
this, RStudio verifies which installed packages need to be updated and
displays them in a dialog.
Figure
4 - RStudio Update Packages Dialog
You
can Select All to update all of the identified
packages or update only specific packages. Once you have made your
selections, simply click the Install Updated button.
RStudio will update the packages. Notice that RStudio does this by
sending the appropriate commands to the command console for execution.