Re: [R] Is it possible to detect whether running as Rscript?

2009-11-10 Thread Duncan Murdoch

Peter Meilstrup wrote:
I would like to write a block of code that runs when a script is being  
run from Rscript, but not to run if the same file is being source()d,  
or submitted via ESS, etc. As a gloss I would like to write a file  
that looks somewhat like:


#!/usr/bin/env Rscript

my.func <- function(...) {
#do something...
}

if ( #is.running.Rscript# ) {
do.call(my.func, commandArgs(trailingOnly=TRUE))
}

If it helps, I'm thinking of something similar to the Python idiom of  
writing


#!/usr/bin/env python

#class, function definitions

if (__name__=='__main__'):
#code to be executed when running this file directly

The intent is to use R's debugging facilities to help solve a problem  
that occurs when a script is invoked (from a makefile) with particular  
arguments. So if the makefile invokes "my.script datafile" then I can  
track down the problem by sourcing the script into R and trying  
my.func("datafile")


Is this doable, or is there a better alternative?
I don't know if it gives enough detail, but you could look at the 
interactive() and/or commandArgs().


Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Is it possible to detect whether running as Rscript?

2009-11-10 Thread Peter Meilstrup
I would like to write a block of code that runs when a script is being  
run from Rscript, but not to run if the same file is being source()d,  
or submitted via ESS, etc. As a gloss I would like to write a file  
that looks somewhat like:


#!/usr/bin/env Rscript

my.func <- function(...) {
#do something...
}

if ( #is.running.Rscript# ) {
do.call(my.func, commandArgs(trailingOnly=TRUE))
}

If it helps, I'm thinking of something similar to the Python idiom of  
writing


#!/usr/bin/env python

#class, function definitions

if (__name__=='__main__'):
#code to be executed when running this file directly

The intent is to use R's debugging facilities to help solve a problem  
that occurs when a script is invoked (from a makefile) with particular  
arguments. So if the makefile invokes "my.script datafile" then I can  
track down the problem by sourcing the script into R and trying  
my.func("datafile")


Is this doable, or is there a better alternative?

Peter

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.