ggtrack icon indicating copy to clipboard operation
ggtrack copied to clipboard

Add a knitr hook to automatically add tracker to plot

Open mrjoh3 opened this issue 2 years ago • 3 comments

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

mrjoh3 avatar Jul 09 '21 01:07 mrjoh3

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() 

mrjoh3 avatar Jul 10 '21 07:07 mrjoh3

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)

mrjoh3 avatar Jul 10 '21 07:07 mrjoh3

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

mrjoh3 avatar Jul 10 '21 07:07 mrjoh3