ggtrack
ggtrack copied to clipboard
Add a knitr hook to automatically add tracker to plot
in a knitr chunk that outputs a ggplot the chunk header could include track=tracker.object
tracker.object would point to a tracker defined earlier in the page
I have had 2 attempts at this, first using the source hook
knit_hooks$set(
source = function(x, options) {
gg = eval(parse(text = x))
print(class(gg))
track <- options$ggtrack
print(track)
add_banner(gg, track)
}
)
{r, ggtrack=track, eval=FALSE, results='hide'}
ggplot(mapping = aes(x = 1:10, y = rnorm(10))) +
geom_bar(stat = 'identity') +
theme_minimal()
second try was with
ggtrack_hook = function(before, options, envir) {
if (before) {
return()
} else {
class(envir$gg)
add_banner(envir$gg, options$ggtrack)
}
}
knitr::knit_hooks$set(ggtrack = track_hook)
both are simple attempts, but the first one errored when adding the banner and the second seemed to work but the chart would not display.
both seemed to pass the correct objects into the hook, which was encouraging . But I spent too much time with little progress.
If anyone is interested see https://github.com/mrjoh3/ggtrack/blob/knitr-hook/R/knitr_hook.R https://github.com/mrjoh3/ggtrack/blob/knitr-hook/inst/working/hooks.Rmd