Is there a way to pass arbitrary arguments to more than one command inside a function? The following function clearly does not work but I hope it explains what I am trying to achieve.
test = function(x = rnorm(20), y = rnorm(20), ..., ---){
plot(x, y, type = "p", ...)
lines(x, y, ---)
}
The goal is to be able to write a function that creates plot with say lines
and points
and polygon
and can take arbitrary arguments for each command and pass them to the respective commands without me having to explicitly specify arguments for each command.
Here is a hackish approach:
.. <- "/////" #or anything which won't be used as a valid parameter
f <- function(...){
arguments <- list(...)
if(.. %in% arguments){
i <- which(arguments == ..)
terms <- unlist(arguments[1:(i-1)])
factors <- unlist(arguments[(i+1):length(arguments)])
c(sum(terms),prod(factors))
}
}
Then, for example,
> f(2,3,4,..,7,8,10)
[1] 9 560
You could obviously extend the idea to multiple ...
fields, each delimited with ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With