R:如何使用列表元素像省略号参数呢? [英] R: How to use list elements like arguments in ellipsis?

查看:538
本文介绍了R:如何使用列表元素像省略号参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否标有我的问题正确的,但我给它一个镜头:

I'm not sure if I labelled my question correct, but I give it a shot:

我想用一个包,它使用省略号的函数, FUNC(...)。我班 My_Class 的所有参数都在列表中。因为我有相当多的争论,我想避免 FUNC(ARG1,ARG2,ARG3)。所以最好我想做 FUNC(my_list)。问题是,该函数看起来像这样

I want to use a package with a function which uses an ellipsis, func(...). All my arguments of class My_Class are in a list. As I have quite a lot of arguments, I'd like to avoid func(arg1, arg2, arg3). So ideally I'd like to do func( my_list ). The problem is that the function looks like this

function(...) {
  cl <- match.call(expand.dots = FALSE)
  names <- lapply(cl[[2]],as.character)  
  ev <- parent.frame()
  classes <- unlist(lapply(names,function(name){class(get(name,envir=ev))}))    
  if( !all( classes == "My_Class" ) ) { 
    stop("an argument to ... is not a My_Class")
  }
}

所以,通过列表不起作用。

So passing a list doesn't work.

我也试过 FUNC(my_list [[1]],my_list [[2]]) FUNC(as.ex pression(my_list [[1]])),但即使他们不工作。由于 FUNC 把参数为字符, FUNC(my_list [[1]])变成 CHR [1:3][[,my_list1里面, GET(姓名,ENVIR = EV)崩溃。我也试过 do.call(FUNC,my_list)但这再次崩溃为 my_list 进行评估,因此名称里面的功能不再是字符 My_CLass ,再次 GET(姓名,ENVIR = EV)崩溃。

I also tried func(my_list[[1]], my_list[[2]]) and func(as.expression(my_list[[1]])), but even they do not work. As func treats the arguments as characters, func(my_list[[1]]) becomes chr [1:3] "[[" "my_list" "1" inside, and get(name,envir=ev) crashes. I also tried do.call(func, my_list) but this again crashes as my_list is evaluated, therefore name inside the function is no longer a character but a My_CLass and again get(name,envir=ev) crashes.

所以我的问题:如何可以完成列表中的手不改变包/底层函数?我想过写一个包装,但我坚持,因为我甚至不能够 FUNC(my_list [[1]])使工作。也许我只是缺少我的问题正确的说法。

So my question: How can I accomplish to hand in a list without changing the package / underlying function? I thought about writing a wrapper, but I'm stuck as I do not even can func(my_list[[1]]) make work. Maybe I'm just missing the correct term for my problem.

在此先感谢!

推荐答案

这工作,但我在该功能的设计选择很迷惑:

This works, but I'm mystified by the design choices in that function:

lst <- list(
  a=structure(1:3, class="My_Class"), 
  b=structure(letters[1:3], class="My_Class")
)
env <- list2env(lst)
call <- as.call(append(list(fun), names(lst)))
eval(call, env)

要欺骗功能进入工作,你必须在你的列表项的对象(这是 list2env 确实,创建环境)的环境来评估它

To trick the function into working, you have to evaluate it in an environment where your list items are objects (this is what list2env does, creates that environment).

有关该功能的可怕的事情是,它查找对象 parent.frame 基础上自己的名字,假设 ... 值的确名称。我真不明白为什么功能并不只是像做:

The terrible thing about that function is that it looks up objects in parent.frame based on their names, assuming that the ... values are indeed names. I really don't understand why that function doesn't just do something like:

if(!all(vapply(list(...), class, "") == "My_Class")) stop(...)

这篇关于R:如何使用列表元素像省略号参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆