Search code examples
rr-markdownknitrxaringan

Xaringan does render a chunk with chunk option class.source defined. How to solve it?


I am using xaringan to create slides with stan code. I want to present the model code and save it using the syntax below. However, when I add the class.source = "stan" (to highlight the stan syntax appropriately) option the code does not render as it should:

---
output:   xaringan::moon_reader
---

---

```{cat, engine.opts = list(file = 'code/stan/ex1.stan'), name = "ex1.stan", class.source = "stan"}
data {
    int<lower=0> N; //number of data
    vector[N] x; //covariates
    vector[N] y; //variates
}

parameters {
  real alpha; //intercept
  real beta; //slope
  real<lower=0> sigma; //scatter
}

model {
  //priors
  alpha ~ normal(0, 10);
  beta ~ normal(0, 10);
  sigma ~ normal(0, 1);
  
  y ~normal(alpha + beta * x, sigma); //likelihood
}

enter image description here

So the desired result would be:

enter image description here


Solution

  • As I understand you in comments above, you simply need this:

    ---
    output:   xaringan::moon_reader
    ---
    
    ---
    ```{cat, engine.opts = list(file = "test.stan", lang = "stan")}
    data {
        int<lower=0> N; //number of data
        vector[N] x; //covariates
        vector[N] y; //variates
    }
    
    parameters {
      real alpha; //intercept
      real beta; //slope
      real<lower=0> sigma; //scatter
    }
    
    model {
      //priors
      alpha ~ normal(0, 10);
      beta ~ normal(0, 10);
      sigma ~ normal(0, 1);
      
      y ~normal(alpha + beta * x, sigma); //likelihood
    }
    ```