Skip to content

Commit

Permalink
Add "svg" and "function" marks
Browse files Browse the repository at this point in the history
  • Loading branch information
juba committed Aug 27, 2021
1 parent 571e8f1 commit f7a3213
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(mark_dot)
export(mark_dotX)
export(mark_dotY)
export(mark_frame)
export(mark_function)
export(mark_line)
export(mark_lineX)
export(mark_lineY)
Expand All @@ -23,6 +24,7 @@ export(mark_rectX)
export(mark_rectY)
export(mark_ruleX)
export(mark_ruleY)
export(mark_svg)
export(mark_text)
export(mark_textX)
export(mark_textY)
Expand Down
23 changes: 23 additions & 0 deletions R/marks.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ mark_frame <- function(g, fill = "none", ...) {
mark_("frame", g, mark_channels = NULL, fill = fill, ...)
}

#' Add an SVG element to an obsplot
#'
#' @param g an obsplot object
#' @param svg SVG to add, as a character string
#' @param ... options passed to define the mark
#' @export

mark_svg <- function(g, svg, ...) {
f_code <- paste0("() => svg`", svg, "`")
f <- htmlwidgets::JS(f_code)
mark_function(g, f)
}

#' Add a render function mark to an obsplot
#'
#' @param g an obsplot object
#' @param f render function, defined with `htmlwidgets::JS()`
#' @param ... options passed to define the mark
#' @export

mark_function <- function(g, f, ...) {
mark_("function", g, mark_channels = NULL, fun = f, ...)
}

mark_ <- function(mark_type, g, mark_channels, ...) {

Expand Down
18 changes: 18 additions & 0 deletions man/mark_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/mark_svg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion srcjs/modules/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ export class Obsplot {

return this.marks.map((mark) => {

// Render function mark
if (["function"].includes(mark.type)) {
return mark.opts.fun;
}

const mark_fun = Plot[mark.type];

// Decorations mark
if (["frame"].includes(mark.type)) {
return mark_fun.call(null, mark.opts)
return mark_fun.call(null, mark.opts);
}

// Mark data
Expand Down

0 comments on commit f7a3213

Please sign in to comment.