# make sure htmlwidgets:::toJSON() turns list() to {} in JSON, instead of []
.emptyList = setNames(list(), character())
emptyList = function() .emptyList
# evaluate a formula using `data` as the environment, e.g. evalFormula(~ z + 1,
# data = data.frame(z = 1:10))
evalFormula = function(x, data) {
if (!inherits(x, 'formula')) return(x)
if (length(x) != 2) stop('The formula must be one-sided: ', deparse(x))
eval(x[[2]], data, environment(x))
}
# merge two lists by names, e.g. x = list(a = 1, b = 2), mergeList(x, list(b =
# 3)) => list(a = 1, b = 3)
mergeList = function(x, y) {
if (!is.list(y) || length(y) == 0) return(x)
yn = names(y)
if (length(yn) == 0 || any(yn == '')) {
warning('The second list to be merged into the first must be named')
return(x)
}
for (i in yn) {
xi = x[[i]]
yi = y[[i]]
if (is.list(xi)) {
if (is.list(yi)) x[[i]] = mergeList(xi, yi)
} else x[[i]] = yi
}
x
}
# automatic labels from function arguments
autoArgLabel = function(arg, auto) {
if (is.null(arg)) return('')
if (inherits(arg, 'formula')) return(deparse(arg[[2]]))
auto
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.