Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created April 6, 2014 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjallaire/10006611 to your computer and use it in GitHub Desktop.
Save jjallaire/10006611 to your computer and use it in GitHub Desktop.
justgage
#' @export
justgage <- function(title, value, min, max,
label = NULL, width = 200, height = 160) {
structure(class = "justgage", list(
title = title,
label = label,
value = value,
min = min,
max = max,
width = width,
height = height
))
}
#' @export
print.justgage <- function(x, ...) {
html_print(justgage_html(x), justgage_dependencies())
}
#' @export
knit_print.justgage <- function(x, options) {
html_knit_print(justgage_html(x), justgage_dependencies())
}
justgage_html <- function(x) {
# create random/unique id to bind the div and script
id <- paste("justgage", as.integer(stats::runif(1, 1, 10000)), sep="-")
# generate html for the justgage
html <- paste("<div id='", id, "' ",
"style='width:", x$width, "px;",
"height:", x$height, "px'>",
"</div>",
"<script>",
"var g = new JustGage({",
" id: '", id, "',",
" value: ", x$value, ",",
" min: ", x$min, ",",
" max: ", x$max, ",",
" title: '", x$title, "',",
" label: '", x$label, "'",
"});",
"</script>",
sep = "")
# return html
html
}
justgage_dependencies <- function() {
list(
html_dependency(
name = "raphael",
version = "2.1.2",
path = system.file("www/libs/raphael-2.1.2", package = "RmdExamples"),
script = "raphael.js"
),
html_dependency(
name = "justgage",
version = "1.0.1",
path = system.file("www/libs/justgage-1.0.1", package = "RmdExamples"),
script = "justgage.1.0.1.min.js"
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment