##
## 1. name in object, return
##
e1 <- getElement2(list(ab=1), 'ab', 2) # 1
# check
stopifnot(
all.equal(e1, 1)
)
##
## 2. name not in object, return default
##
eNA <- getElement2(list(), 'ab') # default default = NA
# check
stopifnot(
all.equal(eNA, NA)
)
e0 <- getElement2(list(), 'ab', 2) # name not in object
stopifnot(
all.equal(e0, 2)
)
e2 <- getElement2(list(ab=1), 'a', 2) # partial matching not used
stopifnot(
all.equal(e2, 2)
)
##
## 3. name NULL in object, return default
##
ed <- getElement2(list(a=NULL), 'a',2) # 2 with a warning
stopifnot(
all.equal(ed, 2)
)
e. <- getElement2(list(a=NULL), 'a', 2, warn.NULL=FALSE) # NULL
stopifnot(
all.equal(e., NULL)
)
eNULL <- getElement2(list(a=NULL), 'a', NULL) # NULL
stopifnot(
all.equal(eNULL, NULL)
)
##
## 4. Language: find, eval, return
##
Qte <- quote(plot(1:4, y=x, col=c2))
if(require(pryr)){
Qt <- pryr::standardise_call(Qte) # add the name 'x'
fn <- getElement2(Qt)
eQuote <- getElement2(Qt, 'y')
Col2 <- getElement2(Qt, 'col', envir=list(c2=2))
# check
stopifnot(
all.equal(fn, 'plot')
)
stopifnot(
all.equal(eQuote, 1:4)
)
stopifnot(
all.equal(Col2, 2)
)
}
Run the code above in your browser using DataLab