Determine path of the executing script

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

    If rather than the script, foo.R, knowing its path location, if you can change your code to always reference all source'd paths from a common root then these may be a great help:

    • https://github.com/r-lib/rprojroot or https://rprojroot.r-lib.org/
    • https://here.r-lib.org/

    Given

    • /app/deeply/nested/foo.R
    • /app/other.R

    This will work

    #!/usr/bin/env Rscript
    library(here)
    source(here("other.R"))
    

    See https://rprojroot.r-lib.org/ for how to define project roots.

提交回复
热议问题