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

后端 未结 5 901
孤独总比滥情好
孤独总比滥情好 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:17

    I think that the interactive() function might work.

    This function returns TRUE when R is being used interactively and FALSE otherwise. So just use if (interactive())

    i.e. the equivalent is

    if (!interactive()) {
      main()
    }
    

提交回复
热议问题