knitr::opts_chunk$set(echo = TRUE, message = F, warning = F)
library(codehover) library(tidyverse) library(htmltools) library(knitr) library(codehover)
ggplot(data = cars) + aes(x = speed) + aes(y = dist) + geom_point(color = 'darkred', size = 4, alpha = .8) + scale_x_continuous(limits = c(0,25)) + geom_smooth() + labs(y = "stopping distance") + labs(title = 'A ggplot fot the rest of us') + labs(subtitle = 'Testing a way of explicit showing the R workflow') + theme_bw() + theme(plot.title.position = "plot")
flipbookr:::chunk_name_return_code_sequence("a_plot") -> partial_code_list partial_code_ch_format <- function(partial_code_list, which){ partial_code_list[[which]] %>% .[str_detect(. ,".+#<<")] %>% str_replace("#<<", "") %>% str_replace("^ ", "<tab2>") %>% str_replace("^ ", "<tab1>") %>% paste0(collapse = "<br>") -> out if(length(partial_code_list) != which) { out %>% paste(., "+") -> out } out }
https://yjunechoe.github.io/posts/2020-12-13-collapse-repetitive-piping-with-reduce/
"ggplot() + <br> <tab1> geom_point(data = cars, </tab1> <br> <tab2> aes(x = speed, </tab2> <br> <tab2> y = dist), </tab2>" library(magrittr) library(codehover) result <- ch_int(type = "incremental") %>% ch_row(text = partial_code_ch_format(partial_code_list, 1), img ="./docs/the_figures-1.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 2), img ="./docs/the_figures-2.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 3), img ="./docs/the_figures-3.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 4), img ="./docs/the_figures-4.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 5), img = "./docs/the_figures-5.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 6), img ="./docs/the_figures-6.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 7), img ="./docs/the_figures-7.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 8), img ="./docs/the_figures-8.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 9), img = "./docs/the_figures-9.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 10), img = "./docs/the_figures-10.png") %>% ch_row(text = partial_code_ch_format(partial_code_list, 11), img = "./docs/the_figures-11.png") %>% ch_out(img = "./docs/the_figures-11.png")
htmltools::HTML(result)
purrr::reduce(.x = c(1:11), .f = ~ .x %>% ch_row(text = partial_code_ch_format(partial_code_list, .y), img = paste0("./docs/the_figures-", .y, ".png")), .init = ch_int(type = "incremental") ) %>% ch_out(img = "./docs/the_figures-11.png") -> a_reduction
partial_code_ch_int_reduce <- function(partial_code_list, fig_path_and_prefix){ purrr::reduce(.x = c(1:length(partial_code_list)), .f = ~ .x %>% ch_row(text = partial_code_ch_format(partial_code_list, .y), img = paste0(fig_path_and_prefix, .y, ".png")), .init = ch_int(type = "incremental") ) %>% ch_out(img = paste0(fig_path_and_prefix, length(partial_code_list), ".png")) } partial_code_ch_int_reduce(partial_code_list = partial_code_list, fig_path_and_prefix = "./docs/the_figures-") -> results_w_reduce
htmltools::HTML(results_w_reduce)
ggplot(data = datasets::sleep) + aes(extra) + geom_rug() + geom_histogram(alpha = .8) + facet_grid(rows = vars(group)) + aes(fill = group) + aes(color = group) + labs(title = "sleep change based on treatement 1 or 2")
flipbookr:::chunk_name_return_code_sequence("sleep") -> sleep_code_seq
partial_code_ch_int_reduce(sleep_code_seq, fig_path_and_prefix = "sleep/the_sleep-" ) -> results_sleep
htmltools::HTML(results_sleep)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.