[英]Load data from package dynamically in R
I would like to be able to write the below in a form that uses string interpolation.我希望能够以使用字符串插值的形式编写以下内容。
r[[glue("fic_{data}")]] <- switch(
data,
"data" = fic::data,
"targets" = fic::targets
)
I have tried things like我尝试过类似的东西
r[[glue("fic_{data}")]] <- eval(glue("fic::{data}"))
r[[glue("fic_{data}")]] <- eval(sym(glue("fic::{data}")))
r[[glue("fic_{data}")]] <- eval(glue("fic::{!!data}"))
in many combinations and placements of sym
and !!
在
sym
和!!
的许多组合和位置中. .
Is it possible to do and how if so?有可能吗?如果可以,怎么办?
With eval
, we may need the parse
as well使用
eval
,我们可能还需要parse
> eval(glue("collapse::wlddev"))
collapse::wlddev
returns just the glue
object只返回
glue
object
> str(eval(parse(text = glue("collapse::wlddev"))))
'data.frame': 13176 obs. of 13 variables:
$ country: chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
..- attr(*, "label")= chr "Country Name"
$ iso3c : Factor w/ 216 levels "ABW","AFG","AGO",..: 2 2 2 2 2 2 2 2 2 2 ...
..- attr(*, "label")= chr "Country Code"
$ date : Date, format: "1961-01-01" "1962-01-01" "1963-01-01" "1964-01-01" ...
$ year : int 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 ...
...
Or use the parse_expr
from rlang
或者使用
parse_expr
的rlang
eval(rlang::parse_expr(glue("collapse::wlddev")))
Another option is to pass the string in the data
to load the data另一种选择是传递
data
中的字符串以加载数据
data(list = "wlddev", package = "collapse")
head(wlddev)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.