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

Modification to allow more renderers & user choice in renderer #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions R/rpivotTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' the \strong{columns} of the pivot table.
#' @param aggregatorName String name of the pivottable.js aggregator to prepopulate the pivot table.
#' @param vals String name of the column in the data.frame to use with \code{aggregatorName}. Must be additive (i.e a number).
#' @param rendererLibraries A vector containing one or more of "core", "D3", "C3", "plotly", "gchart", "export", "subtotals"
#' @param rendererName List name of the renderer selected, e.g. Table, Heatmap, Treemap etc.
#' @param sorter String name this allows to implement a javascript function to specify the ad hoc sorting of certain values. See vignette for an example.
#' It is especially useful with time divisions like days of the week or months of the year (where the alphabetical order does not work).
Expand All @@ -35,7 +36,7 @@
#' \item{tr}
#' \item{zh}
#' }
#' @param subtotals Logical this optional parameter allows use of the pivottable \href{https://github.com/nagarajanchinnasamy/pivottable-subtotal-renderer}{subtotal plugin}. Using this parameter will set all renderer names to the english locale.
#' @param subtotals [DEPRICATED] Logical this optional parameter allows use of the pivottable \href{https://github.com/nagarajanchinnasamy/pivottable-subtotal-renderer}{subtotal plugin}. Using this parameter will set all renderer names to the english locale.
#' @param width width parameter
#' @param height height parameter
#' @param elementId String valid CSS selector id for the rpivotTable container.
Expand Down Expand Up @@ -98,6 +99,7 @@ rpivotTable <- function(
cols = NULL,
aggregatorName = NULL,
vals = NULL,
rendererLibraries = c("core", "D3", "C3"),
rendererName = NULL,
sorter = NULL,
exclusions = NULL,
Expand All @@ -114,6 +116,13 @@ rpivotTable <- function(
stop( "data should be a data.frame, data.table, or table", call.=F)
}

if( sum(rendererLibraries %in% c("core", "D3", "C3", "plotly", "gchart", "export", "subtotals")) != length(rendererLibraries)) {
stop( "rendererLibraries should be one or more of 'core', 'D3', 'C3', 'plotly', 'gchart', 'export', 'subtotals'", call.=F)
}
if (subtotals) {
rendererLibraries <- c(rendererLibraries, "subtotals")
}

#convert table to data.frame
if( length(intersect(c("table","structable", "ftable"), class(data))) > 0 ) data <- as.data.frame( data )

Expand Down Expand Up @@ -151,7 +160,7 @@ rpivotTable <- function(
data = data,
params = params,
locale = locale,
subtotals = subtotals
rendererLibraries = rendererLibraries
)

htmlwidgets::createWidget(
Expand Down
2 changes: 2 additions & 0 deletions inst/htmlwidgets/lib/pivottable/plotly_renderers.min.js

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

7 changes: 7 additions & 0 deletions inst/htmlwidgets/lib/plotly/plotly.min.js

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions inst/htmlwidgets/rpivotTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,40 @@ HTMLWidgets.widget({
}

var locale = $.pivotUtilities.locales[x.locale];
locale.renderers = $.extend({}, locale.renderers,
locale.d3_renderers || $.pivotUtilities.d3_renderers,
locale.c3_renderers || $.pivotUtilities.c3_renderers);

// if subtotals then override renderers to add subtotals
if(x.subtotals) {
x.params.renderers = $.extend(
$.pivotUtilities.subtotal_renderers,
$.pivotUtilities.d3_renderers,
$.pivotUtilities.c3_renderers
);


if (!x.rendererLibraries.includes("core")) {
locale.renderers = {};
}
if (x.rendererLibraries.includes("subtotals")) {
locale.renderers = $.extend(locale.renderers, locale.subtotal_renderers || $.pivotUtilities.subtotal_renderers);
x.params.dataClass = $.pivotUtilities.SubtotalPivotData;
}
if (x.rendererLibraries.includes("D3")) {
locale.renderers = $.extend(locale.renderers, locale.d3_renderers || $.pivotUtilities.d3_renderers);
}
if (x.rendererLibraries.includes("C3")) {
locale.renderers = $.extend(locale.renderers, locale.c3_renderers || $.pivotUtilities.c3_renderers);
}
if (x.rendererLibraries.includes("plotly")) {
locale.renderers = $.extend(locale.renderers, locale.plotly_renderers || $.pivotUtilities.plotly_renderers);
}
if (x.rendererLibraries.includes("gchart")) {
// In theory this should work, however for some reason the google.load request misbehaves
/* $.ajax({
* async: false,
* dataType: "script",
* url: "https://www.google.com/jsapi"
* });
* google.load("visualization", "1", {packages:["corechart", "charteditor"]});
*/

locale.renderers = $.extend(locale.renderers, locale.gchart_renderers || $.pivotUtilities.gchart_renderers);
}
if (x.rendererLibraries.includes("export")) {
locale.renderers = $.extend(locale.renderers, locale.export_renderers || $.pivotUtilities.export_renderers);
}


$('#'+el.id).pivotUI(x.data, x.params, true, x.locale);
}
Expand Down
7 changes: 7 additions & 0 deletions inst/htmlwidgets/rpivotTable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ dependencies:
version: 3.5.5
src: htmlwidgets/lib/d3
script: d3.v3.min.js
- name: plotly
version: 1.42.5
src: htmlwidgets/lib/plotly
script: plotly.min.js
- name: pivottable
version: 2.19.0
src: htmlwidgets/lib/pivottable
Expand All @@ -27,6 +31,9 @@ dependencies:
- pivot.min.js
- d3_renderers.min.js
- c3_renderers.min.js
- plotly_renderers.min.js
- gchart_renderers.min.js
- export_renderers.min.js
- pivot.cs.min.js
- pivot.da.min.js
- pivot.de.min.js
Expand Down
4 changes: 3 additions & 1 deletion man/rpivotTable.Rd

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

14 changes: 12 additions & 2 deletions tests/testthat/test_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ test_that("parameters handled correctly",{
rpivotTable(data.frame(),vals="aval")$x$params$vals[[1]]
,"aval"
)

expect_identical(
rpivotTable(data.frame(),rendererLibraries=c("core"),subtotals = TRUE)$x$rendererLibraries
,c("core","subtotals")
)

expect_match(
rpivotTable(data.frame(),rendererLibraries=c("core"))$x$rendererLibraries[[1]]
,"core"
)
expect_identical(
rpivotTable(data.frame(),subtotals = TRUE)$x$subtotals
,TRUE
rpivotTable(data.frame(),rendererLibraries=c("core", "plotly", "export"))$x$rendererLibraries
,c("core", "plotly", "export")
)
})