Grant Rettke
2014-Aug-10 17:22 UTC
[Rd] How to redefine `require' to generate a warning and delegate work to the original require?
Good afternoon,
My goal is to warn the user any time that they use the `require'
function. The reason is that they probably wanted to use `library'
instead. There is a case where they should use the former though, so I
just want to issue a warning.
My approach was to:
? Re-bind `require' to `original.require'
? Re-implement `require' to
? Warn the user
? Delegate the real work back to `original-require'
? Like this inside of my `.Rprofile':
?????
? .First <- function() {
? gcr <- new.env()
? gcr$original.require(...) <- require
? gcr$require <- function(...) {
? warning("Are you sure you wanted `require` instead of
`library`?")
? original.require(...)
? }
? base::attach(gcr, name="gcr", warn.conflicts = FALSE)
? }
?????
On startup I get the following error though:
?????
? Error in gcr$original.require(...) <- require :
? '...' used in an incorrect context
?????
What am I doing wrong here and what should I have read to grok my
mistake?
?????
? > sessionInfo()
? R version 3.1.1 (2014-07-10)
? Platform: x86_64-apple-darwin13.2.0 (64-bit)
?
? locale:
? [1] en_US/en_US/en_US/C/en_US/en_US
?
? attached base packages:
? [1] stats graphics grDevices utils datasets methods base
?
? loaded via a namespace (and not attached):
? [1] compiler_3.1.1 tools_3.1.1
?????
Kind regards,
Grant Rettke | ACM, ASA, FSF, IEEE, SIAM
gcr at wisdomandwonder.com | http://www.wisdomandwonder.com/
?Wisdom begins in wonder.? --Socrates
((? (x) (x x)) (? (x) (x x)))
?Life has become immeasurably better since I have been forced to stop
taking it seriously.? --Thompson
Jeroen Ooms
2014-Aug-10 18:14 UTC
[Rd] How to redefine `require' to generate a warning and delegate work to the original require?
On Sun, Aug 10, 2014 at 7:22 PM, Grant Rettke <gcr at wisdomandwonder.com> wrote:> > ? Error in gcr$original.require(...) <- require : > ? '...' used in an incorrect contextI think you mean: gcr$original.require <- base::require. You don't need the parentheses since you are not defining or calling a function. You are simply assigning an object to another environment. Basic questions about R usage/syntax like these are better suited for the r-help list or stack-overflow. Have a look at: https://stat.ethz.ch/mailman/listinfo/r-devel.