Skip to content

Instantly share code, notes, and snippets.

@rkingdc
Last active October 16, 2015 17:38
Show Gist options
  • Save rkingdc/940171ed9f03cf485ec3 to your computer and use it in GitHub Desktop.
Save rkingdc/940171ed9f03cf485ec3 to your computer and use it in GitHub Desktop.
Code to make a table of all functions used in your .Rhistory file
myHistory <- readLines('.Rhistory') # read in your .Rhistory file
pkgs <- unlist(unique(regmatches(myHistory, gregexpr('(?<=(library\\()).*(?=\\))|(?<=(require\\()).*(?=\\))', myHistory, perl=TRUE)))) # find all packages used in that session
sapply(pkgs, library, character.only=TRUE) # packages need to be on the search path to list their functions, so load them all
pkgs <- paste0('package:', pkgs) # package names must be in this format for ls() to work
fxns <- unname(unlist(sapply(pkgs, ls))) # list all exported functions in all loaded packages
myHistory <- gsub('\\,', ' ', myHistory) # remove all commas because they mess up remove extra arguments from *apply
myHistory <- gsub('.*([msl(parl)]?apply).*(?<=\\,)(.*)?,.*\\).*', '\\1 \\2', myHistory, perl=TRUE) # for *apply functions, remove eveything but the *apply and the functions applied
myHistory <- gsub("(?=apply)([\\(]).*\\)", '', myHistory, perl=TRUE) # remove anything in between parentheses
myHistory2 <- unlist(strsplit(myHistory, "[^0-9a-z._]")) # split up all the arguments into words, ignoring dots and underscores (because they are valid characters in functions)
myFxns <- myHistory2[which(myHistory2 %in% fxns)] # get a vector of all the valid functions in your history
table(myFxns) # frequency table of functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment