8

Hi I have a package with a functions that makes a gui with gWidgets, is there a way I can get this to be executed on package load so the interface comes up straight away.

I thought something like this:

.OnAttach <- function(libname, pkgname){
  gui()
}

would work, but I'm still learning about hooks, and the events during package load and unload, and R environment stuff.

EDIT:

This is what I currently have in my zzz file:

.onLoad <- function(libname = find.package("GspeEaR"), pkgname = "GspeEaR") {
  gui()
}

Thanks,

Ben W.

SJWard
  • 3,629
  • 5
  • 39
  • 54
  • 2
    have you checked out `.onLoad` you dump it in a .R file called zzz traditionally. Here's an example where I've used this: https://github.com/trinker/gmailR/blob/master/R/zzz.R – Tyler Rinker Feb 17 '13 at 22:40
  • @TylerRinker, why don't you make this an answer? – Arun Feb 17 '13 at 23:22

1 Answers1

4

I think you want .onLoad which you dump in a .R file called zzz.R (traditionally). Here's an example where I've used this: github.com/trinker/gmailR/blob/master/R/zzz.R

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I've just tried this, I'll have to try it on my PC later because I'm not sure if this is solely due to my being on a mac at work, but when I build and reload in RStudio:Error : .onLoad failed in loadNamespace() for 'GspeEaR', details: call: function (classes, fdef, mtable) error: unable to find an inherited method for function ‘.gwindow’ for signature ‘"NULL"’ Error: loading failed – SJWard Feb 18 '13 at 14:16
  • Just tried on my PC, same error happens. The error is beyond my deciphering abilities, I've updated my question show show what I currently have in my zzz file. – SJWard Feb 18 '13 at 20:37
  • @TylerRikner why do you define the `.onload` in this way? I mean why are initializing parameters ? I have used your answer [here](http://stackoverflow.com/questions/26045999/how-to-check-platform-in-onload-in-the-r-package/26046264?noredirect=1#comment40904153_26046264) but it looks wrong.(see hadley comment below my answer in the mentioned link) – agstudy Sep 29 '14 at 19:15
  • Not sure. I'd like to see Hadley expand on how `.onload` does work. PS I no long use this method in that package as you can click the link and see. Here's a bit on it here: http://www.r-bloggers.com/a-dirty-hack-for-importing-packages-that-use-depends/ And this is where my introduction to `.onLoad` first began: http://stackoverflow.com/a/10508191/1000343 But I don't really use it so am likely misunderstanding it. – Tyler Rinker Sep 29 '14 at 20:29
  • @TylerRinker thanks. I just update my answer in the other question. Thanks again for your feedback. – agstudy Sep 30 '14 at 05:26