Determine path of the executing script

前端 未结 28 2484
再見小時候
再見小時候 2020-11-22 08:01

I have a script called foo.R that includes another script other.R, which is in the same directory:

#!/usr/bin/env Rscript
message(\         


        
28条回答
  •  终归单人心
    2020-11-22 08:22

    This works for me. Just greps it out of the command line arguments, strips off the unwanted text, does a dirname and finally gets the full path from that:

    args <- commandArgs(trailingOnly = F)  
    scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))
    

提交回复
热议问题