Determine path of the executing script

前端 未结 28 2966
再見小時候
再見小時候 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条回答
  •  萌比男神i
    2020-11-22 08:26

    The answer of rakensi from Getting path of an R script is the most correct and really brilliant IMHO. Yet, it's still a hack incorporating a dummy function. I'm quoting it here, in order to have it easier found by others.

    sourceDir <- getSrcDirectory(function(dummy) {dummy})

    This gives the directory of the file where the statement was placed (where the dummy function is defined). It can then be used to set the working direcory and use relative paths e.g.

    setwd(sourceDir)
    source("other.R")
    

    or to create absolute paths

     source(paste(sourceDir, "/other.R", sep=""))
    

提交回复
热议问题