1

有没有办法使用setwd()不同级别的R函数返回目录?

例如

> getwd() 
  /home/folder1/folder2/

我想在不打字的情况下一口气到家setwd("../..")。写n次“../”很繁琐

4

2 回答 2

3

一种方法是"../.."动态创建路径。

setwd_n_levels <- function(n) {
  setwd(paste0(rep('..', n), collapse = '/'))
}

setwd_n_levels(2)
getwd()
于 2021-03-15T09:55:54.587 回答
0

我们可以用strrep

setwd_n_levels <- function(n) {
   setwd(trimws(strrep('../', n), whitespace = '/'))
  }
于 2021-03-15T17:58:15.847 回答