Skip to content
Advertisement

How to use the localStorage option for DT in R Shiny?

I want to design a shiny app that allow the users to save their inputs in the local storage, which means when the users reopen the tool with their web browsers, the tool reload the values last time the users provide. This is mainly achieved by the shinyStore package.

Below is an example. So far I can use the shinyStore to restore any shiny input widget, such as textInput. However, I now want to also restore the edited values in a datatable from the DT package.

I know the information of the edited values are in the input$DT_out_cell_edit, but it is not a single value, so the updateStore function would not work. I thought about using dataTableProxy and replaceData from the DT package, but they cannot keep values from the last time when the app runs. Finally, I tried to set stateSave = TRUE as in this example, but it cannot document the edited values.

If possible, please let me know if you have any ideas. If it is not possible, please also let me know.

JavaScript

Advertisement

Answer

Please check the following:

Im using a reactiveValue uiTable to track the changes made to the datatable. Once the save button is clicked updateStore is used to save the data.frame.

When a new session starts input$store$uiTable is monitored for changes. If the table was changed it is updated via replaceData.

For now this doesn’t work for the rownames of a data.frame, as it needs some extra code, which in my eyes isn’t necessary to illustrate the principle.


Edit: I added the mtcars rownames as a column via data.table and disabled editing for the DT rownames to provide a more intuitive example for future readers.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement