Sound
Sound

Reputation: 95

R Shiny: Deleting objects in shiny environment

I've created an App that displays a datatable created from values of some input widgets. I have implemented a feature that allows me to download the contents of the datatable as a CSV file by pressing a downloadButton.

I want to implement the option to reset/delete the contents of the datatable by the press of an actionbutton, in case I want to create a new datatable from scratch.

The contents of the datatable is stored as a dataframe object called datatable.

In a normal R session (when not running the app), I can easily delete/reset this object by manipulating the object in the global enviroment.

Both of these functions will accomplish what I need:

rm(datatable)

or

datatable <- datatable[0,] 

However, I don't want to have to run this from the console everytime I want to reset the datatable.

I can't figure out how to apply this to the actionbutton in the Shiny app. I think it is because the datatable object is being stored in multiple code brackets (local enviroments) in the App and not stored in a global environment (correct me if I'm wrong here).

My approach was the following:

  observeEvent(input$delete_button, {
    rm(datatable)
  })

I would simply get the error message:

Warning in rm(datatable) : object 'datatable' not found

My question is: Is it possible to remove an object from a "Shiny global enviroment"? and if so, how should I go about implementing an actionbutton that can accomplish this?

Upvotes: 0

Views: 714

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

If you set

dtbl <- reactiveValue(datatable = NULL)

observeEvent(something, {
    dtbl$datatable <- fread(...)
})

you can then access it in the following manner

observeEvent(input$delete_button, {
    dtbl$datatable <- NULL  # puff and it's gone
})

Upvotes: 2

Related Questions