R:更改省略号中参数的值并将省略号传递给另一个函数,而不使用list()和eval() [英] R: Change value of an argument in ellipsis and pass ellipsis to the other function without using list() and eval()

查看:283
本文介绍了R:更改省略号中参数的值并将省略号传递给另一个函数,而不使用list()和eval()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通用的方法来改变省略号内的参数值并将其传递给另一个函数。

I am looking for a universal way to change a value of an argument inside ellipsis and pass it to the other function. I know an ugly solution for that, which looks like this:

test <- function(...) {
  a <- list(...)
  a[['y']] <- 2
  return(eval(parse(text=paste0('identical(',paste(unlist(a),collapse=','),')'))))
}

test(x=1,y=1)

理想情况下,我想避免将...转换为列表,然后使用eval()。是否有可能以某种方式引用里面的一个参数,并改变它的值?

Ideally I would like to avoid converting ... to a list and then using eval(). Is it possible to somehow refer to an argument inside ... by name and change it's value?

推荐答案

您必须解压缩 ... 来操纵其内容。丑陋的一点在这里真的是最后一行,可以简化为 do.call(相同,a)

You do have to unpack ... to manipulate its contents. The ugly bit here, really, is your last line, which can be simplified to do.call(identical, a):

test <- function(...) {
  a <- list(...)
  a[['y']] <- 2
  do.call(identical, a)
}

test(x=1,y=1)
# [1] FALSE

这篇关于R:更改省略号中参数的值并将省略号传递给另一个函数,而不使用list()和eval()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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