Which functions in R base call internal code?

September 14, 2010
By

(This article was first published on 4D Pie Charts » R, and kindly contributed to R-bloggers)

In a recent question on Stack Overflow about speeding up group-by operations, Marek wondered which functions called .Internal code (and consequently were fast). I was surprised to see that there appears to be no built-in way to check whether or not this is the case (though is.primitive is available for primitive functions).

Writing such a function is quite straight forward. We simply examine the contents of the function body, and serach for the string “.Internal” within it.

callsInternalCode <- function(fn)
{
+++if(!require(stringr)) stop("stringr package required")
+++any(str_detect(capture.output(body(fn)), ".Internal"))
}

You can retrieve all the function in the base package with this one-liner taken from an example on the Filter help page.

funs <- Filter(is.function, sapply(ls(baseenv()), get, baseenv()))

Now getting the list of functions that call internal code is easy.

names(funs)[sapply(funs, callsInternalCode)]
[1] "abbreviate" "agrep"
[3] "all.names" "all.vars"
[5] "anyDuplicated.default" "aperm"
...


Tagged: internal, r

To leave a comment for the author, please follow the link and comment on his blog: 4D Pie Charts » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.