# [R] How to use rainbow function without the gamma argument

Boris Steipe boris.steipe at utoronto.ca
Sun Apr 20 02:16:21 CEST 2014

```If it MUST be parameter-compatible with the old call, you could just add "..." to your local version of rainbow. The unused parameter will then be dropped.

Here's how:

# The original creates an error ...
rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75, gamma = 1.5)

Error in rainbow(100, s = 1, v = 0.75, start = 0, end = 0.75, gamma = 1.5) :
unused argument (gamma = 1.5)

# The code of the function is here:
> rainbow
function (n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n,
alpha = 1)
{
if ((n <- as.integer(n[1L])) > 0) {
if (start == end || any(c(start, end) < 0) || any(c(start,
end) > 1))
stop("'start' and 'end' must be distinct and in [0, 1].")
hsv(h = seq.int(start, ifelse(start > end, 1, 0) + end,
length.out = n)%%1, s, v, alpha)
}
else character()
}
<bytecode: 0x101968950>
<environment: namespace:grDevices>

# I add ... to the parameters and define a local version of rainbow

rainbow = function (n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n,
alpha = 1, ...)
{
if ((n <- as.integer(n[1L])) > 0) {
if (start == end || any(c(start, end) < 0) || any(c(start,
end) > 1))
stop("'start' and 'end' must be distinct and in [0, 1].")
hsv(h = seq.int(start, ifelse(start > end, 1, 0) + end,
length.out = n)%%1, s, v, alpha)
}
else character()
}

# same code except for the dots...
# Now it accepts and discards unused arguments
> rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75, gamma = 1.5, gefingerpoken = TRUE)
[1] "#BF0000FF" "#BF0900FF" "#BF1100FF" "#BF1A00FF" "#BF2300FF" "#BF2B00FF" "#BF3400FF"
[...]
[99] "#5700BFFF" "#6000BFFF"

# and if I want the original back, I just delete my local version ...
> rm(rainbow)
> rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75, gamma = 1.5)
Error in rainbow(100, s = 1, v = 0.75, start = 0, end = 0.75, gamma = 1.5) :
unused argument (gamma = 1.5)

> rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75)
[1] "#BF0000FF" "#BF0900FF" "#BF1100FF" "#BF1A00FF" "#BF2300FF" "#BF2B00FF" "#BF3400FF"
[...]
[99] "#5700BFFF" "#6000BFFF"

You can read about the '...' argument in the Introduction to R. Here we use it not to pass variables on, but to have them not cause an error when present.

HOWEVER: I personally would consider this poor style. It's probably better to review and update your old code. There may be other less obvious problems.

YMMV
B.

On 2014-04-19, at 7:19 PM, Francesco Brundu wrote:

> Hi Boris,
> yes I tried this way and it worked. The fact is that I wanted to be compliant with the old code, I did not want to change anything. So I wanted to find a new way to rewrite the code.
> Thanks
>
> ~ Francesco Brundu
>
>
> On 19 April 2014 23:18, Boris Steipe <boris.steipe at utoronto.ca> wrote:
> Have you looked at ?rainbow ?
> Is there a reason why you don't simply leave the gamma parameter away?
>
> Try:
> pie(rep(1,100), col=rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75))
>
> Cheers,
> B.
>
>
> On 2014-04-19, at 6:05 AM, Francesco Brundu wrote:
>
> > Hi all,
> > I am using an old code (probably written for R 2.5) and it stops when
> > calling rainbow() with gamma argument. I saw that gamma argument is not
> > present in newer version of R rainbow function. How can I translate this
> > line of code:
> >
> > rainbow(100, s = 1.0, v = 0.75, start = 0.0, end = 0.75, gamma = 1.5)
> >
> > ?
> >
> > It fails with:
> >
> > Error in rainbow(100, s = 1, v = 0.75, start = 0, end = 0.75, gamma = 1.5)
> > :
> >  unused argument (gamma = 1.5)
> > Calls: nmfconsensus ... matrix.abs.plot -> image -> image.default -> rainbow
> > Execution halted
> >
> > Thanks
> >
> > ~ Francesco Brundu
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help