Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stateSave overides option parameters #146

Closed
vnijs opened this issue Jul 21, 2015 · 8 comments
Closed

stateSave overides option parameters #146

vnijs opened this issue Jul 21, 2015 · 8 comments

Comments

@vnijs
Copy link
Contributor

vnijs commented Jul 21, 2015

This is a follow up on #66, #76, and #77. I setup a repo with a minimal example to see if I can save and reset state for a DT table (see https://github.com/vnijs/dt_state). Column and global search work fine without stateSave = TRUE. The remaining parts (sorting, page, show) require stateSave = TRUE.

My goals are to (1) maintain state, (2) restore state from a file, and (3) 'flush' state when needed.

  1. Works seems to work well. It might be convenient to have an option state that can take an object with the same structure as input$outputId_state and parse it.
  2. Seems to work only when starting up the app for the first time. However, if you refresh the browser stateSave stores its values and over-rides the parameters passed through options. To simulate what I'm looking for see line 15 in server.R. I would expect this value to be used on each refresh because it is passed to the searchCols option.
  3. I haven't found a way to do this other than to close the browser. The refresh button/link doesn't work because stateSave overrides it.

The current stateSave setup probably works really well for client-side processing. However, for server-side processing it would be great if there was an option state that can take an object with the same structure as input$outputId_state and parse it. In my example I would assign this state to the r_state list that I can easily flush or I can load a new r_state from file and use that.

@vnijs
Copy link
Contributor Author

vnijs commented Aug 1, 2015

It seems that datatables has a state.clear() option that could be really useful to reset the state of a DT table when needed (see point 3 in the earlier issue post).

I tried to use this feature in a minimal example (see https://github.com/vnijs/dt_state/blob/master/ui.R) but get the following error.

(index):33 Uncaught TypeError: Cannot read property 'clear' of undefined

@yihui
Copy link
Member

yihui commented Aug 14, 2015

I can only answer the state.clear() issue for now: dataviewer is not the DataTables instance; it is only an output id, so you cannot call the clear method. The correct way to do it is to use the callback argument of datatable() to access the DataTables instance table, then attach a click event on the link <a>, like this (not tested):

datatable(..., callback = JS("
$('a#refresh').on('click', function() {
  table.state.clear();
  window.location.reload();
});
"))

@vnijs
Copy link
Contributor Author

vnijs commented Aug 23, 2015

Finally got around to trying your suggestion. Works great with one table (see https://github.com/vnijs/dt_state)!! Thanks.

However, is it possible that conflicts occur when you have multiple DT tables in an app you want to use stateSave for? When I use this in Radiant with Data > View it works great but strange things happen when I try to add the same functionality to Data > Pivot. If this should work I'll try to trace back what is going on.

@yihui
Copy link
Member

yihui commented Aug 25, 2015

I'm relatively sure that table.state.clear() should not generate conflicts since the callbacks are specific to each table. One callback in one table should not affect another table.

@vnijs
Copy link
Contributor Author

vnijs commented Aug 26, 2015

@yihui I'm not sure exactly what the cause is but there is an issue that pops-up when I use stateSave. I created a small app that illustrates the problem (see https://github.com/vnijs/dt_state).

If I turn a filter in the View or Pivot tab in the dt_state app (apply filter checkbox) on and back off again the DT filter options are maintained as you would expect thanks to stateSave. However, for factor variables the 'clear' button disappears and the selected levels do not show up anymore when you click in the input box. You first have to select a level again and then remove it to see all the data. After doing that the clear button shows up again but it is no-longer needed. For numeric variables the clear button also disappears but you can still remove the filter manually. See screenshots (from Radiant) below.

screen shot 2015-08-25 at 4 53 33 pm
screen shot 2015-08-25 at 4 53 44 pm
screen shot 2015-08-25 at 4 54 01 pm

@vnijs
Copy link
Contributor Author

vnijs commented Aug 27, 2015

The best solution I found to maintain state (stateSave = TRUE) and have full control of initial values for filters, ordering, and search is to use:

callback = DT::JS("$(window).unload(function() { table.state.clear(); })")

As the main issue is resolved I will close this one and open a new issue about removing filters for factors when using stateSave.

@jcheng5
Copy link
Member

jcheng5 commented Sep 23, 2019

As of Shiny v1.4, $(window).unload won't be available, you'll have to write this instead:

callback = DT::JS("$(window).on('unload', function() { table.state.clear(); })")

@vnijs
Copy link
Contributor Author

vnijs commented Sep 24, 2019

Thanks for the heads-up @jcheng5! Works as expected.

callback = DT::JS('$(window).on("unload", function() { table.state.clear(); })')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants