diff --git a/R/htmlwidgets.R b/R/htmlwidgets.R
index 0ee57998..0ccec4be 100644
--- a/R/htmlwidgets.R
+++ b/R/htmlwidgets.R
@@ -122,8 +122,15 @@ widget_dependencies <- function(name, package){
# Generates a to appear inside of a script tag, even if it's
+ # inside a quoted string. Fortunately we know that in JSON, the only place
+ # the '<' character can appear is inside a quoted string, where a Unicode
+ # escape has the same effect, without confusing the browser's parser. The
+ # repro for the bug this gsub fixes is to have the string "" appear
+ # anywhere in the data/metadata of a widget--you will get a syntax error
+ # instead of a properly rendered widget.
tags$script(type="application/json", `data-for` = id,
- HTML(toJSON(createPayload(x)))
+ HTML(gsub("<", "\\\\u003c", toJSON(createPayload(x))))
)
}