# NOT RUN {
# Make a nested list
L <- list(x1=list(x=list("val11","val112"),
y=list("val12"),
test=list("testlist2")),
x2=list(x=list("val21","val212"),
y=list("val22"),
test=list("testlist2")),
x3=list(x=list("val31","val312"),
y=list("val32"),
test=list("testlist3")))
# Get the subelement "x1"
str(getse(L, "x1", depth=1))
# Same as
str(L[["x1"]])
# Get the element named x in second layer
str(getse(L, "x", depth=2))
# Depth is default to 2
str(getse(L, "y"))
# Nice when splitting string
x <- strsplit(c("x.k1","y.k2"), "\\.")
# Get all before the split "\."
getse(x, 1)
# Get after
getse(x, 2)
# Get an element with an integer index
x <- strsplit(c("x.k1","y.k2","x2"), "\\.")
getse(x, 1)
# if the element is not there, then an error is thrown
try(getse(x, 2))
# Use regex pattern for returning elements matching in the specified layer
getse(L, "^te", depth=2, useregex=TRUE)
# }
Run the code above in your browser using DataLab