Skip to content

Commit

Permalink
Better system for numbering images
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jun 22, 2017
1 parent 3c000b7 commit 5dc5bd4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
memoise,
purrr,
pkgload,
R6,
rematch,
rlang,
rmarkdown (>= 1.1.9007),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export(template_navbar)
export(template_reference)
import(rlang)
importFrom(MASS,addterm)
importFrom(R6,R6Class)
importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
importFrom(roxygen2,roxygenise)
30 changes: 22 additions & 8 deletions R/rd-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,20 @@ as_data.tag_examples <- function(x, path, ...,
}
}

# TODO Handle plots
id_generator <- UniqueId$new()

html <- purrr:::pmap_chr(
list(code = code, run = run, id = seq_along(code)),
list(code = code, run = run),
format_example_chunk,
env = child_env(env),
path = path,
topic = topic
topic = topic,
obj_id = id_generator$id
)
paste(html, collapse = "")
}

format_example_chunk <- function(code, run, path, topic = "unknown", id = 1, env = global_env()) {
format_example_chunk <- function(code, run, path, topic = "unknown", obj_id, env = global_env()) {
if (!run) {
code <- gsub("^\n|^", "# NOT RUN {\n", code)
code <- gsub("\n$|$", "\n# }\n", code)
Expand All @@ -175,10 +177,7 @@ format_example_chunk <- function(code, run, path, topic = "unknown", id = 1, env

expr <- evaluate::evaluate(code, env, new_device = TRUE)

replay_html(
expr,
name_prefix = paste0(topic, "-", id, "-")
)
replay_html(expr, topic = topic, obj_id = obj_id)
}

#' @export
Expand Down Expand Up @@ -225,3 +224,18 @@ is_newline <- function(x, trim = FALSE) {
}
identical(text, "\n")
}

UniqueId <- R6Class("UniqueId", public = list(
env = new_environment(),

id = function(name) {
if (!env_has(self$env, name)) {
id <- 1
} else {
id <- env_get(self$env, name) + 1
}

env_bind(self$env, !! name := id)
id
}
))
6 changes: 3 additions & 3 deletions R/replay-html.r
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ replay_html.list <- function(x, ...) {

pieces <- character(length(parts))
for (i in seq_along(parts)) {
pieces[i] <- replay_html(parts[[i]], obj_id = i, ...)
pieces[i] <- replay_html(parts[[i]], ...)
}
paste0(pieces, collapse = "")
}
Expand Down Expand Up @@ -103,8 +103,8 @@ replay_html.error <- function(x, ...) {
}

#' @export
replay_html.recordedplot <- function(x, name_prefix, obj_id, ...) {
path <- paste0(name_prefix, obj_id, ".png")
replay_html.recordedplot <- function(x, topic, obj_id, ...) {
path <- paste0(topic, "-", obj_id(topic), ".png")

w <- 700
h <- w / 1.618
Expand Down
1 change: 1 addition & 0 deletions R/util.r
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @importFrom magrittr %>%
#' @importFrom roxygen2 roxygenise
#' @importFrom R6 R6Class
#' @import rlang
NULL

Expand Down
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions docs/reference/pulledpork.html

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

0 comments on commit 5dc5bd4

Please sign in to comment.