Extract elements by name from a nested list

Viewed 2597

For a named, nested list, what is the best way to extract a specific element? If I have a list with known fields (eg, from a yaml file), I want to extract an element (list or otherwise) without having to search through the names and indices or trying to keep track of the levels in the str output.

For example, I know that lm returns a nested list which contains qr info.

fit <- lm(mpg ~ wt, mtcars)
fit$qr$qraux
# [1] 1.176777 1.046354

But if I don't know the order, I just want to specify the list along with the name of the element. Ideally, something would give me both the path of indices to the element and path of names to the element and the element itself.

Related, related, related

2 Answers
Related