Command for “Set working directory to source file location”

前端 未结 4 1723
眼角桃花
眼角桃花 2021-01-17 14:50

RStudio has a useful feature:

Session -> Set Working Directory -> To Source File Location

Is there a way to do this without using the

相关标签:
4条回答
  • 2021-01-17 15:16

    Slight variation on @Ciro's answer above, for Mac users:

    pathwd<-sub("/dummy.R","",system("find . -type f -name dummy.R",intern=T)[1]) 
    setwd(pathwd)
    

    Replace dummy.R with your filename of course.

    0 讨论(0)
  • You can use :

    source("script.R", chdir = TRUE)
    

    and change "script.R" by the name of the file you're interested in.

    0 讨论(0)
  • 2021-01-17 15:28

    This will work on most systems, it gets a little buggy with Macs.

    dir <- dirname(parent.frame(2)$ofile)
    setwd(dir)
    
    0 讨论(0)
  • 2021-01-17 15:37
    pathwd<-sub("/filename","",system("find -perm -g=w -type f -name 'filename'",intern=T)[1])
    setwd(pathwd)
    

    Make sure your file's name is unique.

    0 讨论(0)
提交回复
热议问题