[R-pkg-devel] Assignments to the global environment and use of on.exit
Siddhanta Phuyal
@|ddh@nt@phuy@|7159 @end|ng |rom gm@||@com
Tue Jun 22 10:47:56 CEST 2021
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]]
More information about the R-package-devel
mailing list