How to write javascript function inside string
I am trying to run a JavaScript function in R. One piece of code requires me to pass a JavaScript function that contains the filename as a string. Since the string contains the path to the file, it causes all hell to be destroyed with single and double quotes and backslashes. That's what i am
d3chart$chart(tooltipContent = "#! function(item, x, y, e,z){
return '' + '<img src=\"/Users/cBl.png\"' + '/>'
} !#")
d3chart
The result should be that I see the image in the tooltip in the diagram. This all works without error, but in the diagram I get a question mark instead of an image, which I assume means it cannot follow the path of the filename. I think it has something to do with the fact that I'm not sure how to properly write this filepath line to call the img part src
.
source to share
I am guessing it is a line break that makes the whole line be a syntax error, also I would remove return '' + it makes no difference I added the full path to the file location so you can debug to better understand if the problem is with wrong file location
you can add \ n \ to the end of the line:
d3chart$chart(tooltipContent = "#! function(item, x, y, e,z){ \n\
return '<img src=\"'+document.location.hostname+'/'+document.location.pathname+'/Users/cBl.png\"' + '/>'\n\
} !#")
d3chart
source to share