How to check that given directory exist or not using R?



I am currently studying about how to create directory in R and while studying, I came to know to a function in R

dir.create("directory name")

I want to is there a command in R by which I can check that is a directory is already present or not .


Hi @sid100158,

If you use dir.create on an existing directory, it gives a warning. You can manipulate this behavior into your need (for checking directory existence)


Hi @sid100158,

using just the dir() command would do the trick. It will list all the directories under the current working directory.
Else if you want to create the directory if not present then as @jalFaizy mentioned you can manipulate dir.create() to check if a directory exists and creating it; if not present . I am attaching the code to do so. Please have a look.

dir.create(file.path(mainDir, subDir))

Thanks for asking.


You can use the following command. It returns True or False.