[R] Accessor functions in lattice graphics

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Jun 20 20:25:53 CEST 2011


On Mon, Jun 20, 2011 at 9:22 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Jun 20, 2011, at 10:49 AM, Bert Gunter wrote:
>
>> Hi all:
>>
>> Thanks, David. This is a good example of knowledgeable "R forensic
>> investigation." I leave it to Frank whether it meets his criteria.
>>
>> However, I would argue that that this is bad practice and quite
>> unwise. In general, these details are implementation dependent and
>> could change. Yes, they are exposed, but only because everything is in
>> R. The more desirable and safer way to do things would be to use
>> accessor functions. I believe Frank's question was to ask whether such
>> accessor functions exist. If they do not, then of course one is stuck
>> with writing one based on details like thoseyou have elucidated. But
>> this is really not good programming practice, imo.
>>
>> Contrary and more informed views welcome.
>
> All reasonable comments. I would offer in my defense (but of only the second
> suggestion) the documentation of the lattice-object:
>
> http://finzi.psych.upenn.edu/R/library/lattice/html/trellis.object.html
>
> ... which does suggest that one can rely on $condlevel being a designed
> named component of a lattice-object.

It is, and a direct accessor is dimnames().

> lattice:::dimnames.trellis
function (x)
x$condlevels

> The strip.default and strip.custom functions access a variable in the
> environment of the lattice object creation named "var.name" but I do not see
> of a method to access its values without using the "<<-" construction ....
> generally thought to violate "good practice".

'var.name' is a variable passed to the strip function, and is indeed
not supposed to be accessible from the panel function.

My original intent was to make only the indices available through the
which.packet() accessor function (as it is not clear what else would
be useful for shingles), and the user could pass in the levels as
extra arguments. Note however that by default plot.trellis() saves the
object it is plotting, and it can be accessed from inside the panel
function:

barchart(VADeaths, groups = FALSE,
         panel = function(...) print(dimnames(trellis.last.object())))

The fact that the object is saved _before_ drawing starts is
undocumented, but unlikely to change (and I have no objections to
documenting it).

-Deepayan

>
> --
> David
>>
>> Cheers,
>> Bert
>>
>> On Mon, Jun 20, 2011 at 7:17 AM, David Winsemius <dwinsemius at comcast.net>
>> wrote:
>>>
>>> Frank Harrell wrote:
>>>>
>>>> I know about the current.row, current.column, and panel.number functions
>>>> that are useful within panel functions written for lattice.  Are there
>>>> easy ways to obtain the names of the conditioning variables (those
>>>> appearing after |) and their values for the current panel?
>>>> Thanks
>>>> Frank
>>>>
>>>
>>> Using one of the examples in help(xyplot):
>>>
>>> states <- data.frame(state.x77,
>>>                    state.name = dimnames(state.x77)[[1]],
>>>                    state.region = state.region)
>>> testp <- xyplot(Murder ~ Population | state.region, data = states,
>>>      groups = state.name,
>>>      panel = function(x, y, subscripts, groups) {
>>>          ltext(x = x, y = y, labels = groups[subscripts], cex=1,
>>>                fontfamily = "HersheySans")
>>>      })
>>> str(testp)
>>>
>>> I see that the names of the state.regions are in an attribute of
>>> testp$packet.sizes, so:
>>>
>>>> attr(testp$packet.sizes, "dimnames")[["state.region"]][4]
>>>
>>> #[1] "West"
>>>
>>> #Looking for a further example in help(xyplot with 2 "panel dimensions" I
>>> tried:
>>>
>>>> testp2 <- dotplot(variety ~ yield | year * site, data=barley)
>>>
>>> #I'm not quite sure how the "rows" and "columns" are defined , but I get:
>>>
>>>> attr(testp2$packet.sizes, "dimnames")[[1]][2]
>>>
>>> [1] "1931"
>>>>
>>>> attr(testp2$packet.sizes, "dimnames")[[2]][1]
>>>
>>> [1] "Grand Rapids"
>>>
>>> I'm not sure this is the "right answer" because after looking further, I
>>> now
>>> see a list node entitled $condlevels which returns the same information
>>> is a
>>> much more straightforward fashion:
>>>
>>>> testp2 $ condlevels
>>>
>>> $year
>>> [1] "1932" "1931"
>>>
>>> $site
>>> [1] "Grand Rapids"    "Duluth"          "University Farm" "Morris"
>>> [5] "Crookston"       "Waseca"
>>>
>>> (And other nodes that contain information about conditioning levels:
>>> $ index.cond       :List of 2
>>>  ..$ : int [1:2] 1 2
>>>  ..$ : int [1:6] 1 2 3 4 5 6
>>>  $ perm.cond        : int [1:2] 1 2
>>>  $ condlevels       :List of 2
>>>  ..$ year: chr [1:2] "1932" "1931"
>>>  ..$ site: chr [1:6] "Grand Rapids" "Duluth" "University Farm" "Morris"
>>> ...
>>>
>>> HTH;
>>>
>>> David.
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Accessor-functions-in-lattice-graphics-tp3609435p3611509.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> "Men by nature long to get on to the ultimate truths, and will often
>> be impatient with elementary studies or fight shy of them. If it were
>> possible to reach the ultimate truths without the elementary studies
>> usually prefixed to them, these would not be preparatory studies but
>> superfluous diversions."
>>
>> -- Maimonides (1135-1204)
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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