2

JavaScript console output from r2d3 is rendered directly into the RStudio viewer visualization instead of the javascript console. See documentation.

This also seems to be the behavior when

r2d3::r2d3(..., viewer = 'browser')

I.e., the console.log() does not output to the console even in the browser.

Is there a clean way to change this behavior, for console.log() information in an r2d3 D3.js to be sent to the browser console?

ssp3nc3r
  • 3,662
  • 2
  • 13
  • 23

1 Answers1

4

R2D3 overwrites the console within the shadow DOM that contains the visualization code. This is why the console behavior is altered - which might look nice, but doesn't allow closer inspection of objects, or hiding of the console.

There don't appear to be any parameters that can modify this when creating the visualization. This leaves two options, modify the package or add a line of javascript to the visualization. I'll go with the latter here.

The document window itself still retains default console behavior, so we can use it to redefine the shadow DOM console behavior. In the visualization, at the top line add:

console = d3.window(svg.node()).console;

There are other methods of accessing the window, but this should be sufficient, and might be the shortest, it won't work for d3v3, which could use:

console = svg.node().ownerDocument.defaultView.console;

Yes, it is not ideal and a bit hacky, but it is certainly easier than changing the R source (and continuing to do so when the package is updated).

Andrew Reid
  • 37,021
  • 7
  • 64
  • 83
  • So, I think I'm having similar issues w/ r2d3 but not sure it's 100% what the original poster here was referring to. I would much appreciate if someone could look at the new question I posted here (thanks in advance): https://stackoverflow.com/questions/69859445/de-bugging-r2d3-r-in-inspector-cant-view-data-in-console – DiamondJoe12 Nov 05 '21 at 21:42
  • Looks like the same issue - I'm not able to test at the moment though. The problem being that r2d3 overwrites default console.log behavior to show an error message, but that reduces all objects to strings, hence why you get something logged like Object obejct,... rather than the object itself. – Andrew Reid Nov 06 '21 at 21:32
  • Thanks Andrew! Is there any solution / way around this? It's almost impossible to debug without seeing the console.. – DiamondJoe12 Nov 07 '21 at 05:14
  • Oh, dude. Awesome. I tested this solution and it seems to work. Now I can actually use r2d3. Thanks much! – DiamondJoe12 Nov 08 '21 at 16:34