Having trouble setting working directory - r

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

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)

This may help...
use the following code and browse the folder you want to set as the working folder
setwd(choose.dir())

I just had this error message happen. When searching for why, I figured out that there's a related issue that can occur if you're not paying attention - the same error occurs if the directory you are trying to move into does not exist.

Maybe it is the case that you have your path in couple of lines, you used enter to make it? If so, then part of you paths might look like that "/\nData/" instead of "/Data/", which causes the problem. Just set it to be in one line and issue is solved!

Related

Why do I keep getting an error when trying to import my file?

I'm trying to read csv file using R notebook and keep getting this error:
Error: 'Examples/data/starbucks.csv' does not exist in current
working directory ('C:/Users/c227466/Desktop')
I'm not sure what's going on!
This is the code I used:
starbucks <- read_csv("Examples/data/starbucks.csv")
starbucks
Your working directory is your desktop (which is not recommended). On your desktop, you should add a folder "Examples" and, in it, another folder "data" and your file there.

R studio will not remember my file path for a code chunk

When I run the following code I get an error.
library(tidyverse)
day <- read.csv("day.csv")[, -1]
glimpse(day)
Result:
Show in New Window
Error in file(file, "rt") : cannot open the connection
However when I specify the file path it works:
library(tidyverse)
setwd("~/UofUProjects/IS6489")
day <- read.csv("day.csv")[, -1]
glimpse(day)
I don't want to have to specify the file path for every single code chunk I run. Is this a setting I may have accidentally disabled? I tried deleting an re adding the file. I also tried clicking more and choosing set working directory and I still get the same error. I also tried updating Rstudio and R to the newest versions.
here is my working directory
The R markdown file I was using was saved in my download folder instead of my project folder. I was trying to specify the working directory folder with the R studio settings, and thought it would recognize however it did not work unless I actually saved the R markdown file in the project folder or used setwd(). I ran the code without the setwd() after I saved in the correct location and it worked just the way I wanted it to.

Error in gzfile(file, "rb") - what should I do?

I'm going to write shortly, because my english is not perfect.
My code is:
explanatory=readRDS("explanatory_complete.Rds")
And I want knit pdf or word document, I receive this message:
Error in gzfile(file, "rb")- can't oppen connection
Where is problem? I set the right working directory.
Try with explanatory<-readRDS('filename.rds') this works for me.
Not sure if this is your case but I ran into
Error in gzfile(file, "rb")- can't open connection
when I tried to read a large file after it was downloaded using rsync. This was due to different file access permissions set by rsync than my Windows 10 system. The error was fixed in a local MobaXterm session using
chmod u+rwx filename
(change filename to your files)
The file likely doesn't exist in your working directory. This often happens to me when I have changed my working directory and forgotten to change it back.
Have you tried saving the new R/RMD file first?
I faced the same issue and problem was that, I was not saving the new file first therefore the R session was unable to understand the path for RDS file.
After I saved the file (in the same folder, but not a necessity) and ran the readRDS, it worked.
I had this issue. For me, it was being caused by an update.packages() call in a script my RMD document was sourcing. I'm sure there is a more elegant solution, but I just put a # in front of this line of code for knitting this RMD and then removed it again after
Check your exact file name and the working directly. it might be .rds missing in your saved file name.

Working directory error

I am working with PyCharm 4.04. Since I installed it, every time I open it, as default directory I get this:
C:\Users\Laura\AppData\Local\Temp\main.py1.tmp>cd
Which gives me an error when trying to use the console:
Error:Cannot start process, the path specified for working directory
is not a directory
But even if I change the directory, the message does not disappear.
The terminal, though, it does work and I can run projects, but I would like to use the console.
Another solution is to close the project, run rm -rf .idea and re-open it. Apparently Pycharm gets confused by some direct folder manipulation and doesn't reflect it properly in his .idea/*.xml files
I also got this error, and it got resolved by setting the default working directory. Follow the below path, and set the Working Directory to the folder where your code resides.
File > Settings > Build, Execution, Deployment > Console > Python Console > Working Directory ...
I finally solved the problem.
I think it all started because the first project that I opened with pycharm was in my "download" folder, so the working directory was automatically set to a temporal folder by default and allthough I moved the project to another folder and I manually changed the working directory from the terminal, it was not working.
The solution was creating a new project and giving a correct path to the new project. It seems very easy but it was not that obvious.
In the upper right corner click on small ▼ next to your main to run (look to the left from green right-pointing triangle)
Select Edit configurations.
In ▼ Python select the proper configuration name.
Look at the Configuration panel.
Fix items Script and Working directory.
pycharm
I had this same problem and just had to reinstall pycharm. It's a quickfix and I can't ensure it won't happen again.
I solved this by replacing all instances of the old filename and old directory with the new one in .idea/workspace.xml
It can be done with PyCharm running.
#user1068430 has the answer in the comments to the question:
When you open a project open the directory not a specific python file.
Instead of ~/Documents/myProject/main.py open ~/Documents/myProject
If you "open" the .py file then you'll have to set the working directory (File > Settings > Build, Execution, Deployment > Console > Python Console > Working Directory) every time. If you "open" the directory containing the .py file, then PyCharm will open and all of your .py files will be available in the left window. Select one of them and you're good to go.
i had the same issue, the error comes up when i want to upgrade my packages and when i run my project "this FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\youruser\AppData\Local\Temp\gen_py\3.7\__init__.py'" i found that the Temp file in C:\Users\youruser\AppData\Local was corrupt and it was fixed by deleting the "Temp" file, once deleted it will automatically create a new one with "gen_py" in it
To anyone with similar issues: Python interpreter virtual environment is where your python.exe sits. The working directory is where your script sits. To make everything easier, open a new project, scroll to location where you script is stored, and select. Click the interpreter option, click existing (if old one worked) or choose the python.exe. When asked, open the project in a new window, close old one to avoid confusion.
source = banging my head against the console for past few hours.
if the above mentioned solutions are not working, you can restart a new project.
file > New project...
then,
create a new project.

R list.files(my_working_directory) shows no files but I know they are there. How to fix?

I'm trying to work with R using a directory in my external hard drive as working directory. I can set the working directory to where I want, but when I use list.files R can't see anything. I also can't use read.delim to load a file.
For example:
> rm(list = ls())
> WD<-"../../../Volumes/My_HD/my_working_directory"
> setwd(WD)
> getwd()
[1] "/Volumes/My_HD/my_working_directory"
So far so good, but then:
> list.files(WD)
character(0)
Whilst I would expect a list of the files that I know are there and that I can see and open with Finder and with the Terminal. Actually the R script itself is inside that directory.
If then I try to open a file I get something like this:
> myfile <- read.delim(paste(WD,"/file_name", sep = ""), header = T)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '../../../Volumes/My_HD/my_working_directory/file_name': No such file or directory
I thought I was doing something wrong specifying the path to the directory, but I tried point and click from RStudio (Session>Set Working Directory and either >To Source File Location or >Choose Directory...) and got the same unhappy result.
I'm using R version 2.15.1 on a MacBookPro (OS X 10.7.05). I'm running R from RStudio version 0.97.551, but the problem is not related to RStudio as it happens also when I try directly on R.
Any help will be very much appreciated. Thanks.
Since I think you're wanting to keep some relativity in your paths, and because you're using RStudio, I'll share one strategy that I often use. If I've correctly divined what you're looking for, this should sidestep the issue entirely.
Start by creating a new project from within RStudio. Once you've opened that project, you'll find that your working directory getwd() will be conveniently set where you have your project file at.
If you're doing something larger, you can get yourself off on the right foot by using the "ProjectTemplate" package. On my system:
# install.packages(c("ProjectTemplate"))
library(ProjectTemplate)
create.project("~/Desktop/MyProject")
This sets up a project skeleton, complete with unit test folder, doc folder, and everything else I might need for a "proper" R project.
Next I create a project from RStudio as I mentioned above. Create it from an existing directory. The project will open, and all of your paths will be relative to the root of your 'MyProject' folder.
I also like to initialize a git repository while I'm at it. From an OS X terminal window:
$ cd ~/Desktop/MyProject; git init
...and then add / commit the bare skeleton
$ git add * ; git commit -m "initial project skeleton"
From your RStudio session you can load things relative to your project root. Let's say you had iris.csv in your data subfolder:
read.csv("./data/iris.csv", header=TRUE)

Resources