[R-pkg-devel] Assigning an object to the global environment (shiny package)

Iris Simmons |kw@|mmo @end|ng |rom gm@||@com
Thu Jan 4 22:00:17 CET 2024


You cannot and should not ignore this.

An R package should never ever be assigning variables into environments for
which they haven't been granted permission to do so. You should ask the
user whether they want the object assigned in the global environment before
doing so, and the default should be no so that it will not be assigned in
non interactive sessions.

On Thu, Jan 4, 2024, 15:54 Tiago Olivoto <tiagoolivoto using gmail.com> wrote:

> Hi everyone!
> I which a happy new year!
>
>
> I'm coding a shiny app and I would like to include an option so that the
> users can assign the results to the global environment for further
> analysis.
>
> I have written the following code, which checks if 'globalvarname' (the
> name of object to be created in the global environment) already exists,
> returning an error if so, and asking to the user change the name.
>
> code
> -----------
> observeEvent(input$savetoglobalenv, {
>  ### more code here
>
>   if (exists(input$globalvarname, envir = globalenv())) {
>     sendSweetAlert(
>       session = session,
>       title = "Error",
>       text = paste0("The object'", input$globalvarname, "' already exists
> in the global environment. Please, change the name."),
>       type = "success"
>     )
>   } else {
>     assign(input$globalvarname, report, envir = globalenv())
>     ask_confirmation(
>       inputId = "myconfirmation",
>       type = "warning",
>       title = "Close the App?",
>       text = paste0("The object'", input$globalvarname, "' has been created
> in the Global environment. To access the created object, you need first to
> stop the App. Do you really want to close the app now?"),
>       btn_labels = c("Nope", "Yep"),
>       btn_colors = c("#FE642E", "#04B404")
>     )
>   }
> })
> ---------
>
> Thus, the object is only created when the user decides to assign such an
> object to the global environment. As the object's name is checked, there is
> no way of replacing some object already available in the global
> environment.
>
> Of course, when running devtools::check(), a NOTE is returned
>
> Found the following assignments to the global environment:
>   Arquivo 'plimanshiny/R/mod_analyze.R':
>
> Can I ignore this safely?
> Is there any suggestion to handly this without using 'assign()'
>
> Thanks in advance,
> Olivoto
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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