我尽量把所有的R脚本代码行都控制在80个字符以内。当涉及到字符串时,这可能被证明是一个挑战,但通常只是换行,而不使用任何特殊的字符作品,如下所示:
plot(x, y, main = "some reeeealy long title, so long that
I need to break it into several lines
in order to satisfy my ****-retentive
self.")然而,像setwd()这样的一些函数就是不让我这么做。例如,运行
setwd("/folder/another folder/yet another folder/
what are you doing, hiding pr0n?/I think I've made my point/")返回以下错误:
Error in setwd("/folder/another folder/yet another folder/\n
what are you doing, hiding pr0n?/I think I've made my point/") :
cannot change working directory我试过在斜杠字符以外的不同点上刹车线,但我不能让它工作。我能找到的唯一解决办法是运行
setwd(paste("/folder/another folder/yet another folder/",
"what are you doing, hiding pr0n?/I think I've made my point/",
sep = "")这是可行的,但为了尊重一些自定义的规则,似乎有很多混乱之处。
有没有更优雅的方式来实现这一点?
发布于 2012-03-06 02:22:45
一般来说,paste是我能想到的唯一方法,然而,在这种特殊情况下,file.path是比paste更好的选择,因为它自动为您的平台提供正确的分隔字符。
file.path("/folder", "another folder", "yet another folder",
"what are you doing, hiding pr0n?",
"I think I've made my point")发布于 2012-03-06 02:23:48
将它们粘贴在一起,如下所示:
x <-paste("/folder/another folder/yet another folder/",
"what are you doing, hiding pr0n?/I think I've made my point/",
"and for good measure/", sep="")
setwd(x)发布于 2012-03-06 02:33:35
实现这一点的优雅方法(或者至少是我现在能想到的):
~/Documents/Work/Active Projects/Project name/code/中,但我有一个简单的别名,这样当我引用这样的files/data.~/code/project name/,这样就可以在.rprofile中的options()中存储很多内容。示例:options(Path='/really/long/path/to/something')。然后,您可以只使用setwd(getOption('Path'))https://stackoverflow.com/questions/9571423
复制相似问题