Reputation: 85
I'm new to R and i have question about using file.exist.
I tried:
if(!file.exist("data")){
dir.create("data")
}
But I get the error, could not find function "file.exist".
I then tried:
if (is!TRUE(file.exists("data"))) {
dir.create("data")
}
I still get an error, unexpected '!' in "if (is!". But it creates the folder.
What am I doing wrong?
Upvotes: 6
Views: 7697
Reputation: 1053
While this is potentially a duplicate, I think it's worth a little explanation for you.
if(!file.exists("data")){
dir.create("data")
}
This is the right way to go about it, you've done that well. Your issue is R doesn't know where "data" lives if you've not set the working directory to the location that data would or wouldn't exist. 2 ways to tackle this: 1:
setwd("C:/folder/folder/folder/data_location")
if(!file.exists("data")){
dir.create("data")
}
2:
if(!file.exists("C:/folder/folder/folder/data_location/data")){
dir.create("data")
}
Something else I've noticed is you're looking for a file, then creating a directory. If you're interested in a directory, check out dir.exists
.
Hope this helps!
Upvotes: 3
Reputation: 71
Your are looking for the following:
if(!dir.exists("data")) {
dir.create("data")
}
Here are some Links that might help you on the way:
files2 package for file system interfacing
Upvotes: 7