The objective is to have two simple ways to source some code, say func.R, containing a function. Calling R CMD BATCH func.R initializes the function and evaluat
R CMD BATCH func.R
I think that the interactive() function might work.
interactive()
This function returns TRUE when R is being used interactively and FALSE otherwise. So just use if (interactive())
TRUE
FALSE
if (interactive())
i.e. the equivalent is
if (!interactive()) { main() }