In R language, the packages can have namespaces, and currently, all of the base and recommended packages do except the dataset packages. Understanding the use of namespaces is vital if one plans to submit a package to CRAN because CRAN requires that the package plays nicely with other submitted packages on CRAN.
Namesspaces in R Language
Namespaces ensure that other packages will not interfere with your code and that the package works regardless of the environment in which it’s run. In R Language, the namespace environment is the internal interface of the package. It includes all objects in the package, both exported and non-exported to ensure that every function can find every other function in the package.
For example, plyr and Hmisc both provide a function namely summarize(). Loading plyr package and then Hmise, the summarize() function will refer to the Hmisc. However, loading the package in the opposite order, the summarize() function will refer to the plyr package version.
To avoid confusion, one can explicitly refer to the specific function, for example,
Now, the order in which the packages are loaded would not matter.
Namespaces do three things:
- Namespaces allow the package writer to hide functions and data that are meant only for internal use,
- Namespaces prevent functions from breaking when a user (or other package writers) picks a name that clashes with one in the package, and
- Namespaces provide a way to refer to an object within a particular package
In R language, two operators work with namespaces.
- Doule-Colon Operator
The double-colon operator:: selects definitions from a particular namespace. The transpose function t() will always be available as the base::t because it is defined in the base package. Only functions that are exported from the package can be retrieved in this way.
- Triple-Colon Operator
The triple-colon operator ::: acts like the double-colon operator but also allows access to hidden objects. Users are more likely to use the getAnywhere() function, which searches multiple packages.
Packages are often interdependent, and loading one may cause others to be automatically loaded. The colon operators will also cause automatic loading of the associated package. When packages with namespaces are loaded automatically they are not added to the search list.
Question: What is an R Package?
Answer: R package is a collection of objects that R Language can use. A package contains functions, data set, and documentation (which helps how to use the package) or other objects such as dynamically loaded libraries of already compiled code.
Question: How do I see which packages I have available?
Answer: To see which packages you have to use the command at the R prompt
Question: Which packages do I already have?
Answer: To see what packages are installed one can use the installed.packages() command a R prompt. The output will show the packages installed.
Question: How one can load a Package in R language?
Answer: Basic packages are already loaded. If you want to load a downloaded version of packages use the command
where package name is the name of the package you want to load. Here in the example, we used the “car”, it means “car” package will be loaded.
Question: How one can see the documentation of a particular package?
Answer: To see the documentation of particular package use the command
for more information about getting help follow the link: Getting Help in R Language
Question: How do I see the help for a specific function?
Answer: To get help about a function in R use command
> help(“function name”)
> ? function name
Question: What functions and datasets are available in a package?
Answer: To check what functions and datasets are in a package using the help command at R prompt. This will provide package information giving a list of functions and datasets.
> help(package = “MASS”)
Note that once a package is loaded, the help command can also be used with all available functions and datasets.
Question: How can one add or delete a package?
Answer: A package can be installed using command
> install.packages(“package name”)
and package can be removed or deleted using command
> remove.packages(“package name”)
The post is about R Packages in the form of Questions and Answers.
Question: What version of R do I run on my computer or laptop?
Answer: To get the information about the version of R, use the following command at the R prompt.
> # get a version of R > R.version.string
You will get a result like
 “R version 3.2.1 (2015-06-18)”
Note that a package in R language is a collection of objects that R Language can use. A package contains functions, data sets, and documentation (which helps how to use the package) or other objects such as dynamically loaded libraries of already compiled code.
Question: How to check what packages are already installed?
Answer: To get a list of installed packages, write “library()” without quotation marks at the prompt. You will see the list of all of the packages installed in the local R directory of your computer system and then it will list all packages installed globally on your computer system.
> # list all packages installed > library()
You would get results like (note that results below are given as an example only, it’s not a complete list)
Packages in library ‘C:/Program Files/R/R-3.2.1/library’:
KernSmooth Functions for kernel smoothing for Wand & Jones (1995)
MASS Support Functions and Datasets for Venables and Ripley’s MASS
Matrix Sparse and Dense Matrix Classes and Methods
methods Formal Methods and Classes
mgcv Mixed GAM Computation Vehicle with Automatic Smoothness Estimation
Following is a very short list of packages installed in the local library.
abind Combine Multidimensional Arrays
AlgDesign Algorithmic Experimental Design
askpass Safe Password Entry for R, Git, and SSH
assertthat Easy Pre and Post Assertions
tibble Simple Data Frames
plyr Tools for Splitting, Applying and Combining Data
For further details on packages in R, see the link Packages in R Language.