簡體   English   中英

文件錯誤(文件,“rt”):無法打開連接 - 無法打開文件'specdata'訪問被拒絕

[英]Error in file(file, “rt”) : cannot open the connection - cannot open file 'specdata' access denied

我在Windows 7上運行rStudio v3.1.2。這台筆記本電腦是一台64位計算機。

我正在參加Coursera提供的JHU R編程課程,並且遇到了我在第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是一個管理文件夾,但之前嘗試打開其中的文件之前沒有產生此錯誤。

這一行將失敗:

read.csv(directory, thisFileName)

因為,作為對?read.csv方向的粗略一瞥會告訴你,該函數的第一個參數是:

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’).

並且您將directory傳遞給它(如您顯示的調用中的specdata )。

鑒於setwd()已經將你放在這個目錄中,不會

read.csv(theFileName)

工作?

睡個好覺后,我看到了問題。 我根本沒有使用目錄,所以我需要添加它。

thisFileName = paste(directory, "/", formatC(i, width = 3, flag = "0"), ".csv", sep="")

我在2018年從Coursea學習R編程。我知道這個問題已經在3年前發布了但我還是希望發帖,如果有人想知道的話。

我也面臨同樣的問題,但在閱讀此鏈接后

我開始知道我們需要指定文件夾的位置以及文件夾中的文件。 所以我把代碼放了:

folder<- "C:\\Users\\PHD\\Documents\\specdata"
file_list <- list.files(path=folder, pattern="*.csv")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM