13

I have code in a single R file that I want to be able to source (i.e., to define my functions etc.) within RStudio during development, and also run using the #! /usr/bin/env Rscript syntax via the command line (actually, using Hadoop). For the latter, I need the last thing that Rscript does to be to kick off the analysis (i.e., using a call to a main() function). For the former, I don't want my main() function called. I'd like to be able to test if the code is running within Rscript (or, alternatively, within RStudio), so that I can either execute main() or not. Is this possible, please?

One solution would be to break my code into multiple files, but I'd rather avoid this if possible (to make the Hadoop stuff slightly easier).

Thanks in advance.

Chris
  • 941
  • 9
  • 18
  • Thanks for mentioning RStudio - I wasn't aware of this software program until now, but I'm glad I 'found' it. – Jos Mar 20 '12 at 12:43
  • No worries, Jura25. It's much less mature than the Matlab or Mathematica front ends, but does the job very well for R. – Chris Mar 20 '12 at 13:43

1 Answers1

14

You could use interactive to test if R is running in interactive mode. interactive will return FALSE under Rscript and TRUE under (most?) GUIs.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418