5.9 交互式Rmarkdown

在rmarkdown中植入shiny需要以下两步:

  1. 在YAML头部添加runtime: shiny

  2. 将shiny的组件与渲染函数添加到代码块

例子如下

---
title: "demo"
runtime: shiny
output: html_document
---

## 示例

内容

```{r, echo=FALSE}
numericInput("m", "Number of samples:", 2, min = 1, max = 100)
```

```{r, echo=FALSE}
renderPlot({
    means <- replicate(1e4, mean(runif(input$m)))
    hist(means, breaks = 20)
  }, res = 96)
```