I've been an active R
user for several years now, and something has always confused me. When installing packages (e.g. dplyr
), one must specify the name of the package as a string i.e.
install.packages("dplyr")
Rather than
install.packages(dplyr)
This makes sense to me, since "dplyr"
would pass as the name of the package and not as an object, which would be implied by dplyr
without the quotation marks.
However, when we go to load the library, both the string and object version pass through and load the package. Both of the following correctly load the package:
library("dplyr")
detach("package:dplyr", unload=TRUE)
library(dplyr)
There is no object named dplyr
in my workspace, and I don't understand why these two base
functions would have different syntax. I also don't understand why the version without the quotation marks wouldn't evaluate the object. For example
dplyr <- "mada"
install.packages(dplyr)
library(dplyr)
The above installs mada
, but loads dplyr
, even though dplyr
is an object that evaluates to "mada"
. Note that I am working in RStudio
v.0.99.467 on Mac OS 10.10.4, if that matters.
Is there a reason for this, or is it simply that the functions work differently?
In R, you can easily install and load additional packages provided by other users. or click Tools > Install packages. Write the package name in the dialog, then click install. Once you install the package, you need to load it so that it becomes available to use.
In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library.
3. Which function is used for loading packages? Clarification: library() function is used to load a package. library() is not useful when we are developing a package since you have to install the package first.
library
function takes package name
as first argument. This name
actually refers to the name passed as package argument rather than any value associated to that name. library
function internally converts the package argument to a character literal (except when character.only
is set to TRUE
).
So,
plyr <- "dplyr"
library(plyr)
will undergo
if(!character.only)
package <- as.character(substitute(plyr))
to become
package <- "plyr"
install.packages
takes a character vector as name(s) of packages to be installed.
dd <- "plyr"
install.packages(dd)
This would install plyr
.
It is becauseinstall.packages
don't do any such conversion with pkgs
argument. So install.packages
installs what pkgs
refers to.
I don't understand why these two base functions would have different syntax.
It’s hard to speculate about the reasons for specific language design decisions. The decision to allow unquoted names for the library
call has been criticised by many prominent R programmers as illogical, inconsistent and unnecessary, but here we are.
Besides the argument that it’s (marginally) more convenient not to type the quotes, another potential reason is similarity to other languages: for instance, in Python you import
libraries by specifying their name like so: import lib_name
, not import 'lib_name'
. This encourages library writers to choose library names that are valid identifiers in the language (in Python, loaded libraries are objects that you need to refer to). This is less relevant in R, where you rarely refer to the library besides loading it.
I also don't understand why the version without the quotation marks wouldn't evaluate the object.
Because R allows non-standard evaluation. In particular, function arguments are only evaluated when they are referred to, not before:
f = function (arg) {}
f(stop('this won’t raise an error!'))
arg
is never used, so never evaluated.
In the case of library
, the argument isn’t evaluated either. Instead, it’s used in unevaluated form via substitute(package)
.
If you look through the source code for ?library
you will see at line 230 click here :
if (!character.only)
package <- as.character(substitute(package))
The package name that the user supplies is coerced to character then concatenated:
pkgname <- paste("package", package, sep = ":")
This is what allows the input without quotes for library. install.packages
does not have the same functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With