解压缩R中省略号的参数列表 [英] Unpacking argument lists for ellipsis in R

查看:104
本文介绍了解压缩R中省略号的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些函数中使用省略号(...)使我感到困惑,即如何将包含参数的对象作为单个参数传递.

I am confused by the use of the ellipsis (...) in some functions, i.e. how to pass an object containing the arguments as a single argument.

在Python中,它称为解压缩参数列表",例如

In Python it is called "unpacking argument lists", e.g.

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]

例如,在R中,您具有使用省略号的函数file.path(...).我想要这种行为:

In R for instance you have the function file.path(...) that uses an ellipsis. I would like to have this behaviour:

> args <- c('baz', 'foob') 
> file.path('/foo/bar/', args)
[1] 'foo/bar/baz/foob'

相反,我得到

[1] 'foo/bar/baz' 'foo/bar/foob'

其中args的元素不是解包"的,并且不能同时求值.是否有R等价于Pythons *arg?

where the elements of args are not "unpacked" and evaluated at the same time. Is there a R equivalent to Pythons *arg?

推荐答案

语法虽然不那么漂亮,但这可以解决问题:

The syntax is not as beautiful, but this does the trick:

do.call(file.path,as.list(c("/foo/bar",args)))

do.call带有两个参数:一个函数和用于调用该函数的参数列表.

do.call takes two arguments: a function and a list of arguments to call that function with.

这篇关于解压缩R中省略号的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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