Here is the function I wrote. It wraps the base::source function to store a list of sources in a global environment list called sourced . It will only redirect the file if you provide the argument .force=TRUE to call the source. Its argument signature is otherwise identical to the real source() , so you don't have to rewrite your scripts to use this.
warning("overriding source with my own function FYI") source <- function(path, .force=FALSE, ...) { library(tools) path <- tryCatch(normalizePath(path), error=function(e) path) m<-md5sum(path) go<-TRUE if (!is.vector(.GlobalEnv$sourced)) { .GlobalEnv$sourced <- list() } if(! is.null(.GlobalEnv$sourced[[path]])) { if(m == .GlobalEnv$sourced[[path]]) { message(sprintf("Not re-sourcing %s. Override with:\n source('%s', .force=TRUE)", path, path)) go<-FALSE } else { message(sprintf('re-sourcing %s as it has changed from: %s to: %s', path, .GlobalEnv$sourced[[path]], m)) go<-TRUE } } if(.force) { go<-TRUE message(" ...forcing.") } if(go) { message(sprintf("sourcing %s", path)) .GlobalEnv$sourced[path] <- m base::source(path, ...) } }
This is pretty talkative (lots of message() calls), so you can take these lines if you like it. Any advice from R veteran users is welcome; I am new to R.
Keith Twombley May 16 '13 at 17:16 2013-05-16 17:16
source share