Skip to content

Commit

Permalink
Implement default selection for filter_select
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
jcheng5 committed Jul 7, 2019
1 parent feaf86b commit ee8c64e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: crosstalk
Type: Package
Title: Inter-Widget Interactivity for HTML Widgets
Version: 1.0.1
Version: 1.0.1.9000
Authors@R: c(
person("Joe", "Cheng", role = c("aut", "cre"), email = "[email protected]"),
person(family = "RStudio", role = "cph"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## crosstalk 1.0.1.9000

* Add `selected` parameter to `filter_select`, to specify default selection.

## crosstalk 1.0.1

* `selection_factor` behavior was no longer correct with ggplot2 2.2.0, which
Expand Down
29 changes: 25 additions & 4 deletions R/controls.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ionrangesliderLibs <- function() {
)
}

makeGroupOptions <- function(sharedData, group, allLevels) {
makeGroupOptions <- function(sharedData, group, allLevels, selected = NULL) {
df <- sharedData$data(
withSelection = FALSE,
withFilter = FALSE,
Expand Down Expand Up @@ -89,10 +89,23 @@ makeGroupOptions <- function(sharedData, group, allLevels) {

lvls_str <- as.character(lvls)

if (!is.null(selected)) {
selected <- unique(as.character(selected))
present <- selected %in% lvls_str
if (any(!present)) {
warning(call. = FALSE,
"Default selection was specified that was not present in the data [",
paste0("'", selected[!present], "'", collapse = ","),
"]"
)
selected <- selected[present]
}
}
options <- list(
items = data.frame(value = lvls_str, label = lvls_str, stringsAsFactors = FALSE),
map = setNames(vals, lvls_str),
group = sharedData$groupName()
group = sharedData$groupName(),
selected = selected
)

options
Expand All @@ -114,6 +127,7 @@ makeGroupOptions <- function(sharedData, group, allLevels) {
#' present in the data?
#' @param multiple Can multiple values be selected?
#' @param columns Number of columns the options should be arranged into.
#' @param selected Default value(s) to be selected.
#'
#' @examples
#' ## Only run examples in interactive R sessions
Expand All @@ -126,9 +140,16 @@ makeGroupOptions <- function(sharedData, group, allLevels) {
#'
#' @export
filter_select <- function(id, label, sharedData, group, allLevels = FALSE,
multiple = TRUE) {
multiple = TRUE, selected = NULL) {

options <- makeGroupOptions(sharedData, group, allLevels)
if (!multiple && length(selected) > 1) {
warning(call. = FALSE, "filter_select called with multiple=FALSE ",
"but more than one selected value; only the first element will ",
"be used")
selected <- selected[[1]]
}

options <- makeGroupOptions(sharedData, group, allLevels, selected)

htmltools::browsable(attachDependencies(
tags$div(id = id, class = "form-group crosstalk-input-select crosstalk-input",
Expand Down
7 changes: 5 additions & 2 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.

7 changes: 5 additions & 2 deletions javascript/src/input_selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ input.register({
let items = util.dataframeToD3(data.items);
let opts = {
options: first.concat(items),
items: data.selected,
valueField: "value",
labelField: "label",
searchField: "label"
Expand All @@ -30,7 +31,7 @@ input.register({
let ctHandle = new FilterHandle(data.group);

let lastKnownKeys;
selectize.on("change", function() {
function updateFilter() {
if (selectize.items.length === 0) {
lastKnownKeys = null;
ctHandle.clear();
Expand All @@ -46,7 +47,9 @@ input.register({
lastKnownKeys = keyArray;
ctHandle.set(keyArray);
}
});
}
selectize.on("change", updateFilter);
updateFilter();

return {
suspend: function() {
Expand Down

0 comments on commit ee8c64e

Please sign in to comment.