问题
I'm running rStudio v3.1.2 on Windows 7. This laptop is a 64-bit machine.
I'm taking the JHU R Programming course offered by Coursera and am stuck on an error I'm receiving in part 1 of the problem. I have some error handling functions I'm keeping out of this example so I'm really just trying to show what I absolutely need to. The only reason I include the messages is the demonstrate that all the conditions must be satisfied in order to proceed.
pollutantmean <- function(directory, pollutant, id=1:332) {
setwd("C:\\Users\\WR-eSUB\\specdata")
if(!isValidDirectory(directory)) {
stop("Invalid input given. Please specify valid directory to operate on.")
}
if(!isValidPollutant(pollutant)) {
stop("Invalid input given. Please specify valid pollutant (nitrate/sulfate).")
}
if(!isValidIdRange(id)) {
stop("Invalid input given. Please specify valid id range (1:332).")
}
sortedData = numeric()
for (i in id) {
thisFileName = paste(formatC(i, width = 3, flag = "0"), ".csv", sep="")
thisFileRead = read.csv(directory, thisFileName)
sortedData = c(sortedData, thisFileRead[[pollutant]])
}
mean(sortedData, na.rm = TRUE)
}
Note that inside WR-eSUB is a folder called specdata and inside that folder there is the directory that contains the .csv files also called specdata. I could change this but so far I've been working with it and I have not stumbled into any problems.
When I call pollutantmean("specdata", "nitrate", 1:2)
I get the following error message:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file 'specdata': Permission denied
Now in my numerous attempts to try and finish this part of the assignment I've been able to extract the data in other ways using things like lapply but because I kept getting stuck I threw everything out and wanted to try things this way.
I've searched the web to try and find this solution. Despite the fact there are several answered inquiries none of them seem to be quite as confusing as this one is. WR-eSUB is an administrative folder but previous attempts to open files within it haven't produced this error before.
回答1:
This line will fail:
read.csv(directory, thisFileName)
because, as a cursory glance in the direction of ?read.csv
would have told you, the first argument to that function is:
file: the name of the file which the data are to be read from.
Each row of the table appears as one line of the file. If it
does not contain an _absolute_ path, the file name is
_relative_ to the current working directory, ‘getwd()’.
Tilde-expansion is performed where supported. This can be a
compressed file (see ‘file’).
and you are passing it the directory
(as in specdata
as per the call you showed).
Given that setwd()
has already put you in this directory, wouldn't
read.csv(theFileName)
work?
回答2:
After getting a good nights sleep, I saw the problem. I wasn't using directory at all, so I needed to add it.
thisFileName = paste(directory, "/", formatC(i, width = 3, flag = "0"), ".csv", sep="")
回答3:
I am learning R programming from Coursea in 2018. I know the question has been posted 3 years ago but I still prefer to post if someone want to know.
I also facing the same problem but after reading this link.
I came to know that we need to specify the location of the folder as well as the file in the folder. So I have put the code:
folder<- "C:\\Users\\PHD\\Documents\\specdata"
file_list <- list.files(path=folder, pattern="*.csv")
来源:https://stackoverflow.com/questions/28597536/error-in-filefile-rt-cannot-open-the-connection-cannot-open-file-specd