Reputation: 23650
I want to output html from R, and am hitting issues with particular characters. To illustrate, a function:
output = function(str, fn){ sink(fn); cat(str); sink(); browseURL(fn) }
output("It's an apostrophe", 'good.html')
output("It’s a right single quote", 'bad.html')
Grateful for any tips towards a general solution that doesn't require ad-hoc character replacement.
Upvotes: 1
Views: 170
Reputation: 545588
You did not specify an encoding in your HTML file, so the browser guesses — badly1. This is a general problem with text data, but for HTML there’s a simple solution: specify the encoding in the HTML; for instance:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
It’s a right single quote
</body>
</html>
… and write this text to a file as UTF-8:
output = function (str, filename) {
file = file(filename, encoding = 'UTF-8')
on.exit(close(file))
cat(str, file = file)
browseURL(filename)
}
1 Though it depends on a lot of factors: on my computer, your code happens to work.
Upvotes: 2