[R] Limiting the scope of RNGkind/set.seed
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Tue Apr 16 18:36:40 CEST 2019
I think I'm missing something. Why does something like this not do what you
want:
> RNGkind()
[1] "Mersenne-Twister" "Inversion"
> f <- function(){
+ cur <- RNGkind(NULL)[1]
+ RNGkind("Super-Duper")
+ print(RNGkind())
+ RNGkind(cur)
+ }
> f()
[1] "Super-Duper" "Inversion"
> RNGkind()
[1] "Mersenne-Twister" "Inversion"
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Apr 16, 2019 at 9:13 AM Elizabeth Purdom <epurdom using stat.berkeley.edu>
wrote:
> Hello,
>
> I have a package, and inside of it I have a small function that selects a
> random palette of colors for graphing purposes. It’s a large number of
> colors, which is why I don’t manually select them, but I did want them to
> stay constant so I set the seed before doing so. So I had a little function
> in my package that does this:
>
> .rcolors<-function(){
> set.seed(23589)
> x<-sample(colors()[-c(152:361)])
> return(x)
> }
> massivePalette<-unique(c(bigPalette,.rcolors()))
>
> Now that the sample function has been changed in R 3.6, I would need to
> use `sample.kind=“Rounding”` to get the same set of colors as I had
> previously. However, I don’t want to do that in my package, because that
> appears to change the global environment sampling:
>
> > RNGkind()
> [1] "Mersenne-Twister" "Inversion" "Rejection"
> > RNGkind(sample.kind="Rejection")
> > x<-clusterExperiment:::.rcolors() #now I have changed the function so
> that sample.kind=“Rounding” — I’ve suppressed the warnings
> > RNGkind()
> [1] "Mersenne-Twister" "Inversion" "Rounding”
>
> So I could do something like this:
>
> .rcolors<-function(){
> currentRNG<-RNGkind()
> suppressWarnings(RNGkind(sample.kind="Rounding"))
> set.seed(23589)
> x<-sample(colors()[-c(152:361)])
> #set it back to default
> suppressWarnings(RNGkind(sample.kind=currentRNG[3]))
> return(x)
> }
>
> But is there a way to change the random sampling in the function
> environment and not change it in the global environment? (For this
> function, I can just break down and accept that I will have different
> colors from this point on, but I’d like to know more generally; especially
> since it means that my `fixed` colors are not really fixed since they
> depend on the user’s setting of random sampling techniques, which I hadn’t
> considered before).
>
> All of the best,
> Elizabeth Purdom
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list