Is there an R equivalent of the pythonic “if __name__ == ”__main__“: main()”?

后端 未结 5 903
孤独总比滥情好
孤独总比滥情好 2020-12-13 03:43

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

5条回答
  •  暖寄归人
    2020-12-13 04:23

    Another option is:

    #!/usr/bin/Rscript
    
    # runs only when script is run by itself
    if (sys.nframe() == 0){
    # ... do main stuff
    }
    

提交回复
热议问题