Skip to content
Prev 960 / 12125 Next

[R-pkg-devel] duplicate function during build

On Sat, 23 Jul 2016, ProfJCNash <profjcnash at gmail.com> writes:
You could use R functionality to rewrite the shell
commands. Perhaps along those lines:

--8<---------------cut here---------------start------------->8---
fun_names <- function(dir,
                      duplicates_only = TRUE,
                      file_pattern = "[.][rR]$",
                      fun_pattern = " *([^\\s]+) *<- *function.*") {

    files <- dir(dir, pattern = file_pattern, full.names = TRUE)
    ans <- data.frame(fun = character(0),
                      file = character(0))
    for (f in files) {
        txt <- readLines(f)
        fun.lines <- grepl(fun_pattern, txt)
        
        if (any(fun.lines)) {
            ans <- rbind(ans,
                         data.frame(fun = gsub(fun_pattern, "\\1",
                                               txt[fun.lines],
                                               perl = TRUE),
                                    file = f,
                                    line = which(fun.lines),
                                    stringsAsFactors = FALSE))
        }
    }

    ans <- ans[order(ans[["fun"]]), ]

    if (duplicates_only) {
        d <- duplicated(ans[["fun"]])
        d0 <- match(unique(ans[["fun"]][d]), ans[["fun"]])
        ans <- ans[sort(c(d0, which(d))),]
    }

    ans
}
--8<---------------cut here---------------end--------------->8---

One would call then function on a directory.

For instance,

  fun_names("~/Packages/NMOF/R")

gives me output

         fun                                    file line
10  cfHeston       /home/es/Packages/NMOF/R/callCF.R   41
18  cfHeston /home/es/Packages/NMOF/R/callHestoncf.R   29

## [...] 

But it will be tricky to catch only such re-definitions
of functions that have been left in the files by
mistake. For instance, I often define short helper
functions within other functions, and such helper
functions might then get flagged, too.

Kind regards
        Enrico

  
    
Message-ID: <87invqsth0.fsf@enricoschumann.net>
In-Reply-To: <734d029b-c49d-7a07-28ce-010f4776283c@gmail.com> (profjcnash@gmail.com's message of "Sat, 23 Jul 2016 13:20:01 -0400")