1
## Methods for dealing with formulas
4
function(form, sep = "/")
6
## split, on the sep call, the rhs of a formula into a list of subformulas
7
if (inherits(form, "formula") ||
8
mode(form) == "call" && form[[1]] == as.name("~"))
9
return(splitFormula(form[[length(form)]], sep = sep))
10
if (mode(form) == "call" && form[[1]] == as.name(sep))
11
return(do.call("c", lapply(as.list(form[-1]), splitFormula, sep = sep)))
12
if (mode(form) == "(") return(splitFormula(form[[2]], sep = sep))
13
if (length(form) < 1) return(NULL)
14
list(asOneSidedFormula(form))
17
subFormula <- function(form, pos = 2)
19
## extract component pos of form as a formula preserving the environment
21
val = eval(substitute(~comp))
22
environment(val) = environment(form)
26
getCovariateFormula <- function(object)
28
## Return the primary covariate formula as a one sided formula
29
form <- formula(object)
30
form <- form[[length(form)]]
31
if (length(form) == 3 && form[[1]] == as.name("|")){ # conditional expression
34
eval(substitute(~form))
37
getResponseFormula <- function(object)
39
## Return the response formula as a one sided formula
40
form <- formula(object)
41
if (!(inherits(form, "formula") && (length(form) == 3)))
42
stop("object must yield a two-sided formula")
46
setMethod("getGroupsFormula", signature(object = "ANY"),
47
function(object, asList = FALSE, sep = "/")
49
form = formula(object)
50
if (!inherits(form, "formula")) stop("object must yield a formula")
51
rhs = form[[length(form)]]
52
if (length(rhs) < 2 || rhs[[1]] != as.name("|")) return(NULL)
54
val = splitFormula(asOneSidedFormula(rhs[[3]]), sep = sep)
55
names(val) = unlist(lapply(val, function(el) deparse(el[[2]])))
58
asOneSidedFormula(rhs[[3]])
62
setMethod("getGroups", signature(object="data.frame", form="formula"),
63
function(object, form, level, data, sep, ...)
64
eval(getGroupsFormula(form)[[2]], object))
66
# Return the pairs of expressions separated by vertical bars
68
findbars <- function(term)
70
if (is.name(term) || is.numeric(term)) return(NULL)
71
if (term[[1]] == as.name("(")) return(findbars(term[[2]]))
72
if (!is.call(term)) stop("term must be of class call")
73
if (term[[1]] == as.name('|')) return(term)
74
if (length(term) == 2) return(findbars(term[[2]]))
75
c(findbars(term[[2]]), findbars(term[[3]]))
78
# Return the formula omitting the pairs of expressions separated by vertical bars
80
nobars <- function(term)
82
# FIXME: is the is.name in the condition redundant?
83
# A name won't satisfy the first condition.
84
if (!('|' %in% all.names(term)) || is.name(term)) return(term)
85
if (is.call(term) && term[[1]] == as.name('|')) return(NULL)
86
if (length(term) == 2) {
87
nb <- nobars(term[[2]])
88
if (is.null(nb)) return(NULL)
92
nb2 <- nobars(term[[2]])
93
nb3 <- nobars(term[[3]])
94
if (is.null(nb2)) return(nb3)
95
if (is.null(nb3)) return(nb2)
101
# Substitute the '+' function for the '|' function
103
subbars <- function(term)
105
if (is.name(term) || is.numeric(term)) return(term)
106
if (length(term) == 2) {
107
term[[2]] <- subbars(term[[2]])
110
stopifnot(length(term) == 3)
111
if (is.call(term) && term[[1]] == as.name('|')) term[[1]] <- as.name('+')
112
term[[2]] <- subbars(term[[2]])
113
term[[3]] <- subbars(term[[3]])