## append_rhs.formula
(f1 <- append_rhs.formula(y~x,list(as.name("z1"),as.name("z2"))))
(f2 <- append_rhs.formula(~y,list(as.name("z"))))
(f3 <- append_rhs.formula(~y+x,structure(list(as.name("z")),sign=-1)))
(f4 <- append_rhs.formula(~y,list(as.name("z")),TRUE))
(f5 <- append_rhs.formula(y~x,~z1-z2))
(f6 <- append_rhs.formula(NULL,list(as.name("z"))))
(f7 <- append_rhs.formula(NULL,structure(list(as.name("z")),sign=-1)))
fe <- ~z2+z3
environment(fe) <- new.env()
(f8 <- append_rhs.formula(NULL, fe)) # OK
(f9 <- append_rhs.formula(y~x, fe)) # Warning
(f10 <- append_rhs.formula(y~x, fe, env=NULL)) # No warning, environment from fe.
(f11 <- append_rhs.formula(fe, ~z1)) # Warning, environment from fe
# \dontshow{
stopifnot(f1 == (y~x+z1+z2))
stopifnot(f2 == (y~z))
stopifnot(f3 == (y+x~-z))
stopifnot(f4 == (~y+z))
stopifnot(f5 == (y~x+z1-z2))
stopifnot(f6 == (~z))
stopifnot(f7 == (~-z))
stopifnot(f8 == (~z2+z3), identical(environment(f8), environment(fe)))
stopifnot(f9 == (y~x+z2+z3), identical(environment(f9), globalenv()))
stopifnot(f10 == (y~x+z2+z3), identical(environment(f10), environment(fe)))
stopifnot(f11 == (z2+z3~z1), identical(environment(f11), environment(fe)))
# }
## filter_rhs.formula
(f1 <- filter_rhs.formula(~a-b+c, `!=`, "a"))
(f2 <- filter_rhs.formula(~-a+b-c, `!=`, "a"))
(f3 <- filter_rhs.formula(~a-b+c, `!=`, "b"))
(f4 <- filter_rhs.formula(~-a+b-c, `!=`, "b"))
(f5 <- filter_rhs.formula(~a-b+c, `!=`, "c"))
(f6 <- filter_rhs.formula(~-a+b-c, `!=`, "c"))
(f7 <- filter_rhs.formula(~c-a+b-c(a),
function(x) (if(is.call(x)) x[[1]] else x)!="c"))
# \dontshow{
stopifnot(f1 == ~-b+c)
stopifnot(f2 == ~b-c)
stopifnot(f3 == ~a+c)
stopifnot(f4 == ~-a-c)
stopifnot(f5 == ~a-b)
stopifnot(f6 == ~-a+b)
stopifnot(f7 == ~-a+b)
# }
stopifnot(identical(list_rhs.formula(a~b),
structure(alist(b), sign=1, env=list(globalenv()), class="term_list")))
stopifnot(identical(list_rhs.formula(~b),
structure(alist(b), sign=1, env=list(globalenv()), class="term_list")))
stopifnot(identical(list_rhs.formula(~b+NULL),
structure(alist(b, NULL),
sign=c(1,1), env=rep(list(globalenv()), 2), class="term_list")))
stopifnot(identical(list_rhs.formula(~-b+NULL),
structure(alist(b, NULL),
sign=c(-1,1), env=rep(list(globalenv()), 2), class="term_list")))
stopifnot(identical(list_rhs.formula(~+b-NULL),
structure(alist(b, NULL),
sign=c(1,-1), env=rep(list(globalenv()), 2), class="term_list")))
stopifnot(identical(list_rhs.formula(~+b-(NULL+c)),
structure(alist(b, NULL, c),
sign=c(1,-1,-1), env=rep(list(globalenv()), 3), class="term_list")))
## eval_lhs.formula
(result <- eval_lhs.formula((2+2)~1))
stopifnot(identical(result,4))
Run the code above in your browser using DataLab