向量化分配函数并在全局环境中创建对象
我想对 assign
函数进行向量化,并创建一组反映提供的命名向量的参数,这些参数可在 .GlobalEnv
中直接使用。
代码
vec_args <- c(arg1 = 1,
arg2 = 2,
arg3 = 3)
Vectorize(assign)(x = names(vec_args),
value = vec_args,
envir = globalenv())
错误
点[[3L]][[1L]]中的错误:子集化的参数错误 环境
期望的结果
ag1 <- 1; arg2 <- 2; arg3 <- 3; ls()
# [1] "ag1" "arg2" "arg3" "vec_args"
或通过分配
:
实际上,我想复制调用:
assign(x = "arg1", value = vec_args[1], envir = globalenv())
对于向量的每个元素并使用向量名称在.GlobalEnv
中创建名称。
I would like to vectorize the assign
function and create a set of arguments reflecting provided named vector that would be directly available in the .GlobalEnv
.
Code
vec_args <- c(arg1 = 1,
arg2 = 2,
arg3 = 3)
Vectorize(assign)(x = names(vec_args),
value = vec_args,
envir = globalenv())
Error
Error in dots[[3L]][[1L]] : wrong arguments for subsetting an
environment
Desired results
ag1 <- 1; arg2 <- 2; arg3 <- 3; ls()
# [1] "ag1" "arg2" "arg3" "vec_args"
or via assign
:
In effect, I would like to replicate the call:
assign(x = "arg1", value = vec_args[1], envir = globalenv())
for each element of a vector and use vector names to create names in .GlobalEnv
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此功能已经可以通过
list2env
:类似以下的事情也将起作用:
This functionality is already available via
list2env
:Something like the following would also work: