I am a complete novice when it comes to D3 (with very little experience in JS). Here are the lines of code I am working with:
<script type='text/javascript'> data ='{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4"},{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4 Wag"}';</script>
<script type="text/javascript">
d3.select("body").append("div").text(JSON.parse(data));
</script>
Every time I run this code, I encounter the following error:
Uncaught SyntaxError: Unexpected token , test-html:1
The data itself is generated using R:
<!--begin.rcode results="asis", echo=FALSE, warning=FALSE, message=FALSE
library(RJSONIO)
out <- paste("<script type='text/javascript'> data ='", gsub("\\[|\\]", "",jsonlite::toJSON(mtcars[1:2,])), "';</script>", sep="")
cat(out)
end.rcode-->
This results in the JSON string found in the initial code snippet (data).
If anyone can help me identify what the issue might be, I would greatly appreciate it!
Thank you!