[Rd] Possible documentation problem/bug?

Sokol Serguei @oko| @end|ng |rom |n@@-tou|ou@e@|r
Thu Apr 30 15:00:20 CEST 2020


Le 30/04/2020 à 14:31, Dominic Littlewood a écrit :
> It seems like there is no obvious way in the documentation to convert 
> the expressions in the dots argument to a list without evaluating 
> them. Say, if you want to have a function that prints all its arguments:
If you wish to iterate through all the arguments (not only '...') then 
match.call() seems to be the most straightforward and explicit tool:

f=function(a, ...) {mc <- match.call(); print(as.list(mc)[-1])}
f(x,y[h],abc$d)
#$a
#x
#
#[[2]]
#y[h]
#
#[[3]]
#abc$d

Best,
Serguei.


>
>> foo(abc$de, fg[h], i)
> abc$de
> fg[h]
> i
>
> ...then converting them to a list would be helpful.
> Using substitute(...) was the first thing I tried, but that only gives
> the *first
> *argument in dots. It turns out that there is a way to do this, using
> substitute(...()), but this does not appear to be in either the substitute or
> the dots help page.
>
> In fact, there is a clue how to do this in the documentation, if you look
> closely. Let me quote the substitute page:
>
> "Substituting and quoting often cause confusion when the argument is
> expression(...). The result is a call to the expression constructor
> function and needs to be evaluated with eval to give the actual expression
> object."
>
> So this appears to give a way to turn the arguments into a list -
> eval(substitute(expression(...))).  But that's quite long, and hard to
> understand if you just come across it in some code - why are we using eval
> here? why are we substituting expression? - and would definitely require an
> explanatory comment. If the user just wants to iterate over the arguments,
> substitute(...()) is better. In fact, you can get exactly the same effect
> as the above code using as.expression(substitute(...())). Should the
> documentation be updated?
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list