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

Josiah Parry jo@|@h@p@rry @end|ng |rom gm@||@com
Thu Jan 4 22:11:07 CET 2024


Alternatively, you can create a new environment in your package and assign
into it and fetch out of it. This is definitely safer and probably "more
idiomatic." Hope that helps!


# create a new environment
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-2>my_env
<- rlang::new_environment()
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-3>
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-4>#
assign a value to the environment
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-5>rlang::env_bind(my_env,
var_name = "my value")
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-6>
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-7>#
fetch the value from the environment
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-8>my_env$var_name
<http://localhost:11786/session/agile-pike_reprex_preview.html?viewer_pane=1&capabilities=1&host=http%3A%2F%2F127.0.0.1%3A35355#cb1-9>#>
[1] "my value"


On Thu, Jan 4, 2024 at 4:04 PM Iris Simmons <ikwsimmo using gmail.com> wrote:

> 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]]
>
> ______________________________________________
> 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