[R] Function scope issues

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 17 12:47:52 CET 2011


On 11-02-16 10:23 PM, Sébastien Bihorel wrote:
> Dear R-users,
>
> I have some questions about the scope of functions, which I would like to
> illustrate with the following example:
>
> ### First example
> require(Hmisc)
>
> combine.levels<- function(x,y) browser()
> whatever<- function(x,y) combine.levels(x,y)
>
> x<- 1:5
> names(x)<- LETTERS[x]
> y<- 6: 10
> names(y)<- LETTERS[y-2]
>
> combine(x,y)  # no debugging break
> whatever(x,y)  # debugging break
>
>
> The Hmisc package contains the two original functions 'combine' and
> 'combine.levels': 'combine' calls 'combine.levels' to perform some
> operations. Now, by defining my own (useless) 'combine.levels' function, I
> thought I would mask Frank Harrell's function for all functions calling
> 'combine.levels' in my downstream code. This is obviously not the case, as
> illustrated by the two last calls of the script above. Therefore, my
> questions are:
> - how can I completely mask the Hmisc 'combine.levels' function in my
> example above, while still making the remaining function of Hmisc available?

Modify the source to Hmisc, or assign a new value in the Hmisc namespace 
(but that's something you should only do in debugging, never in released 
code).

> - if I pack the function 'whatever' and my version of 'combine.levels' in a
> new package and modify my example as follows, will I get the same problem of
> scope?

Yes.

Duncan Murdoch

>
> ### Second example
> require(Hmisc)
> require(myUselessPackage) # contains custom functions: whatever and
> combine.levels
>
> x<- 1:5
> names(x)<- LETTERS[x]
> y<- 6: 10
> names(y)<- LETTERS[y-2]
>
> combine(x,y)
> whatever(x,y)
>
> Thanks in advance of your help.
>
> Sebastien
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list