rquarto

How do I add R inline a code within a quarto figure subcaption


In quarto, I a trying to add inline code within a figure subcaption, I have tried several things but none seem to work. Below is a reproducible example:

test=403.87
#| label: example
#| code-fold: true
#| fig-show: "hold"
#| layout-ncol: 2
#| fig-cap: !expr "paste0('R expression in the caption works: ',test)"
#| fig-subcap: 
#|   - !expr "test" 
#|   - !expr "paste0('R expression in the subcaption does not work: ',test)"

library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) + 
        geom_point() + 
        geom_smooth(method = "loess", se = FALSE)
ggplot(airquality, aes(Temp, Ozone)) + 
        geom_point() + 
        geom_smooth(method = "lm", se = FALSE)

Solution

  • This is not supported in fig-subcap. The quarto developers recommend this approach instead:

    ---
    title: "Test"
    format: html
    ---
    
    ```{r}
    #| echo: false
    test=403.87
    ```
    
    :::: {#fig-test layout="[1,1]"}
    
    ::: {#fig-test-1}
    ```{r}
    #| echo: false
    plot(1:10)
    ```
    
    `{r} paste0('R expression in the subcaption works too: ',test)`
    :::
    
    ::: {#fig-test-2}
    ```{r}
    #| echo: false
    plot(1:10)
    ```
    
    `{r} test`
    :::
    
    `{r} paste0('R expression in the caption works: ',test)`
    ::::
    

    Which produces this output:

    sreenshot of rendered html showing captions and subcaptions