I would like to conditionally execute code and render/emit markdown based on a conditional. I don't know how to make a complete self-contained regex in this case, so I will have to resort to pseudocode. I am using the spin format for rmarkdown in .R files rather than traditional rmarkdown syntax. I think the following pseudocode will describe what I want to do:
#+ computeConditional
conditional_flag <- sample(c(TRUE, FALSE), 1)
#+ eval=conditional_flag
#' Flag was true
x <- 1
#+ eval= ! conditional_flag
#' Flag was false
x <- 2
I know this doesn't work because markdown isn't part of the #+ code section that gets defined, but the intent is that, if conditional_flag is true, I want it to print markdown that says the flag is true and to execute the code that is near there when the flag is true. If the flag is false, I want it to instead print the markdown saying it was false and execute that branch of code. I think I might be able to do this if I wrap the markdown in a cat() statement, but I am wondering if I am missing a simpler construct that does what I want.
This is a correct way to do it in a R file to spin.
You could do differently with other tools if you were using .Rmd file. For example, the expoxy package does offer a glue engine to do this kind of think: https://github.com/gadenbuie/epoxy
Thank you! I'll look at knit_expand(). As to why I'm not using .Rmd files... not sure. I'm experimenting with workflows. I'm sticking with .R files because with slight mods, I can run them as straight scripts. At least, I know how to do that. I suppose I could code it to spin (or purl?) out a .R file from an .Rmd file, but I prefer staying in one consistent format for now. A bit extra to add comments with #', but overall not hard, and RStudio makes it easy to prefix with. I'm open to other workflows -- I'll look into epoxy...