Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in file(file, "rt") : cannot open the connection [duplicate]

Tags:

r

csv

I'm new to R, and after researching this error extensively, I'm still not able to find a solution for it. Here's the code. I've checked my working directory, and made sure the files are in the right directory. Appreciate it. Thanks

pollutantmean <- function(directory, pollutant = "nitrate", id= 1:332)             {                 if(grep("specdata",directory) ==1)              {                     directory <- ("./specdata")             }             mean_polldata <- c()             specdatafiles <- as.character(list.files(directory))             specdatapaths <- paste(directory, specdatafiles, sep="")                             for(i in id)                      {                     curr_file <- read.csv(specdatapaths[i], header=T, sep=",")                     head(curr_file)                     pollutant                     remove_na <- curr_file[!is.na(curr_file[, pollutant]), pollutant]                     mean_polldata <- c(mean_polldata, remove_na)                     }             {                     mean_results <- mean(mean_polldata)                     return(round(mean_results, 3))             } }  

The error I'm getting is below:

Error in file(file, "rt") : cannot open the connection  file(file, "rt")  read.table(file = file, header = header, sep = sep, quote = quote,      dec = dec, fill = fill, comment.char = comment.char, ...)  read.csv(specdatapaths[i], header = T, sep = ",")  pollutantmean3("specdata", "sulfate", 1:10)  In addition: Warning message: In file(file, "rt") :   cannot open file './specdata001.csv': No such file or directory 
like image 943
ldeassis Avatar asked Dec 14 '14 04:12

ldeassis


People also ask

How do you fix error in file file RT Cannot open the connection in R?

To fix it for the current session, use the setwd() command. Alternatively, you can specify the exact file name directly in your R code once you have set the working directory properly. It is possible you are having issues with relative R file references to directories above and below the current working directory.

What does it mean in R error in file file RT Cannot open the connection?

However, as you can see the RStudio console returned the error message “Error in file(file, “rt”) : cannot open the connection”. The reason for this is that we didn't properly specify the working directory in which the csv file is stored.

How do you reset the working directory in R?

You can change the default working directory from RStudio menu under: Tools –> Global options –> click on “Browse” to select the default working directory you want.

How do I open a file in R?

Click on the Open an existing file icon in the RStudio toolbar. A Choose file dialog will open. Select the R script you want to open [this is one place where the . R file extension comes in handy] and click the Open button.


2 Answers

You need to change directory <- ("./specdata") to directory <- ("./specdata/")

Relative to your current working directory, you are looking for the file 001.csv, which is in your specdata directory.

This question is nearly impossible to answer without any context, since you have not provided us with the structure of your working directory here. Fortunately for you, I have already taken R Programming on Coursera, so I already did this homework question.

like image 196
capt-calculator Avatar answered Oct 31 '22 05:10

capt-calculator


The reason why you see this error I guess is because RStudio lost the path of your working directory.

(1) Go to session...

(2) Set working directory...

(3) Choose directory...

--> Then you can see a window pops up.

--> Choose the folder where you store your data.

This is the way without any code that you change your working directory. Hope this can help you.

enter image description here

like image 32
BetterTeng Avatar answered Oct 31 '22 04:10

BetterTeng