Intro Thoughts

Status Quo

library(tidyverse)

A code chunk named ‘carsplot’ follows

ggplot(cars) + 
  aes(speed, dist) + 
  geom_point() + 
  aes(size = dist)

knitrExtra::chunk_code_get("carsplot")
## It seems you are currently knitting a Rmd/Qmd file. The parsing of the file will be done in a new R session.
## [1] "ggplot(cars) + "       "  aes(speed, dist) + " "  geom_point() + "    
## [4] "  aes(size = dist)"
knitrExtra::chunk_code_get("carsplot") ->
  code
## It seems you are currently knitting a Rmd/Qmd file. The parsing of the file will be done in a new R session.
unconnected_code <- code %>% str_trim() %>% str_remove("\\+$")


partialcode <- c()



for(i in 1:length(code)){
  
  partialcode[i] <- paste(unconnected_code[1:i], collapse = " + ")
  
}


p <- list()
for(i in 1:length(partialcode)){
  
  p[[i]] <- eval(parse(text = partialcode[i]))
  
 label <- ifelse(i > 1, paste("+", unconnected_code[i]), unconnected_code[i])
 
  p[[i]] <- p[[i]] + annotate(geom = "text",
                              x = I(.025),
                              y = I(.95), 
                              hjust = 0,
                              vjust = 1,
                              label = label,
                              color = "red"
                              )
  # 
}


patchwork::wrap_plots(p, ncol = 2)

knitr::knit_exit()