简体   繁体   English

如何在R中将当前目录设置为其他目录?

[英]How can I set different directory to the current directory in R?

I'm trying to open a file that does not exist in my current directory. 我正在尝试打开当前目录中不存在的文件。 This file is named testFile.r existing in the data folder in my current directory. 该文件名为testFile.r存在于我当前目录的data文件夹中。 I tried file.path("./data") and then wanted to show the file with this command file.show("testFile.r") but it gives this error: 我尝试了file.path("./data") ,然后想要使用此命令file.show("testFile.r")显示文件,但是它给出了此错误:

Error: File testFile.r does not exist. 错误:文件testFile.r不存在。

And the command getwd() gives the previous current directory. 然后命令getwd()给出先前的当前目录。 So any thoughts on this? 那么对此有何想法?

You change your current directory using the command setwd(new_directory) (replacing new_directory with the path to the directory you want). 您可以使用命令setwd(new_directory)更改当前目录(将new_directory替换为所需目录的路径)。

If you'd rather stay in your current directory, just do 如果您希望保留在当前目录中,只需执行

file.show("./data/testFile.r")

To keep track of multiple paths, you can save each as a variable and use the paste function to reference the file: 要跟踪多个路径,可以将每个路径另存为变量,然后使用粘贴功能来引用文件:

path1 <- "./data/"
path2 <- "./second_folder_name/"
file.show(paste0(path1, "testFile.R"))
file.show(paste0(path2, "testFile.R"))

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

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