Last data update: 2014.03.03

R: Extract a named element from an object with a default
getElement2R Documentation

Extract a named element from an object with a default

Description

Get element name of object. If object does not have an element name, return default.

If the name element of object is NULL the result depends on warn.NULL: If TRUE, issue a warning and return default. Otherwise, return NULL

Usage

getElement2(object, name=1, default=NA, warn.NULL=TRUE, 
            envir=list(), returnName)

Arguments

object

object from which to extract component name.

name

Name or index of the element to extract

default

default value if name is not part of object.

warn.NULL

logical to decide how to treat cases where object has a component name: If TRUE, return default with a warning. Otherwise, return NULL.

envir

Supplemental list beyond object in which to look for names in case object[[name]] is a language object that must be evaluated.

returnName

logical: TRUE to return as.character of any name found as an element of object.

FALSE to eval any name found in the environment of object.

Default = TRUE if name == 1 or a character string matching the name of the first element of object.

Details

1. If is.numeric(name) In <- (1 <= name <= length(object))

2. else In <- if(name %in% names(object))

3. El <- if(In) object[[name]] else default

4. warn.NULL?

5. if(returnName) return(as.character(El)) else return(eval(El, envir=object))

Value

an object of the form of object[[name]]; if object does not have an element or slot name, return default.

Author(s)

Spencer Graves with help from Marc Schwartz and Hadley Wickham

See Also

getElement, which also can return slots from S4 objects.

Examples

##
## 1.  name in object, return 
##
e1 <- getElement2(list(ab=1), 'ab', 2) # 1
# check 

all.equal(e1, 1)


##
## 2.  name not in object, return default
##
eNA <- getElement2(list(), 'ab') # default default = NA
# check 

all.equal(eNA, NA)


e0 <- getElement2(list(), 'ab', 2) # name not in object

all.equal(e0, 2)


e2 <- getElement2(list(ab=1), 'a', 2) # partial matching not used 

all.equal(e2, 2)


##
## 3.  name NULL in object, return default 
##
ed <- getElement2(list(a=NULL), 'a',2) # 2 with a warning

all.equal(ed, 2)


e. <- getElement2(list(a=NULL), 'a', 2, warn.NULL=FALSE) # NULL

all.equal(e., NULL)


eNULL <- getElement2(list(a=NULL), 'a', NULL) # NULL

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

  all.equal(fn, 'plot')


  all.equal(eQuote, 1:4)


  all.equal(Col2, 2)

}

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Ecfun)

Attaching package: 'Ecfun'

The following object is masked from 'package:base':

    sign

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Ecfun/getElement2.Rd_%03d_medium.png", width=480, height=480)
> ### Name: getElement2
> ### Title: Extract a named element from an object with a default
> ### Aliases: getElement2
> ### Keywords: manip
> 
> ### ** Examples
> 
> ##
> ## 1.  name in object, return 
> ##
> e1 <- getElement2(list(ab=1), 'ab', 2) # 1
> # check 
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(e1, 1)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> ##
> ## 2.  name not in object, return default
> ##
> eNA <- getElement2(list(), 'ab') # default default = NA
> # check 
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(eNA, NA)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> e0 <- getElement2(list(), 'ab', 2) # name not in object
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(e0, 2)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> e2 <- getElement2(list(ab=1), 'a', 2) # partial matching not used 
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(e2, 2)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> ##
> ## 3.  name NULL in object, return default 
> ##
> ed <- getElement2(list(a=NULL), 'a',2) # 2 with a warning
Warning message:
In getElement2(list(a = NULL), "a", 2) :
  element a is NULL; returning default
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(ed, 2)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> e. <- getElement2(list(a=NULL), 'a', 2, warn.NULL=FALSE) # NULL
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(e., NULL)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> eNULL <- getElement2(list(a=NULL), 'a', NULL) # NULL
> ## Don't show: 
> stopifnot(
+ ## End(Don't show)
+ all.equal(eNULL, NULL)
+ ## Don't show: 
+ )
> ## End(Don't show)
> 
> ##
> ## 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
+ ## Don't show: 
+ stopifnot(
+ ## End(Don't show)
+   all.equal(fn, 'plot')
+ ## Don't show: 
+ )
+ ## End(Don't show)
+ ## Don't show: 
+ stopifnot(
+ ## End(Don't show)
+   all.equal(eQuote, 1:4)
+ ## Don't show: 
+ )
+ ## End(Don't show)
+ ## Don't show: 
+ stopifnot(
+ ## End(Don't show)
+   all.equal(Col2, 2)
+ ## Don't show: 
+ )
+ ## End(Don't show)
+ }
Loading required package: pryr
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>