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?)
有什么稍微优雅的吗? (而且更便携?)
2 个解决方案
#1
41
Your example code doesn't work for me, but you're probably looking for either basename
or dirname
:
您的示例代码对我不起作用,但您可能正在寻找basename或dirname:
> getwd()
[1] "C:/cvswork/data"
> basename(getwd())
[1] "data"
> dirname(getwd())
[1] "C:/cvswork"
#2
7
If you didn't know basename
(and I didn't), you could have used this:
如果你不知道basename(我没有),你可以使用它:
tail(strsplit(getwd(), "/")[[1]], 1)
#1
41
Your example code doesn't work for me, but you're probably looking for either basename
or dirname
:
您的示例代码对我不起作用,但您可能正在寻找basename或dirname:
> getwd()
[1] "C:/cvswork/data"
> basename(getwd())
[1] "data"
> dirname(getwd())
[1] "C:/cvswork"
#2
7
If you didn't know basename
(and I didn't), you could have used this:
如果你不知道basename(我没有),你可以使用它:
tail(strsplit(getwd(), "/")[[1]], 1)