The basic installation of R is equipped with many functions, that allow us to import data, performing transformations, adjustment and evaluation of statistical models, graphic representations, … But, In addition to the functions “serial” we can add many more (miles). At any time we can incorporate the new features we need.
A package (package) is a collection of functions, R data and code that are stored in a folder according to a well defined structure, easily accessible for R.
Web-R can consult the list of available packages. Also in the section Task Views You can find a list sorted according to areas of application packages.
To view the list of packages we currently have installed on your computer only we have to run the command library() on the command line.
We can also access the list by clicking on the tab packages:
It is important to distinguish between having a package installed on your computer and have it loaded in memory:
It installed on your computer simply means that at some point we have downloaded from the internet and we have copied to a directory where you can locate R.
Having it loaded into memory means that during our working session R has read the contents of the package and has incorporated the functions containing your workspace, so that such functions can be invoked and executed and.
Looking at the picture above we see that in the list of packages that presents rstudio, there is a checkbox for each package. Only marked packages are currently loaded into memory. The rest is simply saved in some directory, without their functions are still available to be executed from R. We can see a complete list of packages currently loaded into memory with the command search():
If you click on any of the packages list, Support Package tab appears Help.
To install a package just simply run the command install.packages , with the package name in quotes as an argument. For example, to install the package lattice we wrote:
install.packages(“lattice”)
Another option is to click on the tab install:
In both cases, R is connected to one of the repositories CRAN (Comprehensive R Archive Network) In Internet, download the file containing the package, decompresses and installs in our directory default package.
Once downloaded and installed the package is necessary to do -only once-, to use the functions needed to load memory contains by:
>library(“lattice”)
O, equivalently, through:
>require(“lattice”)
We can also do so by clicking on the check box next to the name d Package:
If a package has already been previously loaded, rerunning library(“Package name”) O require(“Package name”) It has no effect, since R checks whether the packet is in memory and in that case no reloads.
Important: When clicking on the check box next to the package name, the command library() It is running on the console, but not copied to the script (if we were writing one). If we are developing a script that will be reused later and which we will use some library functions included in, It is important that the command library(“library name”) remain written in the script, because if it is not, When you run that file in a new session of the program will not recognize R functions not previously been loaded into memory library.
Differences between library() y require()
The only difference between these two functions is that require() It has been designed specifically for use within functions:
require(“package”) returns TRUE or FALSE depending on whether or not the package installed on the user's computer. further, if there loads it into memory, and if there is no launches a "warning" warning it. Thus, the programmer can use the function require() to check whether or not the user has the required packages to run other commands included in the function, without this interruption. The typical use is require, well, from the way:
miFunction <- function(arguments){
if (!require('PaqueteNecesario'))
stop(“You must install the 'paqueteNecesario package’ to continue”)
code-de-la-function
}
library(“package”), Conversely, if it detects that the requested package does not exist, throws an error and stops the ongoing process.