[R] listing all functions in R

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Jan 6 17:16:01 CET 2007


On Sat, 2007-01-06 at 10:58 -0500, Gabor Grothendieck wrote:
> The arguments to the functions can differ too even if they
> exist on multiple platforms.   system() on Windows has the
> input= argument but not on UNIX.

That's a good point Gabor, and one I hadn't considered as yet. As I'm
only just setting out on the road to providing R help resources for the
wider world (rather than the limited environs of the courses I have
run), I tend to not have thought about these things much - though I
guess I have a few gotchas waiting to bite me in the ass before too
long.

I am just starting to think about the best way to organise the snippets
of code to allow me to keep them up-to-date with current R and changes
in package code that the snippets use. Dropping the code verbatim into
PHP scripts isn't a good idea. At the moment I intend to store all
snippets in individual *.R files and read them into to variables within
the PHP scripts, from where they will be highlighted and formatted for
display.

It would be reasonably easy to write an R script to source all *.R files
in a directory to look for errors and problems. And having them all as
separate files means I can still use Emacs/ESS to prepare, format, and
run the code through R, which is my preferred environment.

All the best,

G

> 
> On 1/6/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> > On 1/6/2007 9:25 AM, Gavin Simpson wrote:
> > > On Sat, 2007-01-06 at 13:48 +0000, Prof Brian Ripley wrote:
> > >> Could you tell us what you mean by
> > >
> > > Thank you for your reply, Prof. Ripley.
> > >
> > >> - 'function'  (if() and + are functions in R, so do you want those?)
> > >
> > > I was thinking about functions that are used like this: foo()
> > > So I don't need things like "names<-". I don't need functions like +. -,
> > > $, as I can highlight the separately if desired, though I'm not doing
> > > this at the moment.
> > >
> > > Functions like for() while(), if() function() are handled separately.
> > >
> > >> - 'a base R installation'?   What is 'base R' (standard + recommended
> > >> packages?)  And on what platform: the list is platform-specific?
> > >
> > > Yes, I mean standard + recommended packages. As for platform, most of my
> > > intended audience will be MS Windows users, though I am using Linux
> > > (Fedora) to generate this list (i.e. my R installation is on Linux).
> >
> > Be careful:  the installed list of functions differs slightly from
> > platform to platform.  For example, on Windows there's a function
> > choose.dir in the utils package, but I don't think this exists on Unix.
> >
> > The list also varies from version to version, so if you could manage to
> > run some code in the user's installed R to generate the list on the fly,
> > you'd get the most accurate list.
> >
> > Duncan Murdoch
> >
> > >
> > >> Here is a reasonable shot:
> > >>
> > >> findfuns <- function(x) {
> > >>      if(require(x, character.only=TRUE)) {
> > >>         env <- paste("package", x, sep=":")
> > >>         nm <- ls(env, all=TRUE)
> > >>         nm[unlist(lapply(nm, function(n) exists(n, where=env,
> > >>                                                mode="function",
> > >>                                                inherits=FALSE)))]
> > >>      } else character(0)
> > >> }
> > >> pkgs <- dir(.Library)
> > >> z <-  lapply(pkgs, findfuns)
> > >> names(z) <- pkgs
> > >
> > > Excellent, that works just fine for me. I can edit out certain packages
> > > that I don't expect to use, before formatting as desired. I can also use
> > > this function on a library of packages that I use regularly and will be
> > > using in the web pages.
> > >
> > >> I don't understand your desired format, but
> > >>
> > >> write(sQuote(sort(unique(unlist(z)))), "")
> > >
> > > I wanted a single string "...", with entries enclosed in "''" and
> > > separated by "," (this is to go in a PHP array). I can generate such a
> > > string from your z, above, as follows:
> > >
> > > paste(sQuote(sort(unique(unlist(z)), decreasing = TRUE)),
> > >       collapse = ", ")
> > >
> > >> gives a single-column quoted list.  It does include internal functions,
> > >> operators, S3 methods ... so you probably want to edit it.
> > >
> > > Once again, thank you.
> > >
> > > All the best
> > >
> > > Gav
> > >
> > >>
> > >> On Sat, 6 Jan 2007, Gavin Simpson wrote:
> > >>
> > >>> Dear List,
> > >>>
> > >>> I'm building an R syntax highlighting file for GeSHi [*] for a website I
> > >>> am currently putting together. The syntax file needs a list of keywords
> > >>> to highlight. How can I generate a list of all the functions in a base R
> > >>> installation?
> > >>>
> > >>> Ideally the list would be formatted like this:
> > >>>
> > >>> "'fun1', 'fun2', 'fun3'"
> > >>>
> > >>> when printed to the screen so I can copy and paste it into the syntax
> > >>> file.
> > >>>
> > >>> I'm sure this has been asked before, but I stupidly didn't save that
> > >>> email and I couldn't come up with a suitable query parameter for
> > >>> Jonathan Baron's search site to return results before timing out.
> > >>>
> > >>> Thanks in advance,
> > >>>
> > >>> Gav
> > >>>
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT                          [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list