Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble setting working directory

Tags:

I created a folder in order for it to be the main work directory meaning all the files I create go there, and files I read will be from there. For some reason after I created the folder and I'm trying to set it as the working directory I get this message:

Error in setwd("~/") : cannot change working directory   

When I try to create files they are saved somewhere different and I get an error when trying to load them. I used the scan function and got:

Error in file(file, "r") : cannot open the connection 

In addition: Warning message:

In file(file, "r") : cannot open file 'ff': No such file or directory 
like image 761
Lee Neumann Avatar asked Nov 13 '15 11:11

Lee Neumann


People also ask

Why is R not letting me change my working directory?

Unfortunately, the RStudio console returns the error message “cannot change working directory”. The reason for this is that the directory we are trying to access does not exist. We might have specified the folder name wrong, or the path before the folder name is not existing.

What does it mean to set working directory?

The working directory in R is the folder where you are working. Hence, it's the place (the environment) where you have to store your files of your project in order to load them or where your R objects will be saved.


1 Answers

The command setwd("~/") should set your working directory to your home directory. You might be experiencing problems because the OS you are using does not recognise "~/" as your home directory: this might be because of the OS, or it might be because of not having set that as your home directory elsewhere.

As you have tagged the post using RStudio:

  • In the bottom right window move the tab over to 'files'.
  • Navigate through there to whichever folder you were planning to use as your working directory.
  • Under 'more' click 'set as working directory'

You will now have set the folder as your working directory. Use the command getwd() to get the working directory as it is now set, and save that as a variable string at the top of your script. Then use setwd with that string as the argument, so that each time you run the script you use the same directory.

For example at the top of my script I would have:

work_dir <- "C:/Users/john.smith/Documents" setwd(work_dir) 
like image 57
JCollerton Avatar answered Sep 23 '22 23:09

JCollerton