Skip to content

Instantly share code, notes, and snippets.

@s-u
Last active November 11, 2020 01:28
Show Gist options
  • Save s-u/6c7f88d209747294653d5e11c5c45dbe to your computer and use it in GitHub Desktop.
Save s-u/6c7f88d209747294653d5e11c5c45dbe to your computer and use it in GitHub Desktop.
Generate S3Method() exports
## automatic generation of S3Method() exports for a NAMESPACE
all = ls(2)
## find generics
isg = sapply(all, function(o) length(grep("UseMethod", deparse(get(o)))))
gens = names(isg[isg > 0])
## check some most common primitives
for (gen in c("+","-","$","$<-"))
if (any(substr(all, 1, nchar(gen) + 1) == paste0(gen, ".")))
gens = c(gens, gen)
## find all methods for the generics
ex = unique(unlist(lapply(gens, function(g) {
o <- all[substr(all, 1, nchar(g) + 1) == paste0(g, '.')]
c(paste0("S3method('", g, "','", gsub("^.*\\.", "", o), "')" ),
paste0("export('", o, "')"))
})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment