简体   繁体   English

错误 setwd() “无法更改工作目录”

[英]Error setwd() “cannot change working directory”

I get "cannot change working directory" error when I try to set up my working directory:尝试设置工作目录时出现“无法更改工作目录”错误:

    setwd("C:\Users\alimo\Desktop\DataVisualizationwithggplot2.R")
*Error: '\U' used without hex digits in character string starting ""C:\U"*

then I did it然后我做到了

options(PACKAGE_MAINFOLDER="C:/Users/...")

then I replaced all " \ " to "/" but I got it this time:然后我将所有“\”替换为“/”,但这次我明白了:

cannot change working directory

Please help me.请帮我。

Yes, writing a path to a file or directory can sometimes be a bit painful, especially when you move across different platforms!是的,写入文件或目录的路径有时会有点痛苦,尤其是当您跨不同平台移动时!

setwd() sets the working directory, so it means you need to specify a directory, not a file. setwd()设置工作目录,所以这意味着你需要指定一个目录,而不是一个文件。

And whenever I'm not sure about the single/double (back)slashes, I like to use file.path() from base R, which adds a correct delimiter in a platform-independent way:每当我不确定单/双(反)斜杠时,我喜欢使用基础 R 中的file.path() ,它以独立于平台的方式添加正确的分隔符:

file.path("~", "myfolder", "myfile.R")

So for your case:所以对于你的情况:

setwd(file.path("C:", "Users", "alimo", "Desktop"))

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

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