element replacement in grid unit vector

2019-05-03 02:03发布

问题:

I've been baffled by this a few times already, so here's a made-up question for others who might stumble upon the same problem.

Consider this grid unit vector,

a = unit(1:3, c("cm", "in", "npc"))

I want to replace some elements with new values. The natural approach would be,

a[1] = unit(2,"pt")
a
# [1] 2cm  2in  3npc

Something went wrong: only the numeric value was changed, not the unit. Why? What to do?

Edit: As pointed out in one answer below, such units are just numeric vectors with attributes. However, their offsprings unit.arithmetic and unit.list should also be considered solution to be fully general (e.g to use in adjusting panel sizes of ggplot objects). Consider this unit vector,

(b = a + unit(1, "npc"))
# [1] 1cm+1npc  2in+1npc  3npc+1npc
# [1] "unit.arithmetic" "unit"   

Now replacing a specific element is more tricky, since they're not atomic anymore.

回答1:

Following a discussion with Paul Murrell (and, amusingly, re-inventing what I'd figured out before), the problem lies in the absence of a [<- method for grid units. The long-term fix would be to implement those methods, but it's not trivial since grid units come with siblings such as unit.arithmetic and unit.list, and their interaction can become hard to comprehend.

The easier, user-oriented fix, is to convert such unit vectors to unit.list objects, which will inherit an accessor method more like regular R lists. This promotion to unit.list object can be done with the unexported function grid:::unit.list().

a = unit(1:3, c("cm", "in", "npc"))
b = grid:::unit.list(a)
is.list(b) # check that indeed this is a list object, thanks @Josh O'Brien
# [1] TRUE
# so now we can use standard list methods
b[[1]] = unit(2,"pt")
b
#[1] 2pt  2in  3npc


回答2:

grid now has a [<-.unit method.



回答3:

Looks like a is just an atomic vector with some attributes attached to it. So, when you use a[1] = unit(2,"pt") , the new unit function creates another atomic vector of length one which replaces the value of a[1]. The attributes stay untouched.

So, something like this seems to be working:

a[1] <- 2
attr(a, 'unit')[1] <- 'pt'

> a
[1] 2pt  2in  3npc


标签: r r-grid