R code for listing directories/code R pour lister les repertoires
Here
is a pretty R code for listing directories in a
given directory
Voici
un code R pour lister les repertoires d'un dossier donné
list.dirs <-function(path=".",recursive=FALSE) {get.list <- list.files(path)if(length(get.list) ==0) {return(NULL)}#no files or directoriesget.list.fixed<- paste(path,get.list,sep="/")whichonesaredir <- file.info(get.list.fixed)$isdirif(!any(whichonesaredir)) {return(NULL)}#no directories, only filesdirlist <-get.list.fixed[whichonesaredir]finaldirlist <- dirlistif((recursive & length(dirlist) >0)) {for(iin1:length(dirlist)) {finaldirlist <- c(finaldirlist,list.dirs(dirlist[i],recursive))}}return(finaldirlist)}
Save
the code in a file (listDir.r for example) and in R console
use the following commands
source('listDir.r') #to source the filelist=list.dirs(path,rec)#path is the current path by default else the dir in path will be listed.#rec (TRUE or FALSE(defaulty)) indicates if the sub-directories will be listed or not

Commentaires
Enregistrer un commentaire