简体   繁体   English

如何找到目录并在R中自动设置工作目录路径?

[英]How can I locate a directory and automatically set the working directory path to it in R?

I have a "Box Sync" folder directly under my R's default root directory. 我直接在R的默认根目录下有一个“ Box Sync”文件夹。 I am trying to create code such that even if there are other directories in between my directory and the "Box Sync" folder, I can locate and set that as the working directory. 我正在尝试创建代码,以便即使我的目录和“ Box Sync”文件夹之间还有其他目录,我也可以找到该目录并将其设置为工作目录。

You can use list.dirs(recursive=T) to find all directories in your working directory, then use endsWith to find those which are named Box Sync themselves. 您可以使用list.dirs(recursive=T)在工作目录中找到所有目录,然后使用endsWith查找本身名为Box Sync You can further filter these if needed and assign with setwd : 您可以根据需要进一步过滤它们,并使用setwd分配:

dirs <- list.dirs(recursive = T)  # List all directories in the working directory
box_dirs <- dirs[endsWith(dirs, 'Box Sync')]  # Show the ones ending with 'Box Sync'

box_dirs
    [1] "./Box Sync"
    [2] "./Library/Application Support/Box/Box Sync"                                    
    [3] "./Library/Logs/Box/Box Sync"           

setwd(box_dirs[1])                                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM