2

The question title may betray some misconception which you're welcome to point out. Nonetheless, there is something mysterious about some of the plyr functions. For example, look at the laply function, which is scripted as:

function (.data, .fun = NULL, ..., .progress = "none", .inform = FALSE, 
    .drop = TRUE, .parallel = FALSE, .paropts = NULL) 
{
    if (is.character(.fun)) 
        .fun <- do.call("each", as.list(.fun))
    if (!is.function(.fun)) 
        stop(".fun is not a function.")
    if (!inherits(.data, "split")) 
        .data <- as.list(.data)
    res <- llply(.data = .data, .fun = .fun, ..., .progress = .progress, 
        .inform = .inform, .parallel = .parallel, .paropts = .paropts)
    list_to_array(res, attr(.data, "split_labels"), .drop)
}
<environment: namespace:plyr>

The last call, which formats the result, uses a function list_to_array. That function is not defined inside laply and neither does it exist outside (check exists("list_to_array")). Yet laply works (of course - and I have used it personally)! How is this possible?

I would like to know how laply combines list elements to an array so I can know how efficient it is. There are alternative ways, for example do.call(rbind, res) (in the 2D case). Knowing how laply does this would help me make an informed decision.

Other examples include (non-exhaustively): splitter_a within aaply and alply, splitter_d within ddply and list_to_dataframe within ldply. By contrast, simplify2array in sapply is explicitly defined outside sapply, and is occasionally useful on its own.

CJB
  • 1,759
  • 17
  • 26
  • 1
    It is defined - but not exported. You can call it using `plyr:::list_to_array` etc. But it will be hard to find documentation. – bdecaf Mar 22 '16 at 12:15
  • 1
    Check `getAnywhere(list_to_array)`. Maybe read [how R searches and finds stuff](http://blog.obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/). Packages have their own namespaces where they can create non-exported functions. Those functions are generally not intended to be called directly by the user. – MrFlick Mar 22 '16 at 12:16
  • @Joshua It's not really asking the same question. `.Primitive` and other functions mentioned are obviously referring to some internal code. This question is about functions using R scripting but that weren't obviously present in any environment. Turns out they're there, but at a deeper level. – CJB Mar 22 '16 at 13:47
  • Now it's asking the same question. :) – Joshua Ulrich Mar 22 '16 at 14:45
  • Well, neither question has changed.... – CJB Mar 22 '16 at 16:21

0 Answers0