本文介绍了文件错误(文件,“rt"):无法打开连接 - 无法打开文件“specdata"访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 7 上运行 rStudio v3.1.2.这台笔记本电脑是 64 位机器.

I'm running rStudio v3.1.2 on Windows 7. This laptop is a 64-bit machine.

我正在学习 Coursera 提供的 JHU R 编程课程,但我在问题的第 1 部分中遇到了一个错误.我有一些错误处理函数,我没有考虑到这个例子,所以我真的只是想展示我绝对需要的东西.我包含这些消息的唯一原因是证明必须满足所有条件才能继续.

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)
}

请注意,WR-eSUB 内有一个名为 specdata 的文件夹,那个 文件夹内有一个包含 .csv 文件的目录,也称为 specdata.我可以改变这一点,但到目前为止我一直在使用它,我没有遇到任何问题.

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.

当我调用 pollutantmean("specdata", "nitrate", 1:2) 时,我收到以下错误消息:

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

现在,在我尝试完成这部分作业的无数次尝试中,我已经能够使用 lapply 之类的东西以其他方式提取数据,但是因为我一直被卡住,所以我把所有东西都扔掉了,想以这种方式尝试.

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.

我在网上搜索过,试图找到这个解决方案.尽管事实上有几个问题得到了答复,但似乎没有一个像这个问题一样令人困惑.WR-eSUB 是一个管理文件夹,但之前尝试打开其中的文件并没有出现此错误.

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.

推荐答案

睡个好觉后,我看到了问题.我根本没有使用目录,所以我需要添加它.

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="")

这篇关于文件错误(文件,“rt"):无法打开连接 - 无法打开文件“specdata"访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:52