Determine path of the executing script

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

    Amazing there is no '$0' type structure in R! You can do it with a system() call to a bash script written in R:

    write.table(c("readlink -e $0"), file="scriptpath.sh",col=F, row=F, quote=F)
    thisscript <- system("sh scriptpath.sh", intern = TRUE)
    

    Then just split out the scriptpath.sh name for other.R

    splitstr <- rev(strsplit(thisscript, "\\/")[[1]])
    otherscript <- paste0(paste(rev(splitstr[2:length(splitstr)]),collapse="/"),"/other.R")
    

提交回复
热议问题