简体   繁体   English

如何确定R中的当前目录名称?

[英]How can I determine the current directory name in R?

The only solution I've encountered is to use regular expressions and recursively replace the first directory until you get a word with no slashes. 我遇到的唯一解决方案是使用正则表达式并递归替换第一个目录,直到您得到不带斜杠的单词为止。

gsub("/\\w*/","/",gsub("/\\w*/","/",getwd()))

Is there anything slightly more elegant? 还有什么更优雅的吗? (and more portable?) (更便携?)

Your example code doesn't work for me, but you're probably looking for either basename or dirname : 您的示例代码对我不起作用,但是您可能正在寻找basenamedirname

> getwd()
[1] "C:/cvswork/data"
> basename(getwd())
[1] "data"
> dirname(getwd())
[1] "C:/cvswork"

如果您不知道基basename (而我也不知道),则可以使用以下代码:

tail(strsplit(getwd(), "/")[[1]], 1)

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

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