Created
November 30, 2018 13:38
-
-
Save EvaMaeRey/6c8e16d1c51c793b88172f290cbe5356 to your computer and use it in GitHub Desktop.
ggplot dplyr reveal breaking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "ggplot tutorial" | |
subtitle: "with Emi Tanaka's kunoichi + ninjutsu theme" | |
author: "<br><br>Gina Reynolds" | |
date: "<br>2018/09/16" | |
output: | |
xaringan::moon_reader: | |
chakra: libs/remark-latest.min.js | |
lib_dir: libs | |
css: ["kunoichi", "ninjutsu"] | |
nature: | |
ratio: "16:9" | |
highlightLines: true | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
knitr::opts_chunk$set(fig.height = 6, out.width = "100%", comment = " ") | |
# reveal lines up to `upto` and highlight lines `highlight` | |
reveal <- function(name, upto, highlight = upto) { | |
content <- knitr:::knit_code$get(name) | |
content[upto] <- gsub("%>%", "", content[upto], fixed = T) | |
content[upto] <- gsub("+", "", content[upto], fixed = T) | |
content[highlight] <- paste(content[highlight], "#<<") | |
content[1:upto] | |
} | |
partial_knit_chunks <- function(chunk_name) { | |
# Create slide for lines 1:N for each line N in the given chunk | |
idx_lines <- seq_along(knitr:::knit_code$get(chunk_name)) | |
partial_knit_steps <- glue::glue( | |
"class: split-40", | |
"count: false", | |
"", | |
".column[.content[", | |
"```{r plot{{idx_lines}}, eval=FALSE, code=reveal('{{chunk_name}}', {{idx_lines}})}", | |
"```", | |
"]]", | |
".column[.content.center[", | |
"```{r output{{idx_lines}}, echo=FALSE, code=reveal('{{chunk_name}}', {{idx_lines}})}", | |
"```", | |
"]]", | |
.open = "{{", .close = "}}", .sep = "\n" | |
) | |
glue::glue_collapse(partial_knit_steps, "\n---\n") | |
} | |
``` | |
```{r} | |
library(tidyverse) | |
library(gapminder) | |
``` | |
--- | |
# Wrangle, plot points should be blue coming up | |
--- | |
```{r complete_01, eval=F, echo=F, comment = " "} | |
gapminder %>% | |
filter(year == 1997) %>% | |
ggplot() + | |
aes(x = gdpPercap) + | |
aes(y = gdpPercap) + | |
geom_point(col = "blue") | |
``` | |
`r paste(knitr::knit(text = partial_knit_chunks("complete_01")), collapse = "\n")` | |
--- | |
# Wrangle, plot - points should be green coming up | |
--- | |
```{r complete_02, eval=F, echo=F, comment = " "} | |
gapminder %>% | |
filter(year == 2007) %>% | |
ggplot() + | |
aes(x = pop) + | |
aes(y = pop) + | |
geom_point(col = "green") | |
``` | |
`r paste(knitr::knit(text = partial_knit_chunks("complete_02")), collapse = "\n")` | |
<!-- Create slides for the "complete" chunk --> | |
<!-- ...it's just R Markdown as a vector of strings --> | |
<!-- ...and why not be super concise about it? --> | |
<!-- This css chunk can't be before the first class: split-40... lines --> | |
```{css, eval = TRUE} | |
.remark-code{ line-height: 2; } | |
``` |
Aha! This is makes sense. So the files were overwritten. I'm excited for this solution!! 🥇
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @EvaMaeRey! The problem was that
partial_knit_chunks()
wasn't creating unique chunk names, so the outputs from second "reveal" chunk were writing over the outputs from the first chunk. That is"```{r output{{idx_lines}}, echo=FALSE, code=reveal('{{chunk_name}}', {{idx_lines}})}",
would write out
output1-1.png
,output2-1.png
, etc. regardless of which complete chunk was used to create the partial chunks.I fixed it in my fork of this gist by incorporating the chunk name into the partial chunk names.
"```{r output_{{chunk_name}}_{{idx_lines}}, echo=FALSE, code=reveal('{{chunk_name}}', {{idx_lines}})}",