简体   繁体   English

如何在Rmarkdown演示(slidy)中回显代码之前显示块输出?

[英]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. 我最近开始在Rmarkdown中使用Slidy演示模板,并喜欢每张幻灯片如何向下滚动以获取更多内容。

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. 我显然可以通过插入两个代码块来解决这个问题,其中第一个具有块选项echo=FALSE ,第二个具有echo=TRUE, fig.show='hide'但这需要我确保两个代码块匹配。 How can I reverse this order to have the plots display before the code is echoed. 如何反转此顺序以在回显代码之前显示图表。

As always, thanks for the help. 一如既往,感谢您的帮助。

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 ). 创建两个代码块具有相同的名称(这里thecode )。
  • In the first code chunk, set echo = FALSE so that the code doesn't print out, but it is sill evaluated. 在第一个代码块中,设置echo = FALSE以便代码不会打印出来,但是它会被评估。
  • In the second code chunk, set echo = TRUE , but keep the chunk entirely empty (no blank lines between the fences either). 在第二个代码块中,设置echo = TRUE ,但保持块完全为空(围栏之间也没有空行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM