What class is inside a non-character library call in R?

In R, we can load packages using:

library(knitr)

or

library("knitr")

As ?libraryhe says, that this argument can be a symbol or a name. However:

is.name(knitr)
Error: object 'knitr' not found
class(knitr)
Error: object 'knitr' not found

It also complicates this when a package contains a function, which is also the name of the package:

library(data.table)
class(data.table)
[1] "function"
library(data.table) #returns nothing

Be that as it may, he does not know how to pass the function data.tableto the library function, but instead interpret it as a call to download the package (I think). How does R handle this?

+4
source share
1 answer

Matching lines library()say

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

We can make our little function ( library()complicated!) To learn how it works:

tmpf <- function(x) {
   str(substitute(x))
}
tmpf(knitr)
## symbol knitr

, quote():

str(quote(knitr))
## symbol knitr

tmpf() - substitute():

tmpf(quote(knitr))
## language quote(knitr)

eval(substitute(x)) tmpf(), " knitr ", knitr - knitr, quote(knitr).

character.only library() - , , , "knitr";

str_var <- "knitr"
library(str_var,character.only=TRUE)
+2

Source: https://habr.com/ru/post/1649327/


All Articles