Created
July 23, 2016 15:53
-
-
Save setempler/7fcf2a3a737ce1293e0623d2bb8e08ed to your computer and use it in GitHub Desktop.
Detect duplicated function definitions from R source files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
f <- list.files("R/", full.name = TRUE) # run from package source, or adjust path | |
d <- lapply(setNames(f, sub("//", "/", f)), function(x) | |
{ | |
x <- readLines(x) | |
names(x) <- seq_along(x) # tag line number | |
x <- sub("#.*", "", x) # delete comments | |
x <- sub("^[\ \t]*", "", x) # delete leading whitespace | |
x <- x[grepl("function", x)] # get function definitions | |
x <- sub("[\ ]+.*", "", x) # get function names | |
x | |
}) | |
d <- unlist(d) | |
d <- d[order(d)] | |
d <- d[d %in% unique(d[duplicated(d)])] | |
cat("duplicated functions:\n") | |
null <- Map(function(n,v){cat(n, ": ", v, "\n", sep = "")}, names(d), d) |
with file and line information:
grep -n -f <(sed -e 's/^[\ \t]*//' -e 's/#.*//' R/* | awk '/function/{print $1}' | sort | uniq -d) R/* | grep function
also:
for f in $(sed -e 's/^[\ \t]*//' -e 's/#.*//' R/* | awk '/function/{print $1}' | sort | uniq -d); do echo $f; grep -n "$f" R/* | sed 's/#.*//' | grep function; done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shell version (without file and lines):
sed -e 's/^[\ \t]*//' -e 's/#.*//' R/* | awk '/function/{print $1}' | sort | uniq -d