Skip to content

Commit

Permalink
chore: simplify slider states logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhav committed Oct 10, 2024
1 parent 6468f33 commit 754ac20
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions R/toggleable_slider.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ toggle_slider_server <- function(id, is_dichotomous_slider = TRUE, step_slider =
}
)

update_widgets <- function() {
slider_states <- reactive({
state_slider <- cur_state()
req(length(state_slider) > 0) # update will otherwise not work
state_low <- state_slider
Expand All @@ -212,12 +212,29 @@ toggle_slider_server <- function(id, is_dichotomous_slider = TRUE, step_slider =
state_low$value <- state_low$value[[1]]
state_high$value <- state_high$value[[2]]
}
if (input$toggle %% 2 == 0) {
state_slider$max <- max(state_slider$max, state_slider$value[2])
state_slider$min <- min(state_slider$min, state_slider$value[1])
}
list(
low = state_low,
high = state_high,
low_value = state_low$value,
high_value = state_high$value,
slider_value = state_slider$value,
slider_max = state_slider$max,
slider_min = state_slider$min
)
})

update_widgets <- function() {
state <- slider_states()
if (input$toggle %% 2 != 0) {
if (length(state_slider$value) > 1) {
do.call(updateNumericInput, c(list(session, "value_low"), state_low))
do.call(updateNumericInput, c(list(session, "value_high"), state_high))
if (length(state$slider_value) > 1) {
do.call(updateNumericInput, c(list(session, "value_low"), state$low))
do.call(updateNumericInput, c(list(session, "value_high"), state$high))
} else {
do.call(updateNumericInput, c(list(session, "value"), state_low))
do.call(updateNumericInput, c(list(session, "value"), state$low))
}
}
}
Expand All @@ -228,45 +245,35 @@ toggle_slider_server <- function(id, is_dichotomous_slider = TRUE, step_slider =
})

output$slider_ui <- renderUI({
state_slider <- cur_state()
req(length(state_slider) > 0)
state_low <- state_slider
state_high <- state_slider
if (!is.null(state_slider$value) && (length(state_slider$value) > 1)) {
state_low$value <- state_low$value[[1]]
state_high$value <- state_high$value[[2]]
}
if (input$toggle %% 2 == 0) {
state_slider$max <- max(state_slider$max, state_slider$value[2])
state_slider$min <- min(state_slider$min, state_slider$value[1])
}
if (length(seq(state_slider$min, state_slider$max)) < 10) {
ticks <- seq(state_slider$min, state_slider$max)
state <- slider_states()
if (length(seq(state$slider_min, state$slider_max)) < 10) {
# The values should be index reference instead of actual values because of how we are calling the `sliderInput`
ticks <- seq(state$slider_min, state$slider_max)
values <- c(
which(ticks == state_low$value) - 1,
which(ticks == state_high$value) - 1
which(ticks == state$low_value) - 1,
which(ticks == state$high_value) - 1
)
args <- list(
inputId = "slider",
label = NULL,
min = state_slider$min,
max = state_slider$max,
min = state$slider_min,
max = state$slider_max,
value = values,
ticks = ticks,
step = step_slider,
...
)
ticks <- paste0(args$ticks, collapse = ",")
args$ticks <- TRUE
html <- do.call("sliderInput", args)
html <- suppressWarnings(do.call("sliderInput", args))
html$children[[2]]$attribs[["data-values"]] <- ticks
} else {
args <- list(
inputId = "slider",
label = NULL,
min = state_slider$min,
max = state_slider$max,
value = c(state_slider$min, state_slider$max),
min = state$slider_min,
max = state$slider_max,
value = c(state$slider_min, state$slider_max),
step = step_slider,
...
)
Expand Down

0 comments on commit 754ac20

Please sign in to comment.