【问题标题】:How can I locate a directory and automatically set the working directory path to it in R?如何在 R 中找到目录并自动为其设置工作目录路径?
【发布时间】:2018-08-20 21:11:28
【问题描述】:

我在 R 的默认根目录下有一个“Box Sync”文件夹。我正在尝试创建代码,即使我的目录和“Box Sync”文件夹之间还有其他目录,我也可以找到并将其设置为工作目录。

【问题讨论】:

  • 祝代码好运。如果需要任何帮助,请联系 SO

标签: r rstudio


【解决方案1】:

您可以使用list.dirs(recursive=T) 查找工作目录中的所有目录,然后使用endsWith 查找名为Box Sync 的目录。如果需要,您可以进一步过滤这些并使用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])                                        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多