2

I create convenience packages with functions specific to different companies that I work with. I'd like to be able to load ggplot2 themes and geom defaults when I load those libraries.

For example:

update_geom_defaults("line",   list(size = 2))
update_geom_defaults("bar",    list(fill="#BEBADA",colour="#000000"))
update_geom_defaults("text",   list(colour="#333333"))
scale_colour_discrete <- function(...) scale_colour_brewer(..., palette="Set2")
scale_fill_discrete <- function(...) scale_fill_brewer(... , palette="Set2")

Right now, I have to remember to retype this every time I do a quick analysis, and I'd like to make it more DRY.

How would I go about forcing these elements to load when the package does?

pseudocode:

library(convenience.lib)
## run the above ## 
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • Take a look at the first answer here [Easy wait to list my data ...](http://stackoverflow.com/questions/15706872/easy-way-to-list-my-data-but-not-my-functions-in-r-hide-functions-from-ls) – Mars Apr 21 '13 at 04:41

1 Answers1

1

Simple add an .onLoad function to your package, for example:

.onLoad <- function(libname = find.package("mypackage"), pkgname = "mypackage") {
    update_geom_defaults("line",   list(size = 2))
    update_geom_defaults("bar",    list(fill="#BEBADA",colour="#000000"))
    update_geom_defaults("text",   list(colour="#333333"))
    scale_colour_discrete <- function(...) scale_colour_brewer(..., palette="Set2")
    scale_fill_discrete <- function(...) scale_fill_brewer(... , palette="Set2")
}
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255