r2d3

Render JavaScript console output from r2d3 into browser console instead of the visualization?


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?


Solution

  • 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).