From Kurt.Hornik@ci.tuwien.ac.at  Fri May 23 06:33:03 1997
From: Kurt.Hornik@ci.tuwien.ac.at (Kurt Hornik)
Date: Fri, 23 May 1997 07:33:03 +0200
Subject: R-alpha: apropos() available
Message-ID: <199705230533.HAA01011@aragorn.ci.tuwien.ac.at>

Martin and I have `written' a function apropos() for finding all objects
with names matching pattern.  I attach code and documentation.  Perhaps
one could include it in the distribution proper.

We are not sure about returning the position in the search list along
with the matching names found.  Martin thinks it should be on by
default, I think it should be off so that the functions does the same as
in Lisp systems ... Perhaps someone else can decide.

-k

*** apropos.R **********************************************************
apropos <- function (pattern) {
  x <- character(0)
  for (i in 1 : length(search()))
    x <- c(x, ls(pos =3D i, pattern =3D pattern))
  x
}

apropos <- function (pattern, where =3D FALSE) {
  ## Give all objects which match  _pattern_.
  ## By	Kurt Hornik & Martin M=E4chler, May 1997.
  x <- character(0)
  for (i in seq(search())) {
    ll <- length(li <- ls(pos =3D i, pattern =3D pattern))
    if (ll)
      if (where)
	x <- c(x, structure(li, names =3D rep(i, ll)))
      else
	x <- c(x, li)
  }
  x
}
*** apropos.d **********************************************************
TITLE(apropos @@ Find all objects with names matching a pattern)
USAGE(apropos(pattern, where =3D FALSE))
ARGUMENTS(
ARG(pattern @@ a regular expression to match against)
ARG(where @@ a logical indicating whether positions in the search list
should also be returned)
)
DESCRIPTION(
LANG(apropos) returns a vector of character strings giving the names of
all objects in the search list matching LANG(pattern).  If LANG(where)
is LANG(TRUE), their position in the search list is returned as their
names attribute.
)
EXAMPLES(
apropos("lm")
apropos("ls")
apropos("lq")
BLANK
# need the `\\'
apropos("\\[")
BLANK
# everything
length(apropos("."))
BLANK
# those starting with `pr'
apropos("^pr")
BLANK
# the 1-letter things
apropos("^.$")
# the 1-2-letter things
apropos("^..?$")
# the 2-to-4 letter things
apropos("^.{2,4}$")
BLANK
# the 8-and-more letter things
apropos("^.{8,}$")
table(nchar(apropos("^.{8,}$")))
)

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


R-alpha: apropos() available
Kurt Hornik