Passing a JSON Object from R to Javascript

I am trying to send data from R to JavaScript and in R I have a I am converting the contents I have to JSON format using the below mentioned code:

data = jsonlite::toJSON(data, pretty=TRUE,flatten=TRUE,auto_unbox = TRUE)

I am assigning a div id for the variable data in R and doing a DOM in JavaScript to get the contents of the variable data in JavaScript.

Please find below JavaScript Code:

var data = document.getElementById("data").innerHTML;
var obj = JSON.parse(data);
var myJSON = JSON.stringify(obj);

function flareData(){
return myJSON;

However, if I hardcode the contents of myJSON after return in flaredata() then it would work as expected. But I am not able to return it using a variable.

I am able to alert the contents in JavaScript but I am not able to pass it as an input to another funtion named flaredata which will generate CirclepackeR(graphs).

Am I missing any conversions in JavaScript or R?