This question already has answers here:
How to open CSV file in R when R says "no such file or directory"?
(16 answers)
Closed 2 years ago.
I'm new to R, and after researching this error extensively, I'm still not able to find a solution for it. Here's the code. I've checked my working directory, and made sure the files are in the right directory. Appreciate it. Thanks
pollutantmean <- function(directory, pollutant = "nitrate", id= 1:332)
{ if(grep("specdata",directory) ==1)
{
directory <- ("./specdata")
}
mean_polldata <- c()
specdatafiles <- as.character(list.files(directory))
specdatapaths <- paste(directory, specdatafiles, sep="")
for(i in id)
{
curr_file <- read.csv(specdatapaths[i], header=T, sep=",")
head(curr_file)
pollutant
remove_na <- curr_file[!is.na(curr_file[, pollutant]), pollutant]
mean_polldata <- c(mean_polldata, remove_na)
}
{
mean_results <- mean(mean_polldata)
return(round(mean_results, 3))
}
}
The error I'm getting is below:
Error in file(file, "rt") : cannot open the connection
file(file, "rt")
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)
read.csv(specdatapaths[i], header = T, sep = ",")
pollutantmean3("specdata", "sulfate", 1:10)
In addition: Warning message:
In file(file, "rt") :
cannot open file './specdata001.csv': No such file or directory
You need to change directory <- ("./specdata") to directory <- ("./specdata/")
Relative to your current working directory, you are looking for the file 001.csv, which is in your specdata directory.
This question is nearly impossible to answer without any context, since you have not provided us with the structure of your working directory here. Fortunately for you, I have already taken R Programming on Coursera, so I already did this homework question.
The reason why you see this error I guess is because RStudio lost the path of your working directory.
(1) Go to session...
(2) Set working directory...
(3) Choose directory...
--> Then you can see a window pops up.
--> Choose the folder where you store your data.
This is the way without any code that you change your working directory.
Hope this can help you.
Set your working directory one level/folder higher. For example, if it is already set as:
setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera/specdata")
go up one level back and set it as:
setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera")
In other words, do not make "specdata" folder as your working directory.
I just spent a lot of time trying to understand what was wrong on my code too...
And it seems to be simple if you are using windows.
When you name your file "blabla.txt" then windows name it "blabla.txt.txt"...
That's the same with .CSV files so windows create a file named "001.csv.csv" if you called it "001.csv"
So, when you create your .csv file, just rename it "001" and open it in R using read.table("/absolute/path/of/directory/with/required/001.csv")
It works for me.
close your R studio and run it again as an administrator. That did the magic for me. Hope it works for you and anyone going through this too.
One better check that can be done if you get such error while accessing a file is to use the file.exists("file_path/file_name") function. This function will return TRUE if the file is existing and accessible, else False.
If running on Windows try running R or R Studio as administrator to avoid Windows OS file system constraints.
Use setwd() to change to appropriate directory.
Use only filename to access any file in the working directory.
Navigate a folder above by using "../<filename>".
Got this error and found that RStudio on my Windows machine try to use \ as escape symbol, so had to replace it with \\ to deal with it.
Try file.exists function with your path, e.g.:
file.exists("D:\\R\\path_to_file.csv")
Error in file(file, "rt") :
I just faced the same error and resolved by removing spacing in address using paste0 instead of paste
filepath=paste0(directory,"/",filename[1],sep="")
I got this same error message and fixed it in the easiest way I could. I put my .csv file into a file folder on my desktop, opened desktop in the window next to console on RStudio, and then opened my file there, and checked the box next to my .csv file, then I used the "more" pull down menu at the top of this window to set this as my working directory...probably the easiest thing for SUPER beginners like me :)
This error also occurs when you try to use the result of getwd() directly in the path. The problem is the missingness of the "/" at the end. Try the following code:
projectFolder <- paste(getwd(), "/", sep = '')
The paste() is to concatenate the forward slash at the end.
I got my R code file from a friend and was not able to run read.csv command but If I copy the same command(read.csv ) to a new R script file, it ran fine.
Below command was not running in R code file shared by my friend, working directory,file name etc were all correct because If I created a new R script file and ran below command ,it worked.
df <- read.csv("file.csv",header=TRUE,stringsAsFactors = FALSE,strip.white =
TRUE,sep = ',')
issue/resolution:
I right clicked the R code file and unblocked the file and click save button and issue got resolved.
I your R code file is in Downloads folder in windows , then move to some other folder.
I had a same issue .I removed the extension from the file name.Example my file name was saved as xyz. csv. i saved it as xyz.
Error in file(file, "rt")
Created a .r file and saved it in Desktop together with a sample_10000.csv file.
Once trying to read it
heisenberg <- read.csv(file="sample_100000.csv")
was getting the same error as you
heisenberg <- read.csv(file="sample_10000")
Error in file(file, "rt") : cannot open the connection In addition: Warning message:
In file(file, "rt") : cannot open file 'sample_10000': No such file or
directory
I knew at least two ways to fix this, one using the absolute path and the other changing the working directory.
Absolute path
I fixed it adding the absolute path to the file, more precisely
heisenberg <- read.csv(file="C:/Users/tiago/Desktop/sample_100000.csv")
Working directory
This error shows up because RStudio has a specific working directory defined which isn't necessarily the place the .r file is at.
So, to fix using this approach I've gone to Session > Set Working Directory > Chose Directory (CTRL + Shift + H) and selected Desktop, where the .csv file was at. That way running the following command also worked
heisenberg <- read.csv(file="sample_100000.csv")
I was getting the same error when trying to import 10,000 files. I tried opening a single file with Excel and Excel gave me an error message: "the file path is too long".
The file was buried in 6 nested folders, which is a problem of itself. Moving all the files to a desktop folder solved the issue without having to change any code.
I came across a solution based on a few answers popped in the thread.
(windows 10)
setwd("D:/path to folder where the files are")
directory <- getwd()
myfile<- "a_file_in_the_folder.txt"
filepath=paste0(directory,"/",myfile[1],sep="")
table <- read.table(table, sep = "\t", header=T, row.names = 1, dec=",")
Related
# Removing all Objects from current environment.
rm(list = ls())
# Setting directory
setwd("C:/Users/Ashwin/Desktop")
# Task 2
RG <- read.csv("Assignment3")
In file(file, "rt") :
cannot open file 'Assignment3': No such file or directory
When reading the file you need to provide the correct file name including the extension, the file name is character size sensitive.
You are getting the error because the name is not matching the file in the directory.
Solutions:
Add an extension eg. .csv, .DAT, .txt
Make sure the file is in the path "C:/Users/Ashwin/Desktop"
MAke sure the file name is correct and the characters are the same size. eg. if file in the drive is "assignment3 you need to write in R "assignment3 as well.
You can also use:
RG <- read.csv(file.choose())
This way a window will pop up to select your file in the file from the folder.
I run into issues reading in csv files with dynamic names and avoiding hard coding the file path. I'd like short tidy code (non-hardcoded). If I hardcode the full path (everything before the "~") it reads in the files fine. But soft-coding (if that is the opposite of hard coding) the file path it gives the error (despite showing the correct path in the error. I have two variable parts of the file name that I paste into the file name before reading it in. If I avoid paste and just type a path per individual it also works.
#dynamic part I usually have in a loop with all the options.
part_a <- "outside" #other options here in my loop include "inside"
part_b <- "late" # other option "early" or "preterm"
#reading in the df
df <-read.csv(paste0("~/Data/FromR/clean_",part_a,part_b,"_2016.csv"),
check.names=FALSE, na.strings="null")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:/Users/myname/Documents/Data/FromR/clean_outsidelate_2016.csv': No such file or directory
if I use getwd() in the first part of the paste in place of ~ as suggested here it works by producing this string "C:/Users/myname/Documents/MyR_Projects/Specific_R_project/" at the beginning of the paste. But how can I get it to work with the "~"? when using the ~ it stops at the "Documents" folder...
The desired outcome is to read in the file without error perform functions and repeat with other files. My loop works fine hardcoded, and I only wanted to make it more general or softcoded.
I just tried to read a file (testFile.txt) in my home from a different wdand it works fine with ~
myFile <- "testFile
mymy <- ".txt"
ciao <- read.delim(paste0("~/",myFile,mymy))
In powershell you can use %~% (have a look here tread), but I am not sure how to expand the $HOME in R.
#-------- edit
Have a look here and here. Basically any variable defined in your .Renviron should be accessible.
A basic question. I have a bunch of transcripts (.docx files) I want to read into a corpus. I use readtext() to read in single files no problem.
dat <- readtext("~/ownCloud/NLP/interview_1.docx")
As soon as I put "*.docx" in my readtext statement it spits an error.
dat <- readtext("~/ownCloud/NLP/*.docx")
Error: '/var/folders/bl/61g7ngh55vs79cfhfhnstd4c0000gn/T//RtmpWD6KSx/readtext-aa71916b691c0cf3cabc73a2e04a45f7/word/document.xml' does not exist.
In addition: Warning message:
In utils::unzip(file, exdir = path) : error 1 in extracting from zip file
Why the reference to a zip file? I have only .docx files in the directory.
I was able to reproduce the same problem. The issue was there are some hidden/temp .docx files in that folder, if you delete them and then try the code it works.
To see the hidden files, go to the folder from where you are reading docx files and based on your OS select a way to show them. On my mac I used
CMD + SHIFT + .
Once you delete them, try the code again and it should work
library(readtext)
dat <- readtext("~/ownCloud/NLP/*.docx")
I am trying to open a Stata .dta file which is compressed into winrar in R. Here are my codes:
library(foreign)
setwd("C:/Users/ASUS/Desktop/Data on oil/Oil discovery")
data <- read.dta("oil_discovery")
and I get :
Error in read.dta("oil_discovery") : unable to open file: 'No such file or directory'
I think that my problem is coming from the assignment of my working directory but I don't know how to manage it.
You need to specify the full file name to read.dta. This includes the file ending. That is, instead of
data <- read.dta("oil_discovery")
you need to write
data <- read.dta("oil_discovery.dta")
If there is an additional problem with the compression, I would imagine that the error message will be different. However, Error in read.dta("oil_discovery") : unable to open file: 'No such file or directory' very explicitly points out that the current error is that the file oil_discovery is not found.
A good way to check if the name or path is causing the error is to use choose.files(). That is, run the following line:
data <- read.dta(choose.files())
This will open a pop-up window where you can manually select the file. If this works, then the name of the file was misspecified.
library(haven)
data <- read_dta("**.dta")
View(data)
I am trying to read a csv file that is contained in a file I extracted from the web. The problem is the zipped file has multiple cascading folders. I have to do that for several different units, so I am performing a loop. There is no problem with the loop, the file name is correct and I get to download the file. However I get an error message (and I think is because R cannot find the exact file I am asking it to find). The error is:
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot locate file 'XXXX.csv' in zip file 'c:\yyy\temp\bla\'
download.file(paste("http://web.com_",units[i],"_",places[j],".zip",
sep=""),
temp,
cacheOK = F )
data <- read.csv2(unz(temp,
paste("name_",units[i],"_",places[j],".csv",
sep="")),
header=F,
skip=1)
unlink(temp)
fili<-rbind(X,
data)
}
How do I make R find the file I want?
You have the right approach but (as the warning tells you) the wrong filename.
It's worth double checking that the zip file does exist before you start trying to read its contents.
if(file.exists(temp))
{
read.csv2(unz(...))
} else
{
stop("ZIP file has not been downloaded to the place you expected.")
}
It's also a good idea to a browse around inside the downloaded file (you may wish to unzip it first) to make sure that you are looking in the right place for the CSV contents.
It looks like the file, you're going to read, is located in directory. In this case your reading should be changed as follows:
data <- read.csv2(unz(temp,
paste("**dirname**/name_",units[i],"_",places[j],".csv",
sep="")),
header=F,
skip=1)