MegDav
MegDav

Reputation: 31

Why is RStudio telling me that my file doesn't exist? I'm looking directly at it

What I'm seeing

I'm on RStudio, just wanting to read a csv file:

newdata <- read_csv("Nameofdata.csv",
       col_names = TRUE)

But it keeps telling me that the file doesn't exist. It does exist. I'm staring DIRECTLY at it, underneath the file path that RStudio insists is wrong. I can open it in another tab, I can open in in excel, it's EXACTLY where RStudio says it's not existing. Please help.

Upvotes: 3

Views: 9509

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226097

Looking very closely at your screen shot, commenters are suggesting that you have an extra space at the beginning of your file name.

Also, I see that the working directory appears to be correct (the error message lists it as C:/users/mdavi/Desktop/School/Research/Sc, which appears to match what is listed in the Files pane

A couple of strategies for trouble-shooting file-locating problems:

  • use getwd() and list.files() to confirm your working directory and what files R can see from there (you can read this introduction to get more background on working directories):
  • use file.choose() to select a file interactively; it's generally a bad idea to use this in production code, as it will add an interactive step when you usually want to be able to run all of your code start to finish without needing manual steps, but it's quite useful for debugging.

If you wanted R to list files that approximately matched your specified filename, you could use

L <- list.files(pattern="*.csv")
agrep("myfile.csv", L, value=TRUE)

You need to set your working directly correctly, either via setwd("~/Desktop/School/Research/SC") in the R Console (that's what I think I see in your screenshot: ~ stands for your home directory, i.e "Users/Whoever") or via Session > Set Working Directory > To File Pane in the RStudio menus.

You can read a variety of Stack Overflow questions about setting the working directory for more complete context ... web searching on "R 'working directory'" might help too.


Upvotes: 4

Related Questions