Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if R is running as Rscript?

Tags:

r

hadoop

rscript

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.

like image 844
Chris Avatar asked Mar 19 '12 18:03

Chris


1 Answers

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

like image 125
Joshua Ulrich Avatar answered Oct 17 '22 03:10

Joshua Ulrich