问题
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.
回答1:
You could use interactive
to test if R is running in interactive mode. interactive
will return FALSE
under Rscript and TRUE
under (most?) GUIs.
来源:https://stackoverflow.com/questions/9776064/how-do-i-test-if-r-is-running-as-rscript