0

I'm rendering an .html file using knitr::spin() on a .R script file, and I want to conditionally display sections in its .html output file. I have some example code below of what I have so far.

#'
#/*****************************************************************************/
#' **SECTION 0:** Some stuff here
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** Other stuff here
#/*****************************************************************************/

# /*
y <- 5
# */

if(x==2 & y==1){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** Scenario 1
  #/*****************************************************************************/
  
} else if(x==2 & y==5){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** Scenario 2
  #/*****************************************************************************/
  
}


# /*
knitr::spin("spin_test.R", knit = TRUE)
# */

How exactly can I modify my script above so that the output of spin_test.html looks like this:

SECTION 0: Some stuff here

SECTION 1: Other stuff here

SECTION 3: Scenario 2

As of now, my current output looks like this:

SECTION 0: Some stuff here

SECTION 1: Other stuff here
if(x==2 & y==1){
  
  #'
  #' **SECTION 3:** Scenario 1
  
} else if(x==2 & y==5){
  
  #'
  #' **SECTION 3:** Scenario 2
  
}
NULL

My goal is to get the appropriate conditional section in the .html output by solely using knitr:spin() on a .R script. Any advice or help would be greatly appreciated.

theneil
  • 488
  • 1
  • 4
  • 14

1 Answers1

0

I was able to get the desired the output by following the post here. All the credit to the user MattP from the Posit Community.

#'
#/*****************************************************************************/
#' **SECTION 0:** Some stuff here
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** Other stuff here
#/*****************************************************************************/

# /*
y <- 5
# */


#+ eval=(x==2 & y==1), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** Scenario 1: x=%s and y=%s",x,y)) %>%
  cat()

#+ eval=(x==2 & y==5), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** Scenario 2: x=%s and y=%s",x,y)) %>%
  cat()

# /*
knitr::spin("spin_test.R", knit = TRUE)
# */
theneil
  • 488
  • 1
  • 4
  • 14