1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/functional_section_fun.R
\name{section_fun}
\alias{section_fun}
\alias{section_fun_sub}
\alias{section_fun_env}
\alias{get_section}
\alias{get_fun}
\title{Section a function and set default values in function}
\usage{
section_fun(fun, nms, vls = NULL, method = "args")
section_fun_sub(fun, nms, vls = NULL, envir = parent.frame())
section_fun_env(fun, nms, vls = NULL)
get_section(object)
get_fun(object)
}
\arguments{
\item{fun}{Function to be sectioned}
\item{nms}{Either a named list of the form name=value where each
name is the name of an argument of the function (in which case
\code{vls} is ignored) or a character vector of names of arguments.}
\item{vls}{A vector or list of values of the arguments}
\item{method}{One of the following: 1) "args" (default); based on
substituting fixed values into the function argument list as
default values). For backward compatibility can also be "def".
2) "body" for substituting fixed values into the function
body. For backward compatibility can also be "sub". 3) "env":
(for environment); using an auxillary argument for storing
sectioned values.}
\item{envir}{Environment}
\item{object}{An object from section_fun (a scaffold object).}
}
\value{
A new function: The input function \code{fun} but with certain
arguments fixed at specific values.
}
\description{
Section a functions domain by fixing certain
arguments of a function call.
}
\details{
Let E be a subset of the cartesian product X x Y where X
and Y are some sets. Consider a function f(x,y) defined on
E. Then for any x in X, the section of E defined by x (denoted
Ex) is the set of $y$s in Y such that (x, y) is in
E. Correspondingly, the section of f(x,y) defined by x is the
function $f_x$ defined on Ex given by $f_x(y)=f(x,y)$.
\code{section_fun} is a wrapper for calling \code{set_default} (default
method), \code{section_fun_env} or \code{section_fun_sub}. Notice that
creating a sectioned function with \code{section_fun_sub} can be
time consuming.
}
\examples{
f <- function(x, y){x + y}
f_ <- section_fun(f, list(y = 10), method="args") ## "def"" is default
f_ <- section_fun(f, nms="y", vls=10, method="args") ## SAME AS ABOVE
f_
f_(x=1)
f_ <- section_fun(f, list(y = 10), method="body") ##
f_ <- section_fun(f, nms="y", vls=10, method="body") ## SAME AS ABOVE
f_
f_(x=1)
f_ <- section_fun(f, list(y = 10), method="env")
f_ <- section_fun(f, nms="y", vls=10, method="env") ## SAME AS ABOVE
f_
f_(x=1)
get_section(f_)
get_fun(f_)
## With more complicated values:
g <- function(A, B) {
A + B
}
g_ <- section_fun(g, list(A = matrix(1:4, nrow=2)))
g_ <- section_fun(g, "A", list(matrix(1:4, nrow=2)))
g_(diag(1, 2))
g_ <- section_fun(g, list(A = matrix(1:4, nrow=2)))
## Using built in function
set.seed(123)
rnorm5 <- section_fun(rnorm, list(n=5))
rnorm5(0, 1)
set.seed(123)
rnorm(5)
}
\seealso{
\code{\link[=bquote_fun_list]{bquote_fun_list()}}
}
\author{
Søren Højsgaard, \email{sorenh@math.aau.dk} based on code
adapted from the curry package.
}
\concept{functional}
|