(1) Loading a library is necessary
to use the downloaded and installed package. Remember, the terms
package and library are used synonymously throughout R
literature; however, one could say a package becomes a library
when it is loaded. In the previous set of notes, we downloaded
and installed the Hmisc package (hopefully you have all packages
by now). To use a package, we must now load it. This is a very
simple procedure; in the console, simply type the following and
hit enter.
library(Hmisc)
Any downloaded and installed package can be
loaded by typing: library(name) where ‘name’ refers to the
package name. Occasionally, when a package loads, it will ‘mask’
objects in other packages and the R console will return a
message for each library loaded. As an example, consider the
following; package Hmisc will mask the object ‘recode’ in
package car if car is already loaded. This bears watching
because; it can be extremely frustrating when attempting to use
a familiar function which will no longer work because a newly
loaded package is masking it. The good news is that this does
not occur frequently even when using many libraries (e.g.
multiple libraries can be loaded and used simultaneously). For
this reason, it can be important and preferable to clean up
after one’s self by using the ‘detach’ library command for
libraries which are not being used.
(2) Detach a library. Again, it is
extremely easy to detach a library which is no longer needed.
Simply type the following and hit enter:
detach(“package:Hmisc”)
Common errors result from forgetting the
parentheses and/or quotations, as well as forgetting the colon
between package and the library name. As an exercise in ensuring
we are doing what we think we are doing, type the following and
hit enter:
search()
The search function shows all the loaded
libraries. Notice, Hmisc is not listed. Now hit your up arrow 3
times and then hit the enter key. Notice, the up arrow scrolls
through your previous commands and the third command up should
have been ‘library(Hmisc)’. When you hit the enter key, you
should have loaded that library again. So, if we hit the up
arrow twice, we should see the ‘search()’ command and if we hit
enter again, we should see that Hmisc is listed as a loaded
package. Also notice that the ‘base’ package is loaded; which as
the name implies is part of the base install of R and is loaded
when the program R is opened/started.
(3) Preloading libraries. It can be
very convenient to have one or a few libraries load when you
start R if you tend to use these libraries during every session.
This is the same rationale behind the base library being loaded
each time R starts; everyone uses it every time
they use R. You can customize R to load certain libraries upon
start up by locating a file called “Rprofile.site” which by
default is located in:
C:\Program Files\R\R-x.xx.x\etc
where x.xx.x refers to the version number
of the R installation you are using (e.g. R-2.14.0) and yes,
there is an ‘etc’ folder. Once you locate the Rprofile.site
file, you can open it in Notepad which by default is available
with each installation of Windows. Here is where you can tell R
to load certain libraries upon start up. As an example, you can
see my current Rprofile.site file
here. One thing to notice in my Rprofile is the line:
setwd("c:\\Documents and Settings\\jons\\Desktop\\Work_Stuff\\Jon_R")
which sets the working directory
for R. This tells R where to start looking when ever you go to
File and open or save in R. In other words, if you wanted to
open or save something from the R console, it will start with
the specified working directory. You can set your working
directory to be any folder on your computer.
*Important Note: If you change your
Rprofile to preload libraries at start up, it will be necessary
to cut and paste the Rprofile file from the ‘etc’ folder
to some other location prior to starting R when you want
to update packages. For instance, after having R and all
packages downloaded and installed for a week, you will likely
want to check for updates. Then, go to the ‘etc’ folder and cut
the Rprofile file out and paste it to your desktop. Then, start
R and update packages/libraries as mentioned previously. Yes,
the program will function without that file. After updating all
packages, then close R. Then, cut the Rprofile file off your
desktop and paste it back into the ‘etc’ folder…then restart R.
The reason you would need to do this is because, R will not
update packages/libraries that are in use (i.e. loaded). So, if
you have your Rprofile set to load some libraries at start up,
you would never be able to update those libraries.
So, you’re probably wondering what’s in a
library? We’ll answer that in the next set of notes.