简体   繁体   中英

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. I tried file.path("./data") and then wanted to show the file with this command file.show("testFile.r") but it gives this error:

Error: File testFile.r does not exist.

And the command getwd() gives the previous current directory. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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