1

I can't add any file path such as ../folder/smaller-folder/stuff.RData or ./folder/smaller folder/stuff.RData or folder/smaller folder/stuff.RDS. Any version of the file path returns the same error

Warning in gzfile(file, mode) :
  cannot open compressed file 'folder/smaller-folder/stuff.RData', probable reason 'No such file or directory'
Error in gzfile(file, mode) : cannot open the connection

A sample code from https://rstudio-education.github.io/hopr/dataio.html

saveRDS(a, file = "stuff.RDS") 
a <- readRDS("stuff.RDS")

What did I miss? Any leads is much appreciated.

hnguyen
  • 772
  • 6
  • 17
  • 1
    See here: https://stackoverflow.com/questions/15363076/error-in-gzfilefile-wb-cannot-open-the-connection-or-compressed-file. Looks like an issue on Windows. – algae Feb 22 '22 at 21:52
  • What is your current working directory? `getwd()` Are you using Windows? Is your data in a OneDrive directory? – MrFlick Feb 22 '22 at 22:03
  • I'm using a Mac, and yes, I'm trying to save the `.RData` file in another folder. Both the working folder and the target folder are not password-protected. – hnguyen Feb 22 '22 at 22:06
  • 1
    We are asking about working folder because your code is from your working directory is found using `getwd()`. Note that this may not be the same folder that the script is in! The error appears to say that the location `folders/smaller-folder/` does not exist inside of your current working directory. – Adam Sampson Feb 22 '22 at 22:32
  • Thank you for explaining. I'm pretty sure that the `write.csv()` function can write a csv file anywhere as long as the path is correct, even outside of the working directory. That's the behavior I would expect from `saveRDS()`. – hnguyen Feb 22 '22 at 23:01
  • 2
    any chance you're having some kind of case-sensitivity problem? Can we see the results of (1) `getwd()` and (2) the results of `list.files("folder")`, `list.files("folder/smaller folder")`, etc. ? – Ben Bolker Feb 22 '22 at 23:44
  • Do those folders already exist? `readRDS` will not create new directories. Use `dir.exists` to make sure you can actually see the directory. – MrFlick Feb 23 '22 at 07:24

1 Answers1

0

Turned out I need ~ in saveRDS(a, file = "~/folder/smaller-folder/stuff.RDS"). Folder names have both upper and lower cases. Thank you all so much for all the hints.

hnguyen
  • 772
  • 6
  • 17