Read() function in R is looking at wrong folder - r

When I'm calling any read.___() function in R it is looking at the wrong file path. I have tried changing the working directory multiple times and it keeps going to the same path. For example I'm trying the following:
setwd("C:/OneDrive/")
read.csv("~/Desktop/Data/file.csv")
I then get the following error:
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'C:/OneDrive/Documents/Desktop/Data/file.csv': No such file
or directory
For some reason the read() function adds the documents folder in place of the specified working directory. I tried uninstalling and deleting all files related to R on my computer then reinstalling. I still get the same issue. I've seen others ask a similar question, but everyone keeps telling them to set the working directory to something different. It doesn't matter what I set the working directory to. R keeps adding the Documents folder into the path. How do I get it to stop adding the Documents folder?

Related

R unable to write CSVs post update [duplicate]

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:
I create a simple data vector to export
x <- c(1,3,4,3,5,7,5,8,2,5,7)
I try to export with:
write.csv(x,file='whatever.csv')
And I get an error:
error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open file 'whatever.csv': Permission denied
How can I solve this?
First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.
To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.
I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.
write.csv(data, file="C:/project/file.csv")
If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.
I got this error today and fixed it by granting everyone write permission to the folder.
Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"
I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.
Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:
library(dplyr)
library(hflights)
path_to_hflights_as_csv <-
file.path(path.expand("~/MyHomeSubDir"),
"hflights.csv")
write.csv(hflights, path_to_hflights_as_csv)
path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".
Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):
https://en.wikipedia.org/wiki/Home_directory
https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).
I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'censoredpath.file.csv': Permission denied
I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

Do you need Microsoft Office (Excel) to read .csv files with R?

I just downloaded R onto my computer, but when I try to read a .csv file I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '[filename].csv': No such file or directory
This happens both when I command the program to read the .csv or when I try to import it manually (although that error code is slightly different).
Could this be because I don't have Microsoft Office (and subsequently, Excel) on my computer? The .csv file exists and is not corrupt because I have no problem uploading the file to Google sheets and displaying the data there. Either that, or I didn't install R correctly. Any suggestions?
No, you don't need any special packages to read plain text CSV files in R.
Looking at your error message:
Warning message: In file(file, "rt") : cannot open file '[filename].csv': No such file or directory
I would guess that the problem is your path to the file in question. As a quick fix, you can try using the fully qualified path to your file. For example, on Windows you might try this:
data <- read.csv(file="c:/path/to/filename.csv", header=TRUE, sep=",")
If this works, then the issue was the location of your CSV file. Then, if you want to continue using a relative path, you'll have to figure out what that would be given your current R setup.

importing a CSV file

I just started learning R which is my first programming language. I tried importing a CSV file from my system which is on my downloads in my computer using:
getwd()
setwd("/downloads")
statesInfo <- read.csv('stateData.csv')
But i keep getting the error message below.
setwd("/downloads")
Error in setwd("/downloads") : cannot change working directory
statesInfo <- read.csv('stateData.csv')
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'stateData.csv': No such file or directory.
Could I be getting it wrong.
R is unable to recognize the directory that you're referring to and that's why you get the first error.
The working directory remains unchanged and R can not find the file stateData.csv in the current working directory which results in the second error message.
For setting a working directory use the full path instead: setwd("C:/Users/yourname/Downloads/") - notice the forward slash instead of the back-slash.
However, If you're only importing files, you do not need to change your working directory every time. You can simply refer to files in other locations. If you're using windows - you will need to use ./ for sub-folders and ../ for folders that are one level up. For example. if your working directory is set to 'C:/Users/yourname/Desktop/R' and you want to read a file from the 'Downloads' folder, simply use the below code:
dat <- read.csv("../../Downloads/stateData.csv")
the first ../ takes you one level up to the 'Desktop' and the second ../ takes you to 'Users'. From there you are referring to 'Downloads' folder where the stateData.csv file is located.
EDIT
The above works for Windows isntallations, for Mac/others you would have to use the tilda notation: e.g. ~/Desktop
So, it should be like this.
setwd("c:/mydir")
Also.
MyData <- read.csv(file="c:/mydir/TheDataIWantToReadIn.csv", header=TRUE, sep=",")

Error while reading csv file

I have a xlsx file and to read from Rstudio i saved it as .csv file. Now when i try to read the file from Rstudio, receiving the below error.
setwd("D:/DATA SCIENCE/CCPP-Linear regression")
ccpp<- read.csv("Folds5x2_pp.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'Folds5x2_pp.csv': No such file or directory
As already mentioned in the comments, the "cannot open the connection" part of the error message confirms that where R is looking is not where the file is.
A few things to keep in mind
use getwd() to see the current R working directory
use setwd() to change R's wd
You should use RStudio projects to organize your projects - this helps with the working directory thing
spaces in paths are difficult sometimes. Looks like you have "D:/DATA SCIENCE", that space can cause problems (you may need to escape the space like "DATA\ SCIENCE".
You can read the file using the full path (e.g. read.csv("D:/DATA SCIENCE/path/file.csv")) or if you are doing this a lot, set a variable to your path data_path ="D:/DATA SCIENCE/path/" and then read.csv(file.path(data_path, "file.csv")) where file.path concatenates with your OS specific path delimiter.
You can import files into RStudio using the Import Dataset option under Tools in the menu bar.

Cannot export data to a file in R (write.csv)

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:
I create a simple data vector to export
x <- c(1,3,4,3,5,7,5,8,2,5,7)
I try to export with:
write.csv(x,file='whatever.csv')
And I get an error:
error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open file 'whatever.csv': Permission denied
How can I solve this?
First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.
To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.
I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.
write.csv(data, file="C:/project/file.csv")
If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.
I got this error today and fixed it by granting everyone write permission to the folder.
Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"
I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.
Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:
library(dplyr)
library(hflights)
path_to_hflights_as_csv <-
file.path(path.expand("~/MyHomeSubDir"),
"hflights.csv")
write.csv(hflights, path_to_hflights_as_csv)
path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".
Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):
https://en.wikipedia.org/wiki/Home_directory
https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).
I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'censoredpath.file.csv': Permission denied
I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

Resources