Computing on the Language Followup

My article on computing on the language was unexpectedly popular and so I wanted to quickly follow up on my own solution. Many of you got the answer, and in fact many got solutions that were quite a bit shorter than mine. Here’s how I did it:

makeList <- function(...) {
        args <- substitute(list(...))
        nms <- sapply(args[-1], deparse)
        vals <- list(...)
        names(vals) <- nms

Baptiste¬†pointed out that Frank Harrell has already implemented this function in his Hmisc package as the ‘llist’ function (thanks for the pointer!). I’ll just note that this function does a bit more actually because each element of the returned list is an object of class “labelled”.

The shortest solution was probably Tony Breyal’s version:

makeList <- function(...) {
        structure(list(...), names = names(data.frame(...)))

However, it’s worth noting that this function modifies the object’s name if the name is non-standard (i.e. if you’re using backticks like `r object name`). That’s just because the ‘data.frame’ function automatically modifies names if they are non-standard.

Thanks to everyone for the responses! I’ll try to come up with another one soon.

comments powered by Disqus