Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display chunk output before echoing code in Rmarkdown presentation (slidy)?

I recently started using the Slidy presentation template in Rmarkdown and like how each slide allows you to scroll down for more content.

One way I'm using this is in sharing plots with my students (see example code below). On a single slide I can display the plot along with the exact code used to create the plot which can be viewed by scrolling down.

---
title: Echo Code Chunks After Code Results
subtitle: Thanks For Your Help
author: Me
date: "today"
output: slidy_presentation
runtime: shiny
---

## Slide with Interactive Plot

```{r, echo=TRUE, warning=FALSE, message=FALSE}
shinyApp(options = list(width = "100%", height = "700px"),
  ui = ( fluidPage(
inputPanel(
  selectInput("n_breaks", label = h3("Number of bins:"),
              choices = c(10, 20, 35, 50), selected = 20),

  sliderInput("bw_adjust", label = h3("Bandwidth:"),
              min = 0.2, max = 2, value = 1, step = 0.2)),
    plotOutput("stuff", height = "650px")

)),

server = function(input,output,session) {

  output$stuff = renderPlot({
  hist(faithful$eruptions, probability = TRUE, 
breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration", 
col = "bisque", border = 1)

  dens <- density(faithful$eruptions, adjust = input$bw_adjust, lwd = 2, col = "blue")
  lines(dens, col = "blue")
})
})
```

The problem I'm having is that the default behavior is to echo the code chunks before the code results, which is reverse of how I want it.

I can obviously solve this by inserting two code chunks where the first has chunk option echo=FALSE and the second has echo=TRUE, fig.show='hide' but this requires me to ensure that both code chunks match. How can I reverse this order to have the plots display before the code is echoed.

As always, thanks for the help.

like image 636
Jason Freels Avatar asked Sep 30 '15 14:09

Jason Freels


1 Answers

You should be able to do what you want with the following for the body of your presentation.

## Slide with Interactive Plot

```{r thecode, echo=FALSE, warning=FALSE, message=FALSE}
shinyApp(options = list(width = "100%", height = "700px"),
  ui = (fluidPage(inputPanel(
    selectInput("n_breaks", label = h3("Number of bins:"), 
                choices = c(10, 20, 35, 50), selected = 20),
    sliderInput("bw_adjust", label = h3("Bandwidth:"), 
                min = 0.2, max = 2, value = 1, step = 0.2)),
    plotOutput("stuff", height = "650px"))),

server = function(input,output,session) {
  output$stuff = renderPlot({
    hist(faithful$eruptions, probability = TRUE, 
         breaks = as.numeric(input$n_breaks), xlab = "Duration (minutes)", 
         main = "Geyser eruption duration", col = "bisque", border = 1)
    dens <- density(faithful$eruptions, adjust = input$bw_adjust, 
                    lwd = 2, col = "blue")
    lines(dens, col = "blue")})
  })
```

```{r thecode, eval=FALSE}
```

That is:

  • Create two code chunks with the same name (here thecode).
  • In the first code chunk, set echo = FALSE so that the code doesn't print out, but it is sill evaluated.
  • In the second code chunk, set echo = TRUE, but keep the chunk entirely empty (no blank lines between the fences either).
like image 110
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 03 '22 05:11

A5C1D2H2I1M1N2O1R2T1