我在 Windows 7 上运行 rStudio v3.1.2。这台笔记本电脑是 64 位计算机。
我正在参加 Coursera 提供的 JHU R 编程类(class),但遇到了问题第 1 部分中收到的错误。我在这个例子中保留了一些错误处理函数,所以我真的只是想展示我绝对需要的东西。我包含这些消息的唯一原因是为了证明必须满足所有条件才能继续。
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)
}
请注意,WR-eSUB 内部有一个名为 specdata 的文件夹,并且在该文件夹内有一个包含 .csv 文件(也称为 specdata)的目录。我可以改变这一点,但到目前为止我一直在使用它,并且没有遇到任何问题。
当我调用 pollutantmean("specdata", "nitrate", 1:2)
时,我收到以下错误消息:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file 'specdata': Permission denied
现在,在我无数次尝试完成这部分作业时,我已经能够使用 lapply 等其他方式提取数据,但因为我一直陷入困境,所以我扔掉了所有东西并想以这种方式尝试.
我已经在网络上搜索过,试图找到这个解决方案。尽管事实上有几个已回答的问题,但似乎没有一个像这个一样令人困惑。 WR-eSUB 是一个管理文件夹,但之前尝试打开其中的文件时并未产生此错误。
最佳答案
睡个好觉后,我发现了问题。我根本没有使用目录,所以我需要添加它。
thisFileName = paste(directory, "/", formatC(i, width = 3, flag = "0"), ".csv", sep="")
关于r - 文件中的错误(文件, "rt"): cannot open the connection - cannot open file 'specdata' access denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597536/