I am trying to build a simple htmlwidget
that makes a bar graph using D3
and R
. I have setup a gitHub with my code and put together a markdown file with the necessary commands to recreate my workflow when creating the widget. I am not sure if I have a syntax error, or just a problem with my process in general?
I ask because although the code runs, it does not produce an output image in RStudio's viewer. I am hoping that perhaps someone who has experience with D3
, htmlwidgets
, JavaScript
and R
can help me figure out what is going wrong with this. Any insights are always greatly appreciated.
Thank you, Nathan
Link: https://github.com/ngfrey/d3BarGraph
Update: I have...
- Installed the latest version of HTMLWidgets from GitHub
- Fixed the problem with my D3 library not being an actual JavaScript document. Good catch timelyportfolio
- Attempted to create this project as a minimal example, however, I do not know how to further minimize my d3BarGraph JavaScript file.
- Thanks to timelyportfolio, I think the problem is within the d3BarGraph.js file.
- Specifically, how I am binding the data to the instance. Within the HTMLWidgets.widget() function's renderValue section.
- I do not see the option to inspect the element when right-clicking in RStudio's Viewer panel. Does this mean nothing is being returned?
- I have posted the code I am suspicious about below.
- Again, thank you everyone for taking the time to help
- Installed the latest version of HTMLWidgets from GitHub
Here is the HTMLWidgets.widget part of my code.
HTMLWidgets.widget({
name: 'd3BarGraph',
type: 'output',
//renderOnNullValue: true,
initialize: function(el, width, height) {
var svg = d3.select(el).append("svg");
svg
.attr("width", width)
.attr("height", height);
//create barD3 instance
return barD3().width(width).height(height).svg(svg); //passing the svg obj & options to the barD3 function.
},
resize: function(el, width, height, instance) {
//var svg = instance.svg;
var svg = d3.select(el).select("svg");
svg
.attr('width', width)
.attr('height', height);
instance.width(width).height(height).svg(svg).resize();
},
renderValue: function(el, params, instance) {
// instance.lastValue = params;
var data = HTMLWidgets.dataframeToD3(params.data);
instance=instance.svg(svg).data(data);
//var data = [8,16,10,18,19,4,12,18,12,11,19,11,15,13,5];
d3.select(el)
.call(instance);
//Time for the barD3 function
return barD3().width(width).height(height).svg(svg);
}
});
I have also updated my GitHub project. I'd love to hear people's comments. Best, NF