Skip to content

Commit 4664b3c

Browse files
authored
Merge pull request #31 from jdblischak/labels
Use consistent labels for flaired chunks
2 parents c8fa252 + eafbc55 commit 4664b3c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

R/decorate_chunk.R

+18-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,21 @@ decorate_chunk <- function(chunk_name,
5454

5555
my_opts <- as.list(attributes(try_chunk$result)$chunk_opts)
5656

57-
# labels mess up knit_child
58-
my_opts <- within(my_opts, rm(label))
57+
# Avoid knitr's duplicate chunk label error by appending "-flaired" to the
58+
# chunk label before rendering with knit_child
59+
my_label <- paste0(my_opts[["label"]], "-flaired")
60+
# If the same chunk is decorated twice, or if the user by chance has labeled
61+
# a chunk of the same name plus "-flaired", add a random string to ensure
62+
# uniqueness
63+
if (my_label %in% knitr::all_labels()) {
64+
random <- sample(c(0:9, letters), 7, replace = TRUE)
65+
random <- paste(random, collapse = "")
66+
my_label <- paste0(my_label, "-", random)
67+
}
68+
# Remove the label from the chunk options. Required for properly forming
69+
# my_code below.
70+
my_opts <- within(my_opts, rm("label"))
71+
5972

6073
} else if (is_live) { # If that failed, try the editor pull
6174

@@ -132,20 +145,20 @@ decorate_chunk <- function(chunk_name,
132145
} else {
133146

134147
my_engine <- my_opts[["engine"]]
135-
my_opts <- within(my_opts, rm(engine))
148+
my_opts <- within(my_opts, rm("engine"))
136149

137150
}
138151

139152
# If there are special options, write them into the chunk.
140153

141154
if (length(my_opts) > 1) {
142155

143-
my_code <- paste0("```{", my_engine, ", ",
156+
my_code <- paste0("```{", my_engine, " ", my_label, ", ",
144157
toString(list_to_strings(my_opts)),
145158
"}\n", my_code, "\n```")
146159
} else {
147160

148-
my_code <- paste0("```{", my_engine, "}\n", my_code, "\n```")
161+
my_code <- paste0("```{", my_engine, " ", my_label, "}\n", my_code, "\n```")
149162

150163
}
151164

0 commit comments

Comments
 (0)