Created
April 6, 2014 14:08
-
-
Save jjallaire/10006611 to your computer and use it in GitHub Desktop.
justgage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @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