J_F
J_F

Reputation: 10352

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.

Upvotes: 2

Views: 2669

Answers (2)

sabre
sabre

Reputation: 694

It won't show as a panel in your presentation, but to add a </> Source Code button to your dashboard, include source_code: embed in your YAML.

--- title: "Example" output: flexdashboard::flex_dashboard: source_code: embed ---

Upvotes: 4

Ian Wesley
Ian Wesley

Reputation: 3624

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)
```

Upvotes: 4

Related Questions