Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/utils.R
1433 views
1
2
modify.args <- function(formals, arglist, ..., dots=FALSE)
3
{
4
# modify.args function from quantstrat
5
6
# avoid evaluating '...' to make things faster
7
dots.names <- eval(substitute(alist(...)))
8
9
if(missing(arglist))
10
arglist <- NULL
11
arglist <- c(arglist, dots.names)
12
13
# see 'S Programming' p. 67 for this matching
14
15
# nothing to do if arglist is empty; return formals
16
if(!length(arglist))
17
return(formals)
18
19
argnames <- names(arglist)
20
if(!is.list(arglist) && !is.null(argnames) && !any(argnames == ""))
21
stop("'arglist' must be a *named* list, with no names == \"\"")
22
23
.formals <- formals
24
onames <- names(.formals)
25
26
pm <- pmatch(argnames, onames, nomatch = 0L)
27
#if(any(pm == 0L))
28
# message(paste("some arguments stored for", fun, "do not match"))
29
names(arglist[pm > 0L]) <- onames[pm]
30
.formals[pm] <- arglist[pm > 0L]
31
32
# include all elements from arglist if function formals contain '...'
33
if(dots && !is.null(.formals$...)) {
34
dotnames <- names(arglist[pm == 0L])
35
.formals[dotnames] <- arglist[dotnames]
36
#.formals$... <- NULL # should we assume we matched them all?
37
}
38
.formals
39
}
40
41
# This is how it is used in quantstrat in applyIndicators()
42
# # replace default function arguments with indicator$arguments
43
# .formals <- formals(indicator$name)
44
# .formals <- modify.args(.formals, indicator$arguments, dots=TRUE)
45
# # now add arguments from parameters
46
# .formals <- modify.args(.formals, parameters, dots=TRUE)
47
# # now add dots
48
# .formals <- modify.args(.formals, NULL, ..., dots=TRUE)
49
# # remove ... to avoid matching multiple args
50
# .formals$`...` <- NULL
51
#
52
# tmp_val <- do.call(indicator$name, .formals)
53
54
55
###############################################################################
56
# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios
57
#
58
# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
59
#
60
# This library is distributed under the terms of the GNU Public License (GPL)
61
# for full details see the file COPYING
62
#
63
# $Id$
64
#
65
###############################################################################
66
67