How to display chunk output before echoing code in

2019-06-22 07:22发布

问题:

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.

回答1:

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