1

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
MegDav
  • 11
  • 1
  • 2
  • 2
    Are you using an absolute path or a relative path? If your code snippet is right, make sure the file you want to read is in the current working directory. (See eipi10's comment.) – Charlie Gallagher Jan 15 '21 at 19:22
  • 3
    Run `getwd()` in the console. That's the folder your R session is currently pointing to. Is that the folder where the file is located? – eipi10 Jan 15 '21 at 19:23
  • 3
    Please show your full code, i.e the libraries you are loading. read_sav is not from base R. Also, what does getwd() tell you? Does it give you the folder the file is stored in? – deschen Jan 15 '21 at 19:23
  • Does `list.files()` return the name of the file? – MrFlick Jan 15 '21 at 20:23
  • 2
    Looks like there is an extra space before `.csv`. If the file name has been built from data using `paste` function then it's a common problem (instead use `paste0` or set `sep` to `""` (empty string)) – Billy34 Jan 15 '21 at 20:54
  • @Billy34, how can you see that?? – Ben Bolker Jan 15 '21 at 23:59
  • You do need to be comfortable with the concept of file/directory [path](https://swcarpentry.github.io/shell-novice/02-filedir/index.html) - read at least the first 2 sections. Then the two commands `getwd()` and `setwd()` will help. You can practice the some more problematic use cases in R by reading about executing a script from Rstudio or from command-line [here](https://stackoverflow.com/questions/65425366/here-issue-in-r-scripts). – r0berts Jan 16 '21 at 00:11
  • Looking more closely at the image in the OP, it looks like @Billy34 identified the problem! – eipi10 Jan 16 '21 at 16:37

1 Answers1

4

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.


Ben Bolker
  • 211,554
  • 25
  • 370
  • 453