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

Implement default selections #70

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## crosstalk 1.1.1.9000

* Add `selected` parameter for to specifying default selections in `filter_select()` and `filter_checkbox()`.
* Add `selected` parameter for to specifying default selections in `filter_select()`, `filter_checkbox()`, and `filter_slider()`.

## crosstalk 1.1.1

Expand Down
16 changes: 13 additions & 3 deletions R/controls.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ inlineCheckbox <- function(id, value, label, checked) {
filter_slider <- function(id, label, sharedData, column, step = NULL,
round = FALSE, ticks = TRUE, animate = FALSE, width = NULL, sep = ",",
pre = NULL, post = NULL, timeFormat = NULL,
timezone = NULL, dragRange = TRUE, min = NULL, max = NULL)
timezone = NULL, dragRange = TRUE, min = NULL, max = NULL, selected = NULL)
{
# TODO: Check that this works well with factors
# TODO: Handle empty data frame, NA/NaN/Inf/-Inf values
Expand All @@ -320,6 +320,16 @@ filter_slider <- function(id, label, sharedData, column, step = NULL,
max <- max(values)
value <- range(values)

if (!is.null(selected)) {
if (!is.numeric(selected) || length(selected) != 2) {
cpsievert marked this conversation as resolved.
Show resolved Hide resolved
stop("selected must be a numeric vector of length 2")
}
selected <- sort(selected)
if (min(selected) < min || max < max(selected)) {
stop("selected range must be within min/max range")
}
}

ord <- order(col)
options <- list(
values = col[ord],
Expand Down Expand Up @@ -403,8 +413,8 @@ filter_slider <- function(id, label, sharedData, column, step = NULL,
`data-type` = if (length(value) > 1) "double",
`data-min` = formatNoSci(min),
`data-max` = formatNoSci(max),
`data-from` = formatNoSci(value[1]),
`data-to` = if (length(value) > 1) formatNoSci(value[2]),
`data-from` = selected[1] %||% formatNoSci(value[1]),
`data-to` = selected[2] %||% if (length(value) > 1) formatNoSci(value[2]),
`data-step` = formatNoSci(step),
`data-grid` = ticks,
`data-grid-num` = n_ticks,
Expand Down
3 changes: 3 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"%||%" <- function(x, y) {
if (is.null(x)) y else x
}
35 changes: 16 additions & 19 deletions inst/www/js/crosstalk.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions inst/www/js/crosstalk.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/www/js/crosstalk.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/www/js/crosstalk.min.js.map

Large diffs are not rendered by default.

38 changes: 17 additions & 21 deletions javascript/src/input_slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ input.register({

let lastKnownKeys = null;

$el.on("change.crosstalkSliderInput", function(event) {
function updateFilter() {
if (!$el.data("updating") && !$el.data("animating")) {
let [from, to] = getValue();
let keys = [];
Expand All @@ -90,26 +90,22 @@ input.register({
ctHandle.set(keys);
lastKnownKeys = keys;
}
});


// let $el = $(el);
// $el.on("change", "input[type="checkbox"]", function() {
// let checked = $el.find("input[type="checkbox"]:checked");
// if (checked.length === 0) {
// ctHandle.clear();
// } else {
// let keys = {};
// checked.each(function() {
// data.map[this.value].forEach(function(key) {
// keys[key] = true;
// });
// });
// let keyArray = Object.keys(keys);
// keyArray.sort();
// ctHandle.set(keyArray);
// }
// });
}
$el.on("change.crosstalkSliderInput", updateFilter);

updateFilter();
cpsievert marked this conversation as resolved.
Show resolved Hide resolved

// Update filter now in case this code happens to execute
// after widget(s) are done rendering
updateFilter();

// Schedule another update when all widgets are done rendering
// This is especially relevant for `runtime: shiny` where widgets
// likely haven't rendered at this point and may only register
// FilterHandle.on("change", ...) callbacks in their renderValue
if (window.HTMLWidgets) {
window.HTMLWidgets.addPostRenderHandler(updateFilter);
}
cpsievert marked this conversation as resolved.
Show resolved Hide resolved

return {
suspend: function() {
Expand Down