[R] avoid <<- in specific case

Duncan Murdoch murdoch.duncan at gmail.com
Fri Oct 5 14:25:57 CEST 2012


On 05/10/2012 8:19 AM, Berry Boessenkool wrote:
>
> Hi all,
>
> I improved a function drawing horizontal histograms (code below) which uses barplot and works fine.
> It does,however, need to assign a function to the global environment to later find the actual location on the vertical axis, and not the number of bars used by barplot.
> Hopefully, running the examples below will illustrate that.
>
> As said, it works perfectly fine and does exactly what I want.
> The problem arises now that I'm building a package (just for myself, for now) and run into R CMD
> check telling me 'no visible binding for '<<-' assignment'.
> (wording below)
> Similar problem as in http://r.789695.n4.nabble.com/R-CMD-check-tells-me-no-visible-binding-for-global-variable-what-does-it-mean-td1837236.html
>
> My question is:
> Is there a way to avoid assigning the function to the global environment with <<- but still have it available? I know it is generally not good practice.

You can return the function as the value of your function.   A bonus:  
if it is created within the body of your function, it will have access 
to all the local variables there.

You shouldn't write to the global environment, because globalenv belongs 
to the user, not to you.  If the user wants your function in the global 
environment s/he can just assign the value of your function to a 
variable there.

Duncan Murdoch


> Or ist it OK in a case like this, and is there a way to avoid the notes from the rcmd check (make the function package-compatible)?
> Or should I just ignore these notes? (I'm completely new to building packages and cannot judge the importance yet.)
>
> I'd be very thankful for any hints!
>
> Berry
>
> PS: I recently read about barcharts in lattice, but by now I'm already used to my function. (And I learned a lot writing it a couple of years ago).
>
>   
> # Function
> horiz.hist <- function(Data, breaks="Sturges", col="transparent", las=1,
> ylim=range(HBreaks), labelat=pretty(ylim), labels=labelat, border=par("fg"), ... )
>    {a <- hist(Data, plot=FALSE, breaks=breaks)
>    HBreaks <- a$breaks
>    HBreak1 <- a$breaks[1]
>    hpos <<- function(Pos) (Pos-HBreak1)*(length(HBreaks)-1)/ diff(range(HBreaks)) # Assign a function to the global environment with values calculated inside the main function.
>    barplot(a$counts, space=0, horiz=T, ylim=hpos(ylim), col=col, border=border,...)
>    axis(2, at=hpos(labelat), labels=labels, las=las, ...)
>    print("use hpos() to address y-coordinates") }
>
> # Data and basic concept
> set.seed(8); ExampleData <- rnorm(50,8,5)+5
> hist(ExampleData)
> horiz.hist(ExampleData, xlab="absolute frequency")
> # Caution: the labels at the y-axis are not the real coordinates!
> # abline(h=2) will draw above the second bar, not at the label value 2. Use hpos:
> abline(h=hpos(11), col=2)
>
> # Further arguments
> horiz.hist(ExampleData, xlim=c(-8,20))
> horiz.hist(ExampleData, main="the ... argument worked!", col.axis=3)
> hist(ExampleData, xlim=c(-10,40)) # with xlim
> horiz.hist(ExampleData, ylim=c(-10,40), border="red") # with ylim
> horiz.hist(ExampleData, breaks=20, col="orange")
> axis(2, hpos(0:10), labels=F, col=2) # another use of hpos()
>
> # One shortcoming: doesn't work with breakpoints provided as a vector with different widths of the bars
>
>
> Wording from the rcmd check when building a package:
> * checking R code for possible problems ... NOTE
> horiz.hist: no visible binding for '<<-' assignment to 'hpos'
> horiz.hist: no visible global function definition for 'hpos'
>
>
>   		 	   		
> 	[[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