[R] Globbing inconsistencies, dir() vs. unlink()

Henrik Bengtsson hb at biostat.ucsf.edu
Thu Nov 18 04:35:29 CET 2010


Hi,

note that to expand tilde, there is also path.expand() which should
give less "surprises" than Sys.glob().

I can confirm that this is not only Windows, but also on Linux;

> dir("~/ttt")
[1] "foo.txt"
> unlink("~/ttt", recursive=TRUE)
> dir("~/ttt")
[1] "foo.txt"
> unlink(path.expand("~/ttt"), recursive=TRUE)
> dir("~/ttt")
character(0)
> file.info("~/ttt")
       size isdir mode mtime ctime atime uid gid uname grname
~/ttt/   NA    NA <NA>  <NA>  <NA>  <NA>  NA  NA  <NA>   <NA>

> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base



Same with R v2.13.0 devel on Windows 7 64-bit:

> sessionInfo()
R version 2.13.0 Under development (unstable) (2010-11-11 r53554)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] R.utils_1.5.6     R.oo_1.7.4        R.methodsS3_1.2.1

loaded via a namespace (and not attached):
[1] tools_2.13.0


My $.02

/Henrik



On Mon, Oct 4, 2010 at 1:58 PM, Ken Williams
<ken.williams at thomsonreuters.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list