Search code examples
rshinyflexdashboard

Show code in flexdashboard


I'm trying to produce a flexdashboard with R and want to show code in my presentation, but this seems not to work. Here a little example:

---
title: "Test"
output: 
  flexdashboard::flex_dashboard
---

```{r setup, include=FALSE}
library(flexdashboard)
```

### Code

```{r, eval=FALSE, include=TRUE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```   

### Output

```{r, fig.align='center', echo = FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

enter image description here

Even other chuck options like fig.show = 'hide' will not work. Is it possible to show code in a R-chuck in flexdashboard? The code highlights would be a benefit instead of a plain text.


Solution

  • If you want both the code and plot to show set the chunk options to: echo = true

    If you just want code set it to: echo=TRUE, eval=FALSE

    ---
    title: "Test"
    output: 
      flexdashboard::flex_dashboard
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    ```
    
    ### Code
    
    ```{r, echo=TRUE, eval=FALSE}
    plot(iris$Sepal.Length, iris$Sepal.Width)
    ```   
    
    ### Code and Plot
    
    ```{r, echo=TRUE}
    plot(iris$Sepal.Length, iris$Sepal.Width)
    ``` 
    
    ### Plot
    
    ```{r, fig.align='center', echo = FALSE}
    plot(iris$Sepal.Length, iris$Sepal.Width)
    ```