[R] Globbing inconsistencies, dir() vs. unlink()
Ken Williams
ken.williams at thomsonreuters.com
Mon Oct 4 22:58:18 CEST 2010
I was trying to remove a directory and couldn't figure out why it was
failing:
> dir("~/p4/r-packages/IREval/Users", recursive=T)
[1] "u0048513/p4/r-packages/IREval/DESCRIPTION"
[2] "u0048513/p4/r-packages/IREval/R/IREval.R"
[3] "u0048513/p4/r-packages/IREval/Read-and-delete-me"
[4] "u0048513/p4/r-packages/IREval/tests/general.R"
> (unlink("~/p4/r-packages/IREval/Users", recursive=TRUE))
[1] 0
> dir("~/p4/r-packages/IREval/Users", recursive=T)
[1] "u0048513/p4/r-packages/IREval/DESCRIPTION"
[2] "u0048513/p4/r-packages/IREval/R/IREval.R"
[3] "u0048513/p4/r-packages/IREval/Read-and-delete-me"
[4] "u0048513/p4/r-packages/IREval/tests/general.R"
It turns out that the '~' is recognized (like a shell globbing operator) in
dir(), but not in unlink(). The unlink() docs say it recognizes "wildcards"
but doesn't get too specific (because of cross-platform concerns, probably).
The confusion is compounded by unlink() returning 0, its value for
successful deletion. In this case it doesn't really mean it, because the
docs say "not deleting a non-existent file is not a failure".
I can delete successfully by expanding the tilde myself:
> dir("~/p4/r-packages/IREval/Users", recursive=T)
[1] "u0048513/p4/r-packages/IREval/DESCRIPTION"
[2] "u0048513/p4/r-packages/IREval/R/IREval.R"
[3] "u0048513/p4/r-packages/IREval/Read-and-delete-me"
[4] "u0048513/p4/r-packages/IREval/tests/general.R"
> (unlink(Sys.glob("~/p4/r-packages/IREval/Users"), recursive=TRUE))
[1] 0
> dir("~/p4/r-packages/IREval/Users", recursive=T)
character(0)
Perhaps a future version of unlink() could use the Sys.glob() mechanism
directly to avoid this inconsistency.
--
Ken Williams
Sr. Research Scientist
Thomson Reuters
Phone: 651-848-7712
ken.williams at thomsonreuters.com
More information about the R-help
mailing list