Error in file(file, “rt”) : cannot open the connection

三世轮回 提交于 2019-11-27 20:24:57

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.

Abi

Set your working directory one level/folder higher. For example, if it is already set as:

setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera/specdata")

go up one level back and set it as:

setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera")

In other words, do not make "specdata" folder as your working directory.

I just spent a lot of time trying to understand what was wrong on my code too...

And it seems to be simple if you are using windows.

When you name your file "blabla.txt" then windows name it "blabla.txt.txt"... That's the same with .CSV files so windows create a file named "001.csv.csv" if you called it "001.csv"

So, when you create your .csv file, just rename it "001" and open it in R using read.table("/absolute/path/of/directory/with/required/001.csv")

It works for me.

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.

If running on Windows try running R or R Studio as administrator to avoid Windows OS file system constraints.

Use setwd() to change to appropriate directory. Use only filename to access any file in the working directory. Navigate a folder above by using "../<filename>".

close your R studio and run it again as an administrator. That did the magic for me. Hope it works for you and anyone going through this too.

One better check that can be done if you get such error while accessing a file is to use the file.exists("file_path/file_name") function. This function will return TRUE if the file is existing and accessible, else False.

I got this same error message and fixed it in the easiest way I could. I put my .csv file into a file folder on my desktop, opened desktop in the window next to console on RStudio, and then opened my file there, and checked the box next to my .csv file, then I used the "more" pull down menu at the top of this window to set this as my working directory...probably the easiest thing for SUPER beginners like me :)

This error also occurs when you try to use the result of getwd() directly in the path. The problem is the missingness of the "/" at the end. Try the following code:

projectFolder <- paste(getwd(), "/", sep = '')

The paste() is to concatenate the forward slash at the end.

I got my R code file from a friend and was not able to run read.csv command but If I copy the same command(read.csv ) to a new R script file, it ran fine.

Below command was not running in R code file shared by my friend, working directory,file name etc were all correct because If I created a new R script file and ran below command ,it worked.

           df <- read.csv("file.csv",header=TRUE,stringsAsFactors = FALSE,strip.white = 
           TRUE,sep = ',')

issue/resolution: I right clicked the R code file and unblocked the file and click save button and issue got resolved. I your R code file is in Downloads folder in windows , then move to some other folder.

I had a same issue .I removed the extension from the file name.Example my file name was saved as xyz. csv. i saved it as xyz.

Got this error and found that RStudio on my Windows machine try to use \ as escape symbol, so had to replace it with \\ to deal with it.

Try file.exists function with your path, e.g.:

file.exists("D:\\R\\path_to_file.csv")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!