@@ -54,8 +54,21 @@ decorate_chunk <- function(chunk_name,
54
54
55
55
my_opts <- as.list(attributes(try_chunk $ result )$ chunk_opts )
56
56
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
+
59
72
60
73
} else if (is_live ) { # If that failed, try the editor pull
61
74
@@ -132,20 +145,20 @@ decorate_chunk <- function(chunk_name,
132
145
} else {
133
146
134
147
my_engine <- my_opts [[" engine" ]]
135
- my_opts <- within(my_opts , rm(engine ))
148
+ my_opts <- within(my_opts , rm(" engine" ))
136
149
137
150
}
138
151
139
152
# If there are special options, write them into the chunk.
140
153
141
154
if (length(my_opts ) > 1 ) {
142
155
143
- my_code <- paste0(" ```{" , my_engine , " , " ,
156
+ my_code <- paste0(" ```{" , my_engine , " " , my_label , " , " ,
144
157
toString(list_to_strings(my_opts )),
145
158
" }\n " , my_code , " \n ```" )
146
159
} else {
147
160
148
- my_code <- paste0(" ```{" , my_engine , " }\n " , my_code , " \n ```" )
161
+ my_code <- paste0(" ```{" , my_engine , " " , my_label , " }\n " , my_code , " \n ```" )
149
162
150
163
}
151
164
0 commit comments