My recursive version 1 started getting more errors than I thought at first, so I made a simple exit and basically prepare the captured output utils:::print.ls_str(I think).
This has at least two drawbacks: captured output and eval-parse-texting, but it seems to work correctly for a very nested list, like in ggplot2::ggplotGrob.
These are just some of the helper functions.
unname2 <- function(l) {
l <- unname(l)
if (inherits(l, 'list'))
for (ii in seq_along(l))
l[[ii]] <- Recall(l[[ii]])
l
}
lnames <- function(l) {
nn <- lpath(l, TRUE)
gsub('\\[.*', '', sapply(strsplit(nn, '\\$'), tail, 1))
}
lpath <- function(l, use.names = TRUE) {
ln <- deparse(substitute(l))
l <- rapply(l, unclass, how = 'list')
L <- capture.output(if (use.names) l else unname2(l))
L <- L[grep('^\\$|^[[]{2,}', L)]
paste0(ln, L)
}
And this one returns useful information
lextract <- function(l, what, path.only = FALSE) {
ln1 <- eval(substitute(lpath(.l, TRUE), list(.l = substitute(l))))
ln2 <- eval(substitute(lpath(.l, FALSE), list(.l = substitute(l))))
cat(ln1[idx <- grep(what, ln1)], sep = '\n')
cat('\n')
cat(ln2[idx], sep = '\n')
cat('\n')
if (!path.only)
setNames(lapply(idx, function(x) eval(parse(text = ln1[x]))), ln1[idx])
else invisible()
}
fit <- lm(mpg ~ wt, mtcars)
lextract(fit, 'qraux')
So I can use this return value directly or now I have indexes.
fit[[7]][[2]]
# [1] 1.176777 1.046354
#
lextract(fit, 'qr', TRUE)
# fit$qr
# fit$qr$qr
# fit$qr$qraux
# fit$qr$pivot
# fit$qr$tol
# fit$qr$rank
#
# fit[[7]]
# fit[[7]][[1]]
# fit[[7]][[2]]
# fit[[7]][[3]]
# fit[[7]][[4]]
# fit[[7]][[5]]
I would prefer to use inline or single line.