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

Refactor slider state management #322

Conversation

vedhav
Copy link
Contributor

@vedhav vedhav commented Oct 21, 2024

Closes #321

Changes:

  1. The toggle_slider_ui and toggle_slider_server can only be used to create dichotomous slider now. There was no instance of single value slider created. So, there is no need to create it and increase the complexity.
  2. Removal of the keep_range_slider_updated in favor of keep_slider_state_updated to keep the states updated based on other widget inputs.
  3. Updated the modules that uses this widget. Note that the tm_g_gh_lineplot does not use the keep_slider_state_updated and directly updates the state reactiveValues.

Check all the modules that use the toggle_slider module:

  • tm_g_gh_boxplot
  • tm_g_gh_correlationplot
  • tm_g_gh_density_distribution_plot
  • tm_g_gh_lineplot
  • tm_g_gh_spaghettiplot
  • tm_g_gh_scatterplot (deprecate in favor of tm_g_gh_correlationplot)

@vedhav vedhav changed the base branch from 133-improve-slider-ticks@main to main October 21, 2024 12:35
@vedhav vedhav changed the base branch from main to 133-improve-slider-ticks@main October 21, 2024 12:35
@vedhav vedhav added bug Something isn't working core and removed bug Something isn't working labels Oct 21, 2024
R/toggleable_slider.R Outdated Show resolved Hide resolved
Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three (or even four) states maintained in the same moment is are not needed.

  • selected_state is actual selection from any (slider and numeric inputs)
  • changes in slider update numeric_state
  • changes in numeric update slider_state

Given above, each state needs to have same values which can be simplified by the single state shared by slider, numeric and also returned to "keep_slider_updated".

@vedhav
Copy link
Contributor Author

vedhav commented Oct 22, 2024

Thank you @gogonzo I was able to remove an unwanted reactiveVal numeric_update_state. However, the other three variables initial_state, selected_state, and selected_state are needed.

Here are my comments related to the state variables:

  1. The min, max, value, step can be changed by other widgets. When this is done, both the slider and numericInput should update their values. Hence the initial_state is needed.
    2 When the slider range is set by the numericInput, it can be done in two ways:
    i. The new range is bigger than the old range (old range was [0,10] and new range is [0,20])
    When this happens we have to increase the selected value of the slider & set the new ranges.
    ii. The new range falls between the older ranges (old range was [0.10] and new range is [4,8]
    When this happens we have to just set the new value of the sliders and the old ranges remain the same.
    So, it is important that we store the initial_state and changing the min and max from this variable will prevent us from achieving the slider state setting for the above user actions.
  2. selected_state is the main state we maintain to return, which the module will use.
  3. If we consider using a single state variable to manage the slider we create cyclic reactive dependency, because we have to render the slider when the state is updated. So, we have a variable slider_update_state which is set when initial state is changed or when the state is changed using the numericInput.

I think some comments can help here. I'll try to come up with better naming to make it easier to understand.

@gogonzo
Copy link
Contributor

gogonzo commented Oct 23, 2024

However, the other three variables initial_state, selected_state, and selected_state are needed.

All above can be simplified to:

  • data_range (so called initial)
  • range (current lower/upper boundary for slider)
  • value (current selection)
  • step

I think change counter is not even needed because technically keep_slider_state_updated can directly change above. SO the code can look like this

state <- list(data_min = reactiveVal(), ...) # list of reactiveVal to not trigger all observers

observeEvent(state$data_range(), {
  # update range if necessary
  # update values if outside the new range
})

observeEvent(state$value(), input$toggle, {  
  # update numeric if visible and if values differ from input$numeric
})

observeEvent(state$value(), state$range(), input$toggle, {  
  # update slider if visible and if values differ from input$slider
})

observeEvent(input$slider, {
  # update state$value
})

observeEvent(input$numeric, {
  # update state$value
  # update state$range if they went outside of the data range
})

return(state)

I'm pretty sure that selected_state and slider_state in the same time are not needed. I think a single state object, so that

numericInput and sliderInput are changing state and are changed when state changes.

@averissimo averissimo assigned gogonzo and unassigned averissimo Oct 23, 2024
@vedhav
Copy link
Contributor Author

vedhav commented Oct 24, 2024

Thank you, let me try to implement this.
The change counter was needed to reset the sliders to the default state when there was no change in min/max/value/step made by the other widget changes but we needed to reset the slider states (this will fail when user changed the slider ranges and other widget does not reset this).

R/toggleable_slider.R Outdated Show resolved Hide resolved
@vedhav vedhav requested a review from gogonzo October 25, 2024 09:00
R/toggleable_slider.R Outdated Show resolved Hide resolved
R/tm_g_gh_spaghettiplot.R Outdated Show resolved Hide resolved
R/toggleable_slider.R Outdated Show resolved Hide resolved
@vedhav vedhav requested a review from gogonzo October 28, 2024 13:51
Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful. Issue fixed, test covered, -300 lines net in the R/ directory 💪

@vedhav vedhav merged commit 665019c into 133-improve-slider-ticks@main Oct 28, 2024
1 check passed
@vedhav vedhav deleted the 321-refactor-sliders@133-improve-slider-ticks@main branch October 28, 2024 14:06
@github-actions github-actions bot locked and limited conversation to collaborators Oct 28, 2024
@m7pr
Copy link
Contributor

m7pr commented Oct 28, 2024

Wooop wooop wooop!!!

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

Successfully merging this pull request may close these issues.

4 participants