Skip to content

[Bioc-devel] Use set.seed inside function

7 messages · Meng Chen, James W. MacDonald, Henrik Bengtsson +3 more

#
Dear BioC team and developers,

I am using BiocCheck to check my package, it returns a warning:
" Remove set.seed usage in R code"

I am using "set.seed" inside my functions, before calling function
distinctColorPalette (randomcoloR package) in order to generate
reproducible "random distinct colors". So what would be the best practice
to solve this warning? I think
1. use set.seed and don't change anything.
2. use the set.seed function, but include something like below inside the
function
*gl.seed <- .Random.seed*
*on.exit(assign(".Random.seed", gl.seed, envir = .GlobalEnv))*
3. use some other functions for the purpose

Any suggestions will be appreciated. Thanks.
#
It appears that you don't actually want random colors, but instead you want the same colors each time. Why not just generate the vector of 'random distinct colors' one time and save the vector of colors?

-----Original Message-----
From: Bioc-devel <bioc-devel-bounces at r-project.org> On Behalf Of Meng Chen
Sent: Monday, November 29, 2021 3:21 PM
To: bioc-devel at r-project.org
Subject: [Bioc-devel] Use set.seed inside function

Dear BioC team and developers,

I am using BiocCheck to check my package, it returns a warning:
" Remove set.seed usage in R code"

I am using "set.seed" inside my functions, before calling function distinctColorPalette (randomcoloR package) in order to generate reproducible "random distinct colors". So what would be the best practice to solve this warning? I think 1. use set.seed and don't change anything.
2. use the set.seed function, but include something like below inside the function *gl.seed <- .Random.seed* *on.exit(assign(".Random.seed", gl.seed, envir = .GlobalEnv))* 3. use some other functions for the purpose

Any suggestions will be appreciated. Thanks.
--
Best Regards,
Chen


_______________________________________________
Bioc-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/bioc-devel
#
Thanks. I think it may work in theory, generating "enough" distinct colors
is fairly easy. Then the problem will be how to find a subset of colors of
size n, and the selected colors are still most distinguishable. I think I
will do this with my eyes if no other methods, a tedious job.
But at least for my curiosity, I still want to know if there are other ways
to achieve this. I feel like 80% of people who use the distinctColorPallete
function actually don't need the "random" feature :) Thanks.
On Mon, Nov 29, 2021 at 9:39 PM James W. MacDonald <jmacdon at uw.edu> wrote:

            

  
    
#
The easiest is to use withr::with_seed(), e.g.
[1] "#A0E1BC" "#B8E363" "#D686BE" "#DEA97F" "#B15CD8" "#A2B9D5"
[1] "#A0E1BC" "#B8E363" "#D686BE" "#DEA97F" "#B15CD8" "#A2B9D5"

It works by undoing globalenv()$.Random.seed after the random number
generator has updated.  If you want to roll your own version of this,
you need to make sure to handle the special case when there is no
pre-existing .Random.seed in globalenv().

Regarding packages and functions changing the random seed via a
set.seed() [without undoing it]: this should *never* be done, because
it will wreak havoc on a analyses and studies that rely on random
numbers.  My rule of thumb: only the end-user should be allowed to use
set.seed(), which should typically be done at the top of their R
scripts.

/Henrik
On Mon, Nov 29, 2021 at 1:23 PM Meng Chen <mengchen18 at gmail.com> wrote:
#
Thanks, and good to know the with_seed function.

best,
Chen
On 29.11.21 22:43, Henrik Bengtsson wrote:
#
Check out grDevices::hcl.pals (also https://www.zeileis.org/papers/Zeileis+Hornik+Murrell-2009.pdf) or the RColorBrewer package (also https://colorbrewer2.org) for principled selection of colors. Sounds like you're interested in 'qualitative' color palletes.

Martin Morgan

?On 11/29/21, 4:23 PM, "Bioc-devel on behalf of Meng Chen" <bioc-devel-bounces at r-project.org on behalf of mengchen18 at gmail.com> wrote:

    Thanks. I think it may work in theory, generating "enough" distinct colors
    is fairly easy. Then the problem will be how to find a subset of colors of
    size n, and the selected colors are still most distinguishable. I think I
    will do this with my eyes if no other methods, a tedious job.
    But at least for my curiosity, I still want to know if there are other ways
    to achieve this. I feel like 80% of people who use the distinctColorPallete
    function actually don't need the "random" feature :) Thanks.
On Mon, Nov 29, 2021 at 9:39 PM James W. MacDonald <jmacdon at uw.edu> wrote:
> It appears that you don't actually want random colors, but instead you
    > want the same colors each time. Why not just generate the vector of 'random
    > distinct colors' one time and save the vector of colors?
    >
    > -----Original Message-----
    > From: Bioc-devel <bioc-devel-bounces at r-project.org> On Behalf Of Meng Chen
    > Sent: Monday, November 29, 2021 3:21 PM
    > To: bioc-devel at r-project.org
    > Subject: [Bioc-devel] Use set.seed inside function
    >
    > Dear BioC team and developers,
    >
    > I am using BiocCheck to check my package, it returns a warning:
    > " Remove set.seed usage in R code"
    >
    > I am using "set.seed" inside my functions, before calling function
    > distinctColorPalette (randomcoloR package) in order to generate
    > reproducible "random distinct colors". So what would be the best practice
    > to solve this warning? I think 1. use set.seed and don't change anything.
    > 2. use the set.seed function, but include something like below inside the
    > function *gl.seed <- .Random.seed* *on.exit(assign(".Random.seed", gl.seed,
    > envir = .GlobalEnv))* 3. use some other functions for the purpose
    >
    > Any suggestions will be appreciated. Thanks.
    > --
    > Best Regards,
    > Chen
    >
    >         [[alternative HTML version deleted]]
    >
    > _______________________________________________
    > Bioc-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/bioc-devel
    >


    -- 
    Best Regards,
    Chen


    _______________________________________________
    Bioc-devel at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/bioc-devel
#
Martin's suggestion is the way to go. For display purposes, you basically
never want random colors, you almost always want to select distinct colors
and there are principled ways of doing that.

Best,
Kasper

On Tue, Nov 30, 2021 at 6:59 AM Martin Morgan <mtmorgan.bioc at gmail.com>
wrote: