Skip to content

Commit

Permalink
Crosstalk works with DT::datatable
Browse files Browse the repository at this point in the history
Doesn't work with filter_checkbox.
  • Loading branch information
kent37 committed Mar 21, 2017
1 parent b22cbd2 commit 8d503f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export(renderSummarywidget)
export(summarywidget)
export(summarywidgetOutput)
import(crosstalk)
import(htmlwidgets)
1 change: 1 addition & 0 deletions R/summarywidget.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ summarywidget <- function(data,
stop("Unexpected two-sided formula: ", deparse(selection))
selection = eval(selection[[2]], data, environment(selection))
}

if (!is.logical(selection))
stop("Selection must contain TRUE/FALSE values.")
data = data[selection,]
Expand Down
39 changes: 20 additions & 19 deletions inst/htmlwidgets/summarywidget.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
HTMLWidgets.widget({

name: 'summarywidget',

type: 'output',

factory: function(el, width, height) {

filterKeys = function(obj, keys) {
var filterKeys = function(obj, keys) {
var result = {};
keys.forEach(function(k) { result[k]=obj[k];});
return result;
};

return {
renderValue: function(x) {

// Make a data object with keys
// Make a data object with keys so we can easily update the selection
var data = {};
var i;
if (x.settings.crosstalk_key === null) {
Expand All @@ -27,30 +26,32 @@ HTMLWidgets.widget({
}
}

update = function(d) {
// Update the display to show the values in d
var update = function(d) {
// Get a simple vector. Don't use Object.values(), RStudio doesn't seem to support it.
var values = [];
for (var key in d) {
if (d.hasOwnProperty(key)) { values.push(d[key]);}
}

var value = 0;
switch (x.settings.statistic) {
case 'count':
value = values.length;
break;
case 'sum':
value = values.reduce(function(acc, val) {return acc + val;}, 0);
break;
case 'mean':
value = values.reduce(function(acc, val) {return acc + val;}, 0) / values.length;
break;
}
var value = 0;
switch (x.settings.statistic) {
case 'count':
value = values.length;
break;
case 'sum':
value = values.reduce(function(acc, val) {return acc + val;}, 0);
break;
case 'mean':
value = values.reduce(function(acc, val) {return acc + val;}, 0) / values.length;
break;
}

if (x.settings.digits !== null) value = value.toFixed(x.settings.digits);
el.innerText = value;
if (x.settings.digits !== null) value = value.toFixed(x.settings.digits);
el.innerText = value;
};

// Set up to receive crosstalk events
var ct_sel = new crosstalk.SelectionHandle();
ct_sel.setGroup(x.settings.crosstalk_group);
ct_sel.on("change", function(e) {
Expand Down
16 changes: 10 additions & 6 deletions man/summarywidget.Rd

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

23 changes: 23 additions & 0 deletions vignettes/Minimal example.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "Minimal Example"
author: "Kent S Johnson"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Minimal Example}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

This is a minimal example of using a `summarywidget` _without_ `crosstalk`. This is mostly useful as a test of the `htmlwidget` character of `summarywidget`.

```{r}
library(summarywidget)
```

The `mtcars` dataset has `r summarywidget(mtcars)` rows (32).

The mean mpg of four-cylinder cars is
`r summarywidget(mtcars, 'mean', selection=~cyl==4, column='mpg', digits=2)` mpg (26.66).

`r summarywidget(mtcars, 'sum', column='am')` cars in the dataset have manual transmissions (13).
8 changes: 5 additions & 3 deletions vignettes/Using summarywidget.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
title: "Using summarywidget"
author: "Kent S Johnson"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
output:
rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using summarywidget}
%\VignetteEngine{knitr::rmarkdown}
Expand All @@ -16,8 +17,6 @@ library(summarywidget)
shared_mtcars = SharedData$new(mtcars[,c("mpg", "cyl", "hp", "am")])
summarywidget(shared_mtcars)
bscols(widths = c(3,NA),
list(
filter_checkbox("cyl", "Cylinders", shared_mtcars, ~cyl, inline = TRUE),
Expand All @@ -27,4 +26,7 @@ bscols(widths = c(3,NA),
)
```

### Summary

You have selected `r summarywidget(shared_mtcars)` cars with an average mpg of
`r summarywidget(shared_mtcars, 'mean', column='mpg', digits=1)`.

0 comments on commit 8d503f9

Please sign in to comment.