evilphil
evilphil

Reputation: 41

.onLoad and interactive - unexpected behaviour?

I have a package which is intended to be used by non-interactive scripts run via Rscript. But I also want to be able to load it up in an interactive session, mostly for debugging purposes. When used in an Rscript or other non-interactive session I want to use .onLoad to do some initialisation, so I have something along the lines of:

.onLoad <- function(libname, pkgname) {
    if ( !interactive() ) { 
        # Do some stuff 
    } else {
    # Do something slightly different
}

The problem is that even in an interactive session, interactive() is returning FALSE inside .onLoad. I test this via:

.onLoad <- function(libname, pkgname) {
    print( paste( "interactive() = ", interactive(), collapse="" ) )
}

Then when I build() and install() (using devtools) the installation attaches the library with the message "interactive() = FALSE".

Any ideas? Neither the documentation for .onLoad() nor for interactive() say anything pertinent.

Phil

Upvotes: 3

Views: 175

Answers (1)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162331

After you run build() and install(), have you tried un-attaching and un-loading the package, then reloading it?

Here's what I get when I do that, including your .onLoad function in a package called 'dummy':

 library(devtools)
 document('dummy')
 build('dummy')
 install('dummy')
 # ...
 # [1] "interactive() =  FALSE"

 detach("package:dummy", unload=T)
 library(dummy)
 # [1] "interactive() =  TRUE"

Upvotes: 2

Related Questions