Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can library() accept both quoted and unquoted strings

Tags:

r

For example, in an R session, typing library(ggplot2) and library("ggplot2") can both import the library ggplot2. However, if I type ggplot2 in the interactive session, I got:

> ggplot2
Error: object 'ggplot2' not found

Thus, obviously ggplot2 is not an object. How can library() accepts an undefined variable without complaining about it?

like image 525
Yu Fu Avatar asked Aug 08 '14 18:08

Yu Fu


People also ask

What are quoted and unquoted strings?

Sass supports two kinds of strings whose internal structure is the same but which are rendered differently: quoted strings, like "Helvetica Neue", and unquoted strings (also known as identifiers), like bold. Together, these cover the different kinds of text that appear in CSS.

How do you parse unquoted text in CSS?

When an unquoted string is parsed, the literal text of escapes are parsed as part of the string. For example, \a is parsed as the characters \, a , and space. In order to ensure that unquoted strings that have the same meanings in CSS are parsed the same way, though, these escapes are normalized.

Why are unquoted strings being normalized in CSS?

In order to ensure that unquoted strings that have the same meanings in CSS are parsed the same way, though, these escapes are normalized. For each code point, whether it’s escaped or unescaped:


1 Answers

Great question!

Let's crack open the library() function to see how it works.

enter library into your interactive session to see the innards of the function.

The key parts of the function are from lines 186 to 197.

 if (!missing(package)) {
     if (is.null(lib.loc))
         lib.loc <- .libPaths()
     lib.loc <- lib.loc[file.info(lib.loc)$isdir %in% TRUE]
     if (!character.only)
         package <- as.character(substitute(package))
     if (length(package) != 1L)
         stop("'package' must be of length 1")
     if (is.na(package) || (package == ""))
         stop("invalid package name")
     pkgname <- paste("package", package, sep = ":")
     newpackage <- is.na(match(pkgname, search())) 

The key lines are

if (!character.only)
             package <- as.character(substitute(package))

This means that as long as you don't change the character.only argument of library to TRUE, R will convert your package name into a character string and search for that.

Let's test:

 > library(ggplot2,character.only=TRUE)

outputs:

 Error in library(ggplot2, character.only = TRUE) :
   object 'ggplot2' not found

whereas

library("ggplot2",character.only=TRUE)

loads the package.

Basically, no matter what you give the library() function as an argument for package it will convert it into a characters unless you specify character.only to be TRUE.

As Dason points out in the comments, a good use of the character.only argument is in cases where you have the library names stored as objects themselves.

like image 77
bjoseph Avatar answered Oct 18 '22 20:10

bjoseph