1modify.args <- function(formals, arglist, ..., dots=FALSE)2{3# modify.args function from quantstrat45# avoid evaluating '...' to make things faster6dots.names <- eval(substitute(alist(...)))78if(missing(arglist))9arglist <- NULL10arglist <- c(arglist, dots.names)1112# see 'S Programming' p. 67 for this matching1314# nothing to do if arglist is empty; return formals15if(!length(arglist))16return(formals)1718argnames <- names(arglist)19if(!is.list(arglist) && !is.null(argnames) && !any(argnames == ""))20stop("'arglist' must be a *named* list, with no names == \"\"")2122.formals <- formals23onames <- names(.formals)2425pm <- pmatch(argnames, onames, nomatch = 0L)26#if(any(pm == 0L))27# message(paste("some arguments stored for", fun, "do not match"))28names(arglist[pm > 0L]) <- onames[pm]29.formals[pm] <- arglist[pm > 0L]3031# include all elements from arglist if function formals contain '...'32if(dots && !is.null(.formals$...)) {33dotnames <- names(arglist[pm == 0L])34.formals[dotnames] <- arglist[dotnames]35#.formals$... <- NULL # should we assume we matched them all?36}37.formals38}3940# This is how it is used in quantstrat in applyIndicators()41# # replace default function arguments with indicator$arguments42# .formals <- formals(indicator$name)43# .formals <- modify.args(.formals, indicator$arguments, dots=TRUE)44# # now add arguments from parameters45# .formals <- modify.args(.formals, parameters, dots=TRUE)46# # now add dots47# .formals <- modify.args(.formals, NULL, ..., dots=TRUE)48# # remove ... to avoid matching multiple args49# .formals$`...` <- NULL50#51# tmp_val <- do.call(indicator$name, .formals)525354###############################################################################55# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios56#57# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt58#59# This library is distributed under the terms of the GNU Public License (GPL)60# for full details see the file COPYING61#62# $Id$63#64###############################################################################656667