For an R package I am designing I'd like to programmatically define a bunch of functions in a for loop. For example, I might want to do something like make new versions of base functions which always ignore NA values. Like:
for(f in c('mean', 'sd', 'median', 'quantile', 'max', 'min', 'range')) {
cur = function() {}
formals(cur) = formals(args(get(f)))
body(cur) = parse(text = paste0(f,
'(',
names(formals(cur))[1],
', na.rm = TRUE)'))
assign(paste0(f, 'x'), cur)
}
This code creates seven new functions (meanx, sdx, medianx, quantilex, etc.) which simply call their eponymous functions (mean, sd, median, quantile, etc.) with na.rm = TRUE.
My problem is this: How can I use roxygen to document these seven functions? At very least, I'd like to @export them, but if I put a #' @export line into the loop, roxygen doesn't do anything with it. Obviously, I could write the names into the NAMESPACE file myself, but if I ever use roxygen again it will overwrite my changes.
Any ideas?
(Just to be clear, this is just an illustrative example. These seven functions are not really the functions I'm defining, so I'm not looking for advice on how to automatically use na.rm or anything. I'm interested in ideas for how to make roxygen export functions defined in a loop into the NAMESPACE file.)