Getting function call arguments with default values ​​in R

Is there a way to get function arguments from the evaluated formula that are not specified in the function call?

For example, consider calling seq(1, 10) . If I wanted to get the first argument, I could use quote() and just use quote(seq(1,10))[[1]] . However, this only works if the argument is defined in the function call (instead of the default value), and I need to know its exact position.

In this example, is there a way to get the by argument from seq(1, 10) without a long list of if to find out if this is defined?

+4
source share
1 answer

First of all, it should be noted that all named arguments that you use ( from , to , by , etc.) refer to seq.default() , the method that is sent by your call to seq() , and not to seq() . ( seq() has only one formal, ... ).

From there you can use these two building blocks

 ## (1) Retrieves pairlist of all formals formals(seq.default) # [long pairlist object omitted to save space] ## (2) Matches supplied arguments to formals match.call(definition = seq.default, call = quote(seq.default(1,10))) # seq.default(from = 1, to = 10) 

do something like this:

 modifyList(formals(seq.default), as.list(match.call(seq.default, quote(seq.default(1,10))))[-1]) # $from # [1] 1 # # $to # [1] 10 # # $by # ((to - from)/(length.out - 1)) # # $length.out # NULL # # $along.with # NULL # # $... 
+7
source

Source: https://habr.com/ru/post/1485466/


All Articles