Conditional inline text in Rmarkdown

I am trying to produce an automated html output using Rmarkdown. Some of the reports contain data for 2022 only and some contain data for 2017 and 2022.

I want to conditionally print text based on what data is available for that clinic.

I initially create a dataset where for each variable a 0 will be printed if there are no observations:

data_from_2017 <- as.data.frame(colSums(!is.na(clinic_subset17))) %>% 
  t() %>% 
  as.data.frame()

Then this is the inline code I am using:

`r 
if (data_from_2017$lifesatisfaction == 0) {
  Their current life satisfaction was rated on a scale of 1-10 (with 10 representing the highest satisfaction). The average life satisfaction score for your clinic was `r life_sat` in 2022.
} else {
     Their current life satisfaction was rated on a scale of 1-10 (with 10 representing the highest satisfaction). The average life satisfaction score for your clinic was `r life_sat17` in 2017 and `r life_sat` in 2022.
} `

However, it fails to parse – what am I doing wrong?

Leave a Comment