R:调色板(彩虹())未在第一次尝试时更新

R: palette(rainbow()) not being updated for the first attemp

我第一次尝试使用函数 palette(rainbow()) 更新颜色似乎失败了,但第二次尝试成功了。有没有更聪明的方法来更新颜色,而不是我调用函数两次的方法?

# call the function 
colRainbow = palette(rainbow(3))
colRainbow
# [1] "red"   "green" "blue" 
# success!

# call the function, overwrite the object colRainbow 
colRainbow = palette(rainbow(5))
colRainbow
# [1] "red"   "green" "blue" 
# Fail!

# Re-overwrite the object
colRainbow = palette(rainbow(5))
colRainbow
# [1] "red"     "#CCFF00" "#00FF66" "#0066FF" "#CC00FF"
# success!

palette 的文档说 returns:

A character vector giving the palette which was in effect

所以看到

colRainbow = palette(rainbow(3))
colRainbow = palette(rainbow(5))
colRainbow
# [1] "red"   "green" "blue" 

是你应该期待的,你看之前的调色板。在 R 中设置选项的函数通常以这种方式工作,因此您可以这样做 old_options = set_options(new_value),然后您可以存储旧值以供日后恢复。

要在每次更改后查看当前调色板,只需不带参数调用 palette()