Determine path of the executing script

前端 未结 28 3135
再見小時候
再見小時候 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:36

    I also had this problem, and none of the above solutions worked for me. Maybe with the source or things like that, but it was not clear enough.

    I found this, for me elegant, solution:

    paste0(gsub("\\", "/", fileSnapshot()$path, fixed=TRUE),"/")
    

    The important thing in that is the fileSnapshot() that gives you a lot of information about a file. It returns a list of 8 elements. When you pick path as the list element, the path is returned with \\ as separator, so the rest of the code is just to change that.

    I hope this helps.

提交回复
热议问题