[R] R debugging options

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Apr 22 16:31:52 CEST 2006


On Sat, 22 Apr 2006, John Fox wrote:

> 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)?

In this case, just to do the is.function test at C level.  There should be 
no extra copying in your code, but all versions will force promises, which 
under lazy-loading will load all objects in the environment.  So 
potentially this sort of thing is wasteful, and that is why I at least 
have resisted putting a version in base R.

[Incidentally, just from thinking about your comment I have corrected 
ls.str, documented what a NULL index does and corrected the 
documentation of exists.]

Brian

>
> 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:ripley at stats.ox.ac.uk]
>> Sent: Saturday, April 22, 2006 3:08 AM
>> To: John Fox
>> Cc: r-help at stat.math.ethz.ch; '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:spencer.graves at pdf.com]
>>>>> Sent: Friday, April 21, 2006 11:21 AM
>>>>> To: John Fox
>>>>> Cc: 'Larry Howe'; r-help at stat.math.ethz.ch; 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: r-help-bounces at stat.math.ethz.ch
>>>>>>> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
>> Larry Howe
>>>>>>> Sent: Tuesday, April 18, 2006 12:46 PM
>>>>>>> To: r-help at stat.math.ethz.ch
>>>>>>> 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
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> 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
>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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
>>>>
>>>> ______________________________________________
>>>> 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
>>>>
>>>
>>>
>>
>> --
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> 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
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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




More information about the R-help mailing list