Using get inside lapply, inside a function

前端 未结 3 781
萌比男神i
萌比男神i 2020-12-16 01:09

this may seem like a overly complicated question, but it has me driving me a little nuts for some time. It is also for curiosity, because I already have a way of doing what

3条回答
  •  醉话见心
    2020-12-16 01:31

    This is adapted from @Josh O'Brien's solution above using sapply to automatically assign the correct names to the resulting list (saves one line of code):

    foo <- function(a=1, b=5, h='coconut') {
        frm <- formals(foo)
        parms <- sapply(names(frm), get, envir=sys.frame(sys.parent(-1)), simplify=FALSE)
        return(parms)
    }
    

提交回复
热议问题