Use the default value to retrieve the parameters called function

Is there a way to retrieve function parameters that are not specified in the function call from the evaluation formula?

For example, consider calling seq(1,10). If I want to get the first parameter, I can use quote() and simply use quote(seq(1,10))[ [1]]. However, this only works when the parameters are defined in the function call (not have default values), and I need to know their exact location.

In this example, is there some way Can you get the by parameter from seq(1,10) without needing a lengthy list of if statements to see if it is defined?

The first thing to note is that all the named parameters you are pursuing (from, to, by, etc. ) Belongs to seq.default(), which is how you call seq() scheduling, not seq() itself. (seq() itself 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
#
# $...

Is there a way to retrieve function parameters that are not specified in the function call from the evaluation formula?

For example, consider calling seq(1,10). If I want to get the first parameter, I can use quote() and simply use quote(seq(1,10))[ [1]]. However, this only works when the parameters are defined in the function call (not have default values), and I need to know their exact location.

In this example, is there some way Can you get the by parameter from seq(1,10) without needing a lengthy list of if statements to see if it is defined?

The first thing to note is that all the named parameters you are after (from, to, by, etc.) belong to seq.default(), which is what you call seq() scheduling method, not seq() itself. (seq() itself 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
#
# $...< /pre>

Leave a Comment

Your email address will not be published.