Tried to save my R markdown document as a file in a folder. File to save as and then put it in my lab folder. Titled the r document R-Lab6/7.rmd and it does not let me save the file. The error is "Error file not found".
-
1Does the folder itself exist? You would need to create the folder first if not. How exactly are you saving the file? – MrFlick Oct 25 '20 at 23:14
-
`getwd()`, `list.files()` – Ben Bolker Oct 25 '20 at 23:36
-
2Those also may not be the best file names, I try to only you underscores, no `/` or `-`, but that’s just me – Daniel_j_iii Oct 25 '20 at 23:38
-
oops, I didn't see that you said "in a folder!" revising my answer again ... – Ben Bolker Oct 26 '20 at 01:24
1 Answers
The folder/directory must already exist before you can save a file into it. You can either create the folder outside of R (in your operating system's file browser, or in RStudio's "files" panel), or you can use dir.create("R-Lab6")
from the R console.
Here's an illustration of the problem, using command-line functions:
save("x", file="a/b.rda")
Error in gzfile(file, "wb") : cannot open the connection In addition: Warning message: In gzfile(file, "wb") : cannot open compressed file 'a/b.rda', probable reason 'No such file or directory'
Now create the directory a
:
dir.create("a")
and saving works
save("x", file="a/b.rda")
So it has now saved a file called b.rda
in directory a
(not a file called a/b.rda
).
I originally thought/misunderstood that you wanted to include a /
in your file name.
While modern tools allow you to use almost any characters you like in a file name, slashes are almost never allowed, because they are used by the operating system to denote that a file is contained in a directory (in Windows and on Linux and almost always on MacOS).
Once you start dealing with programming, it's safest not only to avoid slashes (which are impossible) but also other "special" characters: use only numbers, letters, and simple punctuation (dash -
, underscore _
, and dot .
) in your file names ...

- 211,554
- 25
- 370
- 453