Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "dir" of a non-existent directory return an error?

Tags:

r

I am demoing a new R environment at my work. The directory structure is a mystery, so I'm walking around in the R shell to discover what files there are. I just noticed that if I type dir("./NonExistentDirectory") the answer is character(0), which is no different from the output of dir("./ExistingButEmptyDirectory"). I would expect the former case to return an error along the lines of Error: directory './NonExistentDirectory' is non-existent. Is there a reason for R giving the output that it does?

like image 624
AdamO Avatar asked Aug 31 '25 16:08

AdamO


1 Answers

This was a design decision. path (the first argument) is vectorized. The documentation says

If a path does not exist or is not a directory or is unreadable it is skipped.

That can be convenient. Note that dir.exists is available for checking existence of a directory.

like image 65
Roland Avatar answered Sep 02 '25 08:09

Roland