如何在 Rmarkdown 演示(滑动)中回显代码之前显示块输出? [英] How to display chunk output before echoing code in Rmarkdown presentation (slidy)?

查看:44
本文介绍了如何在 Rmarkdown 演示(滑动)中回显代码之前显示块输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在 Rmarkdown 中使用 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.

我显然可以通过插入两个代码块来解决这个问题,其中第一个具有块选项 echo=FALSE,第二个具有 echo=TRUE, fig.show='hide' 但这需要我确保两个代码块匹配.如何在回显代码之前反转此顺序以显示图表.

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.

推荐答案

您应该能够对演示文稿的正文进行以下操作.

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}
```

即:

  • 创建两个同名的代码块(这里是thecode).
  • 在第一个代码块中,设置 echo = FALSE 以便代码不会打印出来,但会被评估.
  • 在第二个代码块中,设置 echo = TRUE,但保持块完全为空(栅栏之间也没有空行).
  • 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).

这篇关于如何在 Rmarkdown 演示(滑动)中回显代码之前显示块输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆