Skip to content

Instantly share code, notes, and snippets.

@setempler
Created July 23, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save setempler/7fcf2a3a737ce1293e0623d2bb8e08ed to your computer and use it in GitHub Desktop.
Save setempler/7fcf2a3a737ce1293e0623d2bb8e08ed to your computer and use it in GitHub Desktop.
Detect duplicated function definitions from R source files
#!/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)
@setempler
Copy link
Author

shell version (without file and lines):
sed -e 's/^[\ \t]*//' -e 's/#.*//' R/* | awk '/function/{print $1}' | sort | uniq -d

@setempler
Copy link
Author

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

@setempler
Copy link
Author

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