Determine path of the executing script

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

    I had issues with the implementations above as my script is operated from a symlinked directory, or at least that's why I think the above solutions didn't work for me. Along the lines of @ennuikiller's answer, I wrapped my Rscript in bash. I set the path variable using pwd -P, which resolves symlinked directory structures. Then pass the path into the Rscript.

    Bash.sh

    #!/bin/bash
    
    # set path variable
    path=`pwd -P`
    
    #Run Rscript with path argument
    Rscript foo.R $path
    

    foo.R

    args <- commandArgs(trailingOnly=TRUE)
    setwd(args[1])
    source(other.R)
    

提交回复
热议问题