I would like R scripts to have a main() function that gets executed while in interactive mode. But the main() function should not be executed while sourcing the file.
There is already a question about this and a very good answer suggests using the interactive() function. However this doesn't work for me. I don't have enough reputation points to comment or answer in that question. So I ask the question here again.
I write this in script_1.r
if(interactive()){
your main code here
}
If I use knitr to waive a html or pdf document, sourcing the script. This code under if(interactive())
won't be executed. This is good for me, that's what I want.
My problem is that if I source("script_1.r")
from script_2.r
in interactive mode, it will still run the code under this if(interactive())
part.