Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in file(file, "rt") : cannot open the connection - cannot open file 'specdata' access denied

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.

like image 360
ASwiftPeregrine Avatar asked Sep 29 '22 17:09

ASwiftPeregrine


1 Answers

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="")
like image 96
ASwiftPeregrine Avatar answered Oct 06 '22 20:10

ASwiftPeregrine