Specify directory with setwd()

本小妞迷上赌 提交于 2019-12-09 13:42:16

问题


I understand that in R to set my working directory I can do:

setwd("~/dir1/dir2/mydir")

If mydir is a unique directory name, is there way to specify the working directory without stating dir1/dir2 ?

Thanks


回答1:


Sys.glob does what you want:

setwd(Sys.glob("~/*/*/mydir"))

If you don't know how deep it's nested, you can use list.files or dir:

list.files(path="~", pattern="mydir", all.files=TRUE, full.names=TRUE,
  recursive=TRUE, ignore.case=FALSE, include.dirs=TRUE)


来源:https://stackoverflow.com/questions/12324539/specify-directory-with-setwd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!