[R-pkg-devel] Assignments to the global environment and use of on.exit
Oliver Madsen
o||ver@p@m@d@en @end|ng |rom gm@||@com
Tue Jun 22 18:14:52 CEST 2021
The problem is solved by either
1) creating an environment variable within the package namespace (as you
would a function) using new.env(). This can then be used instead of the
global environment.
2) assigning variables to the namespace of the function running the shiny
app.
1) is the second answer in the link provided by Jeff.
On Tue, 22 Jun 2021, 18:05 Jeff Newmiller, <jdnewmil using dcn.davis.ca.us> wrote:
> Just don't. E.g.
> https://stackoverflow.com/questions/12598242/global-variables-in-packages-in-r
>
> On June 22, 2021 1:47:56 AM PDT, Siddhanta Phuyal <
> siddhantaphuyal7159 using gmail.com> wrote:
> > Hello,
> >
> >A few weeks ago, I submitted a package to CRAN. The automated system
> >rejected the package showing the following note:
> >
> >Found the following assignments to the global environment:
> > File 'EuclideanSD/R/EuclideanSD.R':
> > assign("nums", x, envir = globalenv())
> >
> >Context of the problem:
> >
> >The package has a function that takes a vector from the user. The
> >vector is
> >used by the shiny app to produce the desired output. The sever and UI
> >functions are located inside the 'inst' folder. Since they are located
> >at a
> >different location, the server function cannot directly access the
> >vector
> >received by the function from the user. Hence, the package creates a
> >global
> >variable to store the data which can also be used by the server
> >function.
> >
> >The solution I offered:
> >
> >However, before assigning the variable to a global environment, the
> >function checks whether there is a variable with the same name in the
> >global environment. If there is such a variable, then the function
> >restores
> >that variable back to its original state by using on.exit(num<-oldnum)
> >prior to exiting the function. On the contrary, if such a variable does
> >not
> >exist, then the variable assigned to the global environment is removed
> >on
> >exit. I have included the code to clarify my argument. Please see below
> >for
> >the code:
> >
> >if (exists("nums",where = 1)){
> > oldnums <- nums
> > nums <- x
> > shiny::runApp(system.file(package="EuclideanSD","app"))
> > on.exit(nums<-oldnums)
> >
> > }else{
> > assign("nums",x,envir = globalenv())
> > shiny::runApp(system.file(package="EuclideanSD","app"))
> > on.exit(remove("nums",pos=1))
> > }
> >
> >I tried to explain the same to the maintainers of CRAN by replying to
> >the
> >message that I got from the CRAN. However, there was no response.
> >
> >I have two questions that I hope to get help from this community:
> >
> >1) Is my solution reasonably okay with the requirements of the CRAN?
> >2) If the solution is not okay, what could be the better solution?
> >However,
> >the features of the package cannot be changed. For example, the data
> >has to
> >be fed through the function and not through the website.
> >
> >Thank you for your help.
> >
> >Looking forward to hearing from you.
> >
> >Best Regards,
> >Siddhanta Phuyal
> >
> > [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-package-devel using r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
[[alternative HTML version deleted]]
More information about the R-package-devel
mailing list