r - How to display chunk output before echoing code in Rmarkdown presentation (slidy)? -
i started using slidy presentation template in rmarkdown , how each slide allows scroll down more content.
one way i'm using in sharing plots students (see example code below). on single slide can display plot along exact code used create plot can viewed scrolling down.
--- title: echo code chunks after code results subtitle: author: me date: "today" output: slidy_presentation runtime: shiny --- ## slide 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 default behavior echo code chunks before code results, reverse of how want it.
i can solve inserting 2 code chunks first has chunk option echo=false
, second has echo=true, fig.show='hide'
requires me ensure both code chunks match. how can reverse order have plots display before code echoed.
as always, help.
you should able want following body of presentation.
## slide 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 2 code chunks same name (here
thecode
). - in first code chunk, set
echo = false
code doesn't print out, sill evaluated. - in second code chunk, set
echo = true
, keep chunk entirely empty (no blank lines between fences either).
Comments
Post a Comment