Skip to content

list.files(recursive=T) does not return directory names

2 messages · Bill Dunlap, Henrik Bengtsson

#
list.files() (and dir()) don't appear to return names of
directories when one uses the recursive=T argument.  E.g.,
  > dir(file.path(R.home(),"library"), pattern="^R$", recursive=T)
  [1] "Malmig/help/R"
but the unix find commmand finds lots of R directories
  > z <- system(paste("find", file.path(R.home(),"library"), "-name R"), intern=T)
  > length(z)
  [1] 665
  > file.info(z[1:3])[,1:3]
                                            size isdir mode
  /dept/devel/sw/R/R.linux/R/library/aCGH/R 4096  TRUE 2755
  /dept/devel/sw/R/R.linux/R/library/RBGL/R 4096  TRUE 2755
  /dept/devel/sw/R/R.linux/R/library/XML/R  4096  TRUE 2755

The help file is silent on this behavior.  I am writing
an emulation of these for functions for Splus and was
wondering about 3 things.

a) Is this behavior intended?

b) Is there an easy way to get the names of all directories
under a given one?

b) I would like to add an argument to list.files() to specify
that I'd like the names of only non-directories, only directories,
or both.  I've tentatively called this argument "type" (following
the unix find command) and the acceptable values are "files",
"directories", and "all" (or any abbreviation).  Symbolic links,
fifos, etc. might be nice, but I don't want to fill the code
with unixisms or tempt folks to use them.  Would adding
	type = "files","directories","all"
to list.files and dir conflict with any plans for R's list.files
or dir?

----------------------------------------------------------------------------
Bill Dunlap
Insightful Corporation
bill at insightful dot com
360-428-8146
 "Formerly known as MathSoft, Insightful Corporation provides analytical
 solutions leveraging S-PLUS, StatServer and consulting services."

 "All statements in this message represent the opinions of the author and do
 not necessarily reflect Insightful Corporation policy or position."
#
Hi,

the R.utils package has a function listDirectory() that returns the 
directory names too.  (I've made some changes to the function recently, 
which is not in the CRAN version, so get it from http://www.braju.com/R/ 
instead.)

The package also has isFile() and isDirectory() to test if a pathname 
refers to an existing file and directory, respectively.  These are not 
"vectorized" (yet), so you have to call them with sapply() if you have 
many pathnames, e.g.

 > path <- file.path(R.home(), "share")
 > ld <- listDirectory(path, recursive=TRUE, fullNames=TRUE)
 > ld[sapply(ld, isDirectory)]
  [1] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses"
  [2] "C:\\PROGRA~1\\R\\rw2011pat/share/locale"
  [3] "C:\\PROGRA~1\\R\\rw2011pat/share/make"
...
[27] "C:\\PROGRA~1\\R\\rw2011pat/share/perl/R"
[28] "C:\\PROGRA~1\\R\\rw2011pat/share/perl/Text"
 > ld[sapply(ld, isFile)]
  [1] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/Artistic"
  [2] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/BSD"
  [3] "C:\\PROGRA~1\\R\\rw2011pat/share/licenses/GPL-2"
...
[50] "C:\\PROGRA~1\\R\\rw2011pat/share/texmf/ts1aett.fd"
[51] "C:\\PROGRA~1\\R\\rw2011pat/share/texmf/upquote.sty"

Hope this helps.

BTW, this package also have functions to read Windows Shortcuts files 
(*.lnk) and the function filePath("data", "raw", expandLinks="any") will 
recognize if any part is a shortcut to another directory, e.g. data.lnk 
links to another directory containing subdirectory "raw" (and directory 
data/ does not exist).  (filePath() also not vectorized). 
listDirectory() does not follow Windows Shortcuts.

Henrik
bill at insightful.com wrote: