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

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

One of the ways I use this is to share plots with my students (see code example below). On one slide, I can display the graph along with the exact code used to create the graph, 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 am facing is that the default behavior is to repeat code snippets before the code results, which is the opposite of what I want.

I will obviously resolve this by inserting two code snippets where the first has the chunk parameter echo=FALSE , and the second has echo=TRUE, fig.show='hide' , but this requires that both code fragments match. How can I reverse this order to display graphs before the code echoes.

As always, thanks for the help.

+5
source share
1 answer

You can do what you want with the following text for 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} ``` 

I.e:

  • Create two pieces of code with the same name (here thecode ).
  • In the first block of code, set echo = FALSE so that the code does not print, but it is evaluated by default.
  • In the second code snippet, set echo = TRUE , but keep the piece completely empty (no empty lines between the guards).
+4
source

Source: https://habr.com/ru/post/1232630/


All Articles