如何在RMarkdown中嵌入ggvis交互式图表?

时间:2016-10-29 19:14:48

标签: r r-markdown ggvis

我正在尝试将RMarkdown编织成来自interactivity vignette的html这个情节:

mtcars %>% ggvis(x = ~wt) %>%
    layer_densities(
      adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
      kernel = input_select(
        c("Gaussian" = "gaussian",
          "Epanechnikov" = "epanechnikov",
          "Rectangular" = "rectangular",
          "Triangular" = "triangular",
          "Biweight" = "biweight",
          "Cosine" = "cosine",
          "Optcosine" = "optcosine"),
        label = "Kernel")
    )

但是我收到以下错误消息:

## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

1 个答案:

答案 0 :(得分:6)

您必须设置"输出:html_document"和"运行时:闪亮"在标题中。这对我有用:

---
title: "stackoverflow"
author: "Kári S Friðriksson"
date: "7 nóvember 2016"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggvis)
```



```{r eruptions, echo=FALSE}
mtcars %>% ggvis(x = ~wt) %>%
    layer_densities(
      adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
      kernel = input_select(
        c("Gaussian" = "gaussian",
          "Epanechnikov" = "epanechnikov",
          "Rectangular" = "rectangular",
          "Triangular" = "triangular",
          "Biweight" = "biweight",
          "Cosine" = "cosine",
          "Optcosine" = "optcosine"),
        label = "Kernel")
    )
```

最简单的方法是转到file / new file / R markdown / Shiny / Shiny文档。然后会自动为您设置标题,并且代码框上方的栏上会有一个播放按钮而不是编织按钮。另请注意,您需要在代码中包含library(ggvis),因为闪亮将以清除平板开始,而不能"请参阅"已加载的包或函数。