Dear Brian, I figured that there was a reason for using exists() that I didn't see. Do you prefer using exists() to get() for reasons of efficiency (e.g., minimizing object copying)?
Thanks for this, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -------------------------------- > -----Original Message----- > From: Prof Brian Ripley [mailto:[EMAIL PROTECTED] > Sent: Saturday, April 22, 2006 3:08 AM > To: John Fox > Cc: [email protected]; 'Spencer Graves' > Subject: Re: [R] R debugging options > > On Sat, 22 Apr 2006, Prof Brian Ripley wrote: > > > On Fri, 21 Apr 2006, John Fox wrote: > > > >> Dear Spencer, > >> > >> I wasn't aware of getFunctions(), though I've now taken a > look at it. > >> It's pretty similar to listFunctions() (from my email to > the list), > >> except that > >> getFunctions() uses exists() rather than is.function() to test > >> whether an object is a function. There must be a reason > for this, but > >> I can't think what it is, since in both cases the vector of object > >> names shouldn't include nonexistent objects. > > > > John, > > > > Your code uses eval(parse()) to find the object, and that > is somewhat > > clumsy. A more usual way would be > > > > function(x) is.function(get(x, envir=envir)) > > > > and exists() can shortcircuit that by asking for a > particular 'mode'. > > There's a prototypical version in utils::ls.str of using > exists(), and > > a simpler yet more powerful version than either of yours would be > > > > listFunctions <- function(all.names=FALSE, envir=.GlobalEnv){ > > # all.names=TRUE: include names beginning with "." > > # envir: environment to search > > envir <- as.environment(envir) > > z <- ls(envir=envir, all.names=all.names) > > z[sapply(z, function(x) exists(x, mode="function", > > envir=envir, inherits=FALSE))] } > > Aargh, sapply does not simplify if the result is of length 0. Use > unlist(lapply()) instead (which is incidentally slightly more > efficient). > > > Note that inherits=FALSE is needed, for although the named object > > exists, > > exists() has been told to search for a function of that > name and might > > find one elsewhere. (ls.str appears to have that wrong.) > > > > Now for some pickiness: what is a 'function'? This lists "$" as a > > function, which it is, but it is not a closure. Although > > exists(mode="closure") would appear to be documented to > differentiate, > > it does not and so one needed function(x) typeof(get(x, > ...)) == "closure". > > > > Brian > > > >> > >> Regards, > >> John > >> > >> -------------------------------- > >> John Fox > >> Department of Sociology > >> McMaster University > >> Hamilton, Ontario > >> Canada L8S 4M4 > >> 905-525-9140x23604 > >> http://socserv.mcmaster.ca/jfox > >> -------------------------------- > >> > >>> -----Original Message----- > >>> From: Spencer Graves [mailto:[EMAIL PROTECTED] > >>> Sent: Friday, April 21, 2006 11:21 AM > >>> To: John Fox > >>> Cc: 'Larry Howe'; [email protected]; Philippe Grosjean > >>> Subject: Re: [R] R debugging options > >>> > >>> Regarding a function that lists functions, have you > considered > >>> "getFunctions" in library(svIDE)? You need to provide > the argument, > >>> as > >>> in "getFunctions(1)"; "getFunctions()" returns an error message. > >>> > >>> Beyond this, the "objects" function in S-Plus (at > least version > >>> 6.2) has a "classes" argument, which the R > >>> 2.2.1 implementation does not have. It doesn't look like > it would > >>> be too difficult to add such an argument to "objects" > >>> in R, but I have not been in a position to volunteer to > do it, and > >>> without that, I didn't feel it was appropriate for me to > suggest it. > >>> > >>> hope this helps, > >>> spencer graves > >>> > >>> John Fox wrote: > >>> > >>>> Dear Larry, > >>>> > >>>> I'm not aware of an existing function that lists functions, > >>> but here's > >>>> a simple solution: > >>>> > >>>> listFunctions <- function(all.names=FALSE, envir=.GlobalEnv){ > >>>> # all.names=TRUE: include names beginning with "." > >>>> # envir: environment to search > >>>> Objects <- objects(envir, all.names=all.names) > >>>> if (length(Objects) == 0) Objects > >>>> else names(which(sapply(Objects, > >>>> function(object) is.function(eval(parse(text=object), > >>>> envir=envir))))) > >>>> } > >>>> > >>>> Getting mtrace() to use the function names returned by > >>> listFunctions() > >>>> is a bit tricky, because of the way mtrace() evaluates its > >>> arguments. > >>>> You could do something like the following: > >>>> > >>>> for(f in listFunctions()) mtrace(char.fname=f) > >>>> > >>>> Perhaps someone else knows of an existing or better solution. > >>>> > >>>> I hope this helps, > >>>> John > >>>> > >>>> -------------------------------- > >>>> John Fox > >>>> Department of Sociology > >>>> McMaster University > >>>> Hamilton, Ontario > >>>> Canada L8S 4M4 > >>>> 905-525-9140x23604 > >>>> http://socserv.mcmaster.ca/jfox > >>>> -------------------------------- > >>>> > >>>> > >>>>> -----Original Message----- > >>>>> From: [EMAIL PROTECTED] > >>>>> [mailto:[EMAIL PROTECTED] On Behalf Of > Larry Howe > >>>>> Sent: Tuesday, April 18, 2006 12:46 PM > >>>>> To: [email protected] > >>>>> Subject: Re: [R] R debugging options > >>>>> > >>>>> On Monday April 17 2006 21:08, Francisco J. Zagmutt wrote: > >>>>> > >>>>>> RSiteSearch("debug") or RSiteSearch("debugging") will give > >>>>> > >>>>> you a lot > >>>>> > >>>>>> or relevant information. I personally use library(debug) > >>>>> > >>>>> extensivelly > >>>>> > >>>>>> and it should do all the taks you asked about. There is a > >>>>> > >>>>> nice article > >>>>> > >>>>>> describing the debug lilbrary in the 2003/3 issue of R News > >>>>>> http://cran.r-project.org/doc/Rnews/Rnews_2003-3.pdf > >>>>>> > >>>>>> Cheers > >>>>>> > >>>>>> Francisco > >>>>> > >>>>> Wow! That is a great package. I think it does all I need. > >>>>> > >>>>> Is there a way to turn on debugging for all loaded functions? > >>>>> My source file contains many functions and I would prefer > >>> not to have > >>>>> to mtrace() each one. > >>>>> Something like > >>>>> > >>>>> > >>>>>> mtrace(how_do_I_get_a_list_of_all_loaded_functions) > >>>>> > >>>>> ? > >>>>> > >>>>> Larry > >>>>> > >>>>> ______________________________________________ > >>>>> [email protected] mailing list > >>>>> https://stat.ethz.ch/mailman/listinfo/r-help > >>>>> PLEASE do read the posting guide! > >>>>> http://www.R-project.org/posting-guide.html > >>>> > >>>> > >>>> ______________________________________________ > >>>> [email protected] mailing list > >>>> https://stat.ethz.ch/mailman/listinfo/r-help > >>>> PLEASE do read the posting guide! > >>> http://www.R-project.org/posting-guide.html > >> > >> ______________________________________________ > >> [email protected] mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide! > >> http://www.R-project.org/posting-guide.html > >> > > > > > > -- > Brian D. Ripley, [EMAIL PROTECTED] > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
