Hi there,
I use RMarkdown (specifically {bookdown}) for the sake of reproducibility and dynamic control of the human-readable text. I wonder if it is possible to automatically convert dplyr::filter()
conditions to human-readable text.
For instance,
library(tidyverse)
threshold <- 3
# Condition 1
mpg %>%
filter(displ > threshold) %>%
ggplot(aes(x = cty,
y = hwy,
color = as.factor(cyl))) +
geom_point() +
theme(legend.position = "bottom")
In this case, the plot description can be done with in-line code in the way:
City miles per gallon vs. highway miles per gallon for cars with engine displacements greater than `r threshold `.
However, if I change the filtering condition (not just the threshold), the text will become invalid.
# Condition 2
mpg %>%
# >= used instead of > in "Condition 1"
filter(displ >= threshold) %>%
ggplot(aes(x = cty,
y = hwy,
color = as.factor(cyl))) +
geom_point() +
theme(legend.position = "bottom")
or
# Condition 3
threshold <- c(2.5, 5)
mpg %>%
filter(between(displ, threshold[[1]], threshold[[2]])) %>%
ggplot(aes(x = cty,
y = hwy,
color = as.factor(cyl))) +
geom_point() +
theme(legend.position = "bottom")
So, would it be possible to dynamically set the filtering condition and then compile a human-readable text from it?