The following code I found on an other post (I could link if anybody is interested) adds the saving option on the R rpivottable using flexdashboard. The saving is done through cookies.
I am trying to recreate it but having multiple saving options, basically have SAVE1, SAVE2,.. and being able to load the wanted save. I am unexperienced with JS so it is really hard to do anything.
---
title: "rpivottable_test"
output: html_document
---
knitr::opts_chunk$set(echo = FALSE)
# devtools::install_github("fraupflaume/rpivotTable")
library(rpivotTable)
data(mtcars)
names(mtcars)[10] <- "George.Dontas"
Save Current Configuration Restore Previous Configuration
rpivotTable(mtcars,rows="George.Dontas", cols = c("cyl"), width = "90%", height = "40%",
rendererOptions = list(
c3 = list(legend = list(show = FALSE),
data = list(labels = TRUE),
options = list(responsive = TRUE,
maintainAspectRatio = FALSE),
size = list(width = "600",
height = "500")),
d3 = list(size = list(width = "500", height = "500"))))
// save current state of the tables to my browser
setTimeout(function(){ //add the events first
document.querySelector('a#saveBtn').addEventListener('click', savoring);
document.querySelector('a#restoBtn').addEventListener('click', giveItBack);
function savoring() { // function to save
el = document.querySelectorAll('.rpivotTable');
for(i=0; i < el.length; i++){
elId = el[i].getAttribute("id");
stringy = $('#' + elId).data("pivotUIOptions"); // collect rows/columns filters
delete stringy['aggregators']; // remove the arbitrary
delete stringy['renderers'];
stringy2 = JSON.stringify(stringy); // make it one key:value
window.localStorage.setItem('table' + i, stringy2); // store it!
}
};
function giveItBack() { // function to regurgitate
el = document.querySelectorAll('.rpivotTable');
console.log("working on the giver");
ods = [...el[0].ownerDocument.scripts]; // make it an array
for(j=0; j < el.length; j++){
elId = el[j].getAttribute("id");
where = ods.filter(function(ods){ // filter scripts for table data
return ods.dataset['for'] === elId;
})[0].innerHTML;
where2 = JSON.parse(where).x.data; // WOOO HOO! I figured it out!!
where3 = HTMLWidgets.dataframeToD3(where2); // finally sheesh!!
gimme = window.localStorage.getItem('table' + j); // get storage
$('#' + elId).pivotUI(where3, JSON.parse(gimme), true, "en"); // put it back!
}
}
},100);
This uses the default system alert and prompt boxes. These vary in appearance a bit from one OS to another.
I've included CSS for the buttons and RMD. I used
html_document. If you useflex_dashboardyou won't need the CSS for the classmain-container. The CSS forbodymay or may not be useful (depending on what else you have going on).If you're using the Cran package, the
rendererOptionswon't work as-is. You have to remove a list level after you make the object. The D3 rendererOptions won't work unless you change the Javascript file, d3_renderers.js, in therpivotTablepackage. (That's what I did with my fork.) If you use my fork, it will work as-is.I've added handlers for
You may notice quite a few changes in the restoration function. That's because the original code would lost
subtotals,sorters, and therenderertsv.I tried to think of anything that could go wrong and this is where I'm at. If you run into any issues, let me know. If you have questions, ask.
The YAML, options, and CSS:
The table.
Lastly, the JS.
In this image, you can see that 'bar chart' was entered (name violation).
During restoration, I searched for 'bar' (which didn't exist).
Now when I'm prompted again, I'm going to type bar chart (with the space) even though you can see that 'barchart' exists, not bar chart.
You'll still get the chart. The handler will remove white space and special characters here, as well.