gpt4 book ai didi

Error setwd() "cannot change working directory" [closed](错误setwd()“无法更改工作目录”[已关闭])

转载 作者:bug小助手 更新时间:2023-10-27 20:04:50 38 4
gpt4 key购买 nike




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 ran:

然后我跑了起来:


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

and replaced all "" to "/" but I got this error this time:

并将所有的“”替换为“/”,但这一次我得到了这个错误:



cannot change working directory


更多回答

Either setwd("C:\\Users\\alimo\\Desktop\\DataVisualizationwithggplot2.R") or setwd("C:/Users/alimo/Desktop/DataVisualizationwithggplot2.R")

Setwd(“C:\\Users\\alimo\\Desktop\\DataVisualizationwithggplot2.R”)或setwd(“C:/Users/alimo/Desktop/DataVisualizationwithggplot2.R”)

But you cannot change directory to an R file, consider setwd("C:/Users/alimo/Desktop")

但您不能将目录更改为R文件,请考虑setwd(“C:/USERS/ALIMO/Desktop”)

优秀答案推荐

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"))

更多回答

(1) double-backslashes are only required if the user chooses to use backslashes; on Windows, R will accept either, so there really is no need to type in backslashes (though other commands/env-vars will still contain backslashes). (2) Good use of file.path, further good to know is that it has an argument fsep= which defaults to \` on windows and /` everywhere else. When I use file.path, even on windows I almost always use fsep="/" since backslashes hurt my eyes. However, once can always get the file-sep with .Platform$file.sep (default value for file.path, btw).

(1)只有当用户选择使用反斜杠时,才需要双反斜杠;在Windows上,R会接受任何一个,因此确实不需要输入反斜杠(尽管其他命令/env-var仍将包含反斜杠)。(2)很好地使用了file.path,另外要知道的是,它有一个参数fsep=,该参数在Windows上缺省为\`,在其他地方缺省为/`。当我使用file.path时,即使是在Windows上,我几乎总是使用fsep=“/”,因为反斜杠会伤害我的眼睛。但是,ONCE总是可以使用.Platform$file.sep(文件路径的缺省值,btw)获得文件-sep。

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com