从字符串加载R包

dyn*_*amo 31 string r character

我想创建一个函数,包括加载我在函数中创建的包.一个简短的例子(不运行!):

loadMe <- function(name){
    genLib(xxx, libName = name) #make a new library with name "name"
    library(name)               #load the new library...
}
Run Code Online (Sandbox Code Playgroud)

这不起作用!一些可重现的代码说明了我的主要问题:

library(ggplot)         #this works fine
load.this <- "ggplot"
library(load.this)      #I want this to load ggplot!
Run Code Online (Sandbox Code Playgroud)

我知道问题在于,library()并将require()一个尚不存在的对象名称作为参数.我已经试过包装我的字符串,parse(),deparse(),substitute(),expression(),quote(),等等等等,这些都返回了同样的问题:

library(load.this)
# Error in library(loadss) : there is no package called 'loadss'
library(deparse(load.this))
# Error in library(deparse(loadss)) : 'package' must be of length 1
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

Sac*_*amp 46

使用character.only参数

foo <- "ggplot2"
library(foo,character.only=TRUE)
Run Code Online (Sandbox Code Playgroud)