Install R Packages

Suppose we wish to install package car knowing that it's not in the library list (by issuing command library())

(package01.png here)

To install an R package, do PackagesInstall Packages from CRAN For the first time, the user is prompted to choose a mirror site for download, so choose one that is near your location. Then you're prompted with package that you wish to download.

(package02.png here)

Session Output

> utils:::menuInstallPkgs()

--- Please select a CRAN mirror for use in this session ---
also installing the dependency 'leaps'

trying URL 'http://cran.us.r-project.org/bin/windows/contrib/2.4/leaps_2.7.zip'
Content type 'application/zip' length 73318 bytes
opened URL
downloaded 71Kb

trying URL 'http://cran.us.r-project.org/bin/windows/contrib/2.4/car_1.2-1.zip'
Content type 'application/zip' length 708700 bytes
opened URL
downloaded 692Kb

package 'leaps' successfully unpacked and MD5 sums checked
package 'car' successfully unpacked and MD5 sums checked

The downloaded packages are in
        C:\Documents and Settings\WMUFaculty\Local Settings\Temp\RtmpD5Hprb\downloaded_packages
updating HTML package descriptions

Note that, by default, R check for dependencies of the intended package and install as well other packages that the intended package depends on.

Now check for library list again:

(package03.png here)

To use the library first note that R preload some standard packages together with user's folder (that stores objects the user created or about to create). They are arranged in standard order known as search path so that R identifies objects along this path. The user's folder is known as .GlobalEnv. The following is an example session on using the package car just installed.

> search() # List folders in search path

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     

> library(car) # load library car

> search() # Now, list it again

 [1] ".GlobalEnv"        "package:car"       "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"     

> objects(2) # List objects in folder 2 (i.e. package:car)

... some entries here were removed ...
 [49] "hccm"               "influencePlot"      "inv"               
 [52] "is.aliased"         "Leinhardt"          "levene.test"   
... more ...

> levene.test # function for Levene test

function (y, group) 
{
    meds <- tapply(y, group, median, na.rm = TRUE)
    resp <- abs(y - meds[group])
    table <- anova(lm(resp ~ group))[, c(1, 4, 5)]
    rownames(table)[2] <- " "
    attr(table, "heading") <- "Levene's Test for Homogeneity of Variance"
    table
}

> # To use it for fiber data for instance, do

> # with(data=fiber, levene.test(strength, as.factor(cotton)))

> detach(2) # Detach (unload) folder 2 (i.e., package:car)

> search() # Now, display search path again.

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     

> # Some functions are created with name space and can be

> # accessed by using the operator :: without loading the library.

> # It can virtually be considered as an abstract container providing

> # context for the items it holds so that items having the same

> # name residing in distinct namespaces are unambiguous.

> # For instance, surgery::knife and butchery::knife

> # are two different tools (!?)

> car::levene.test

function (y, group) 
{
    meds <- tapply(y, group, median, na.rm = TRUE)
    resp <- abs(y - meds[group])
    table <- anova(lm(resp ~ group))[, c(1, 4, 5)]
    rownames(table)[2] <- " "
    attr(table, "heading") <- "Levene's Test for Homogeneity of Variance"
    table
}

> # So, without loading package:car, do the following for the fiber data

> # with(data=fiber, car::levene.test(strength, as.factor(cotton)))

> require(car) # Another way to load the package

Loading required package: car
[1] TRUE

> search() # Now, display the search path again.

 [1] ".GlobalEnv"        "package:car"       "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"