[Rd] How to redefine `require' to generate a warning and delegate work to the original require?

Grant Rettke gcr at wisdomandwonder.com
Sun Aug 10 19:22:46 CEST 2014


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



More information about the R-devel mailing list