rmarkdown in spin: conditional execution of code and markdown render

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 the solution I have implemented so far, just wondering if there is a cleaner way:

#' ---
#' title: "Sample Section Chunking"
#' author: "My Name (my.name@example.com)"
#' date: "`r Sys.Date()`"
#' output:
#'   html_document:
#'     toc: true
#'     toc_depth: 3
#'     code_folding: hide
#' editor_options: 
#'   chunk_output_type: console
#' ---

#+message=FALSE, warning=FALSE
library(glue)
library(dplyr)

#' # Intro
#' 
#' This gets rendered.
#' 
#' See: https://forum.posit.co/t/rmarkdown-in-spin-conditional-execution-of-code-and-markdown-render/96554
#' 

#+
chunk_flag <- FALSE





#+ eval=chunk_flag,include=chunk_flag,results="asis"
glue("
## The True branch

Gets rendered when chunk_flag=TRUE

Real Value: chunk_flag={chunk_flag}
") %>%
    cat()



#+ eval=!chunk_flag,include=!chunk_flag,results="asis"
glue("
## The False branch

Gets rendered when chunk_flag=FALSE

Real Value: chunk_flag={chunk_flag}
") %>%
    cat()

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

Also, the asis engine can sometimes be useful for this: https://bookdown.org/yihui/rmarkdown-cookbook/eng-asis.html and the knit_expand function is a bit like glue: https://bookdown.org/yihui/rmarkdown-cookbook/knit-expand.html

Just curious: why not use a .Rmd file and keep a .R file to spin ?

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...

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.