[r] How to open CSV file in R when R says "no such file or directory"?

I have an excel file that I want to open in R. I tried both of these commands after saving the excel file as a csv file or a text file.

read.table() or read.csv()

I think part of the problem is where the file is located. I have it saved on the desk top. What am I missing here?

Here is the R output

In file(file, "rt") :
  cannot open file 'Rtrial.csv': No such file or directory
> help.search("read.csv")
> read.csv("Rtrial.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'Rtrial.csv': No such file or directory
> read.table("tab")

This question is related to r

The answer is


  1. Kindly check whether the file name has an extension for example:

    abc.csv
    

    if so remove the .csv extension.

  2. set wd to the folder containing the file (~)

  3. data<-read.csv("abc.csv")

Your data has been read the data object


I have experienced that this error occurs when you either move the excel file to the destination other than where your r file is located or when you move your r file to the destination other than where your excel file is located.

Good Practice:

  1. Keep your .r and .csv files in the same directory.
  2. open your .r file from getting into its directory instead of opening the r file from rstuio's open file option.

You also have import Dataset option at Environment Block, just click there and get your required packages installed & from next time use this option to read datasets. You will not get this error again. I also appreciate the above provided answers.

enter image description here


MAC OS It happened to me as well. I simply chose from the R toolbar MISC and then chose Change Working Directory. I was able to choose the directory that the .csv file was saved in. When I went back to the command line and typed getwd() the full directory was updated and correct and the read.csv function finally worked.


Sound like you just have an issue with the path. Include the full path, if you use backslashes they need to be escaped: "C:\\folder\\folder\\Desktop\\file.csv" or "C:/folder/folder/Desktop/file.csv".

myfile = read.csv("C:/folder/folder/Desktop/file.csv")  # or read.table()

It may also be wise to avoid spaces and symbols in your file names, though I'm fairly certain spaces are OK.


I just had this problem and I first switched to another directory and then switched back and the problem was fixed.


I had to combine Maiasaura and Svun answers to get it to work: using setwd and escaping all the slashes and spaces.

setwd('C:\\Users\\firstname\ lastname\\Desktop\\folder1\\folder2\\folder3')
data = read.csv("file.csv")
data

This solved the issue for me.


In my case this very problem was raised by wrong spelling, lower case 'c:' instead of upper case 'C:' in the path. I corrected spelling and problem vanished.


Here is one way to do it. It uses the ability of R to construct file paths based on the platform and hence will work on both Mac OS and Windows. Moreover, you don't need to convert your xls file to csv, as there are many R packages that will help you read xls directly (e.g. gdata package).

# get user's home directory
home = setwd(Sys.getenv("HOME"));

# construct path to file
fpath = file.path(home, "Desktop", "RTrial.xls");

# load gdata library to read xls files
library(gdata);

# read xls file
Rtrial = read.xls(fpath);

Let me know if this works.


  1. Save as in excel will keep the file open and lock it so you can't open it. Close the excel file or you won't be able to use it in R.
  2. Give the full path and escape backslashes read.csv("c:\\users\\JoeUser\\Desktop\\JoesData.csv")

this work for me, accesing data from root. use double slash to access address.

dataset = read.csv('C:\\Users\\Desktop\\Machine Learning\\Data.csv')

I had the same problem and when I checked the properties of the file on file explorer, it shows me the next message:

"Security: This file came from another computer and might be blocked to help protect this computer"

You click on the "Unblock" button and... you can access to the file from R without any problem, just using read.csv() function and from the directory specified as your working directory, even if is not the same as the file’s directory you are accessing to.


Try

f <- file.choose()

to choose the file interactively and save the name in f.

Then run read.csv on the saved filename

d <- read.csv(f)

Another way of reading Excel including the new format xlsx could be the package speedR (https://r-forge.r-project.org/projects/speedr/). It is an interactive and visual data importer. Besides importing you can filter(subset) the existing objects from the R workspace.


My issue was very simple, the working directory was not the "Source" directory that was printed when the file ran. To fix this, you can use getwd() and setwd() to get your relative links working, or just use a full path when opening the csv.

print(getwd()) # Where does the code think it is?
setwd("~/Documents") # Where do I want my code to be?
dat = read.csv("~/Documents/Data Visualization/expDataAnalysis/one/ac1_survey.csv") #just make it work!