[Rd] modifs on ls

Romain Francois rfrancois at mango-solutions.com
Tue Sep 26 18:56:54 CEST 2006


Hi all,

It would be great to be able to use a syntax like :

R> ls("grid:::grid\.")

instead of :

R> ls("package:grid", pattern = "^grid\.")

Here is a modified version of `ls` that does the job. [ The only modifs 
are just after the if(!missing(name))  ]
Does that make sense ?

Cheers,

Romain


ls <- objects <-
    function (name, pos = -1, envir = as.environment(pos), all.names = 
FALSE,
              pattern)
{


    if (!missing(name)) {
       
        if(length(grep(":::", name))){
           spl <- strsplit(name, ":::")[[1]] 
           name <- paste("package:", spl[1] , sep="")
           pattern  <- paste("^", spl[2], sep="")
        }
       
       
        nameValue <- try(name)
        if(identical(class(nameValue), "try-error")) {
            name <- substitute(name)
            if (!is.character(name))
                name <- deparse(name)
            pos <- name
        }
        else
            pos <- nameValue
    }
    all.names <- .Internal(ls(envir, all.names))
   
  
   
    if (!missing(pattern)) {
       
        if ((ll <- length(grep("[", pattern, fixed=TRUE))) > 0 &&
            ll != length(grep("]", pattern, fixed=TRUE))) {
            if (pattern == "[") {
                pattern <- "\\["
                warning("replaced regular expression pattern '[' by  
'\\\\['")
            }
            else if (length(grep("[^\\\\]\\[<-", pattern) > 0)) {
                pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
                warning("replaced '[<-' by '\\\\[<-' in regular 
expression pattern")
            }
        }
        grep(pattern, all.names, value = TRUE)
    }
    else all.names
}


-- 
*mangosolutions*
/data analysis that delivers/

Tel   +44 1249 467 467
Fax   +44 1249 467 468




More information about the R-devel mailing list