Skip to content

Commit

Permalink
First attempt at getting crosstalk to work.
Browse files Browse the repository at this point in the history
  • Loading branch information
kent37 committed Mar 20, 2017
1 parent 9cd3da8 commit b22cbd2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.Rproj.user
.Rhistory
.RData
inst/doc
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ Imports:
crosstalk,
htmltools,
htmlwidgets
Suggests: knitr,
rmarkdown
VignetteBuilder: knitr
6 changes: 6 additions & 0 deletions R/summarywidget.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ summarywidget <- function(data,
if (!is.logical(selection))
stop("Selection must contain TRUE/FALSE values.")
data = data[selection,]
key = key[selection]
}

# We just need one column, either the row.names or the specified column.
Expand Down Expand Up @@ -111,3 +112,8 @@ renderSummarywidget <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
htmlwidgets::shinyRenderWidget(expr, summarywidgetOutput, env, quoted = TRUE)
}

# Use a <span> container rather than the default <div>
summarywidget_html <- function(id, style, class, ...){
htmltools::tags$span(id = id, class = class)
}
45 changes: 41 additions & 4 deletions inst/htmlwidgets/summarywidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,62 @@ HTMLWidgets.widget({

factory: function(el, width, height) {

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

return {
renderValue: function(x) {

// Make a data object with keys
var data = {};
var i;
if (x.settings.crosstalk_key === null) {
for (i=0; i<x.data.length; i++) {
data[i] = x.data[i];
}
} else {
for (i=0; i<x.settings.crosstalk_key.length; i++) {
data[x.settings.crosstalk_key[i]] = x.data[i];
}
}

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 = x.data.length;
value = values.length;
break;
case 'sum':
value = x.data.reduce(function(acc, val) {return acc + val;}, 0);
value = values.reduce(function(acc, val) {return acc + val;}, 0);
break;
case 'mean':
value = x.data.reduce(function(acc, val) {return acc + val;}, 0) / x.data.length;
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;
};

var ct_sel = new crosstalk.SelectionHandle();
ct_sel.setGroup(x.settings.crosstalk_group);
ct_sel.on("change", function(e) {
if (e.value && e.value.length) {
update(filterKeys(data, e.value));
} else {
update(data);
}
});

update(data);
},

resize: function(width, height) {
Expand Down
30 changes: 30 additions & 0 deletions vignettes/Using summarywidget.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: "Using summarywidget"
author: "Kent S Johnson"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using summarywidget}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r}
library(crosstalk)
library(DT)
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),
filter_slider("hp", "Horsepower", shared_mtcars, ~hp, width = "100%")
),
datatable(shared_mtcars, width=600)
)
```


0 comments on commit b22cbd2

Please sign in to comment.