Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Created December 15, 2016 01:14
Show Gist options
  • Save jonocarroll/55046430b23d88e9628ac6b4edf8bb52 to your computer and use it in GitHub Desktop.
Save jonocarroll/55046430b23d88e9628ac6b4edf8bb52 to your computer and use it in GitHub Desktop.
Analyse an R script and determine which package the functions are likely to be from
library(dplyr)
library(ggplot2)
find_functions <- function(file) {
getParseData(parse(file = file)) %>%
filter(token == "SYMBOL_FUNCTION_CALL") %>%
distinct(text) %>%
use_series(text)
}
identify_packages <- function(functionNames) {
db <- hsearch_db()
db$Aliases %>%
filter(Alias %in% functionNames) %>%
mutate(pkgString = paste0(Package, "::", Alias)) %>%
group_by(Alias) %>%
mutate(ambiguity = n()) %>%
arrange(Alias, Package) %>%
select(-ID) %>%
ungroup()
}
find_functions("~/somefile.R") %>%
identify_packages() %>%
head()
#> # A tibble: 6 x 4
#> Alias Package pkgString ambiguity
#> <chr> <chr> <chr> <int>
#> 1 aes ggplot2 ggplot2::aes 1
#> 2 AIC stats stats::AIC 2
#> 3 AIC VGAM VGAM::AIC 2
#> 4 arrange dplyr dplyr::arrange 2
#> 5 arrange plyr plyr::arrange 2
#> 6 as.character base base::as.character 1
find_functions("~/somefile.R") %>%
identify_packages() %>%
filter(ambiguity == 1) %>%
group_by(Package) %>%
summarise(nCalls = n()) %>%
ungroup() %>%
ggplot() +
geom_col(aes(x="", fill=Package, y=nCalls), width=1) +
coord_polar(theta = "y") +
scale_x_discrete("") +
theme_void()
@jonocarroll
Copy link
Author

jonocarroll commented Dec 15, 2016

Function calls in ~/somefile.R which are unambiguously available within available (installed) packages (those found via hsearch_db()).

testscript

Determining which namespace overwrites would require a much more careful investigation of the environment/search path/namespaces at run-time.

@jonocarroll
Copy link
Author

Suggestion for improvement: Determine available namespaces prior to running, and whittle down the list of packages to just those. Should reduce the ambiguity significantly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment