如何在 R 中的同一个 HTML 页面中导出两个 HTML 小部件?

How to export two HTML widgets in the same HTML page in R?

我们打算使用 R 创建一个包含两个(或更多)小部件的 HTML 页面。一个小部件保存时间轴,另一个保存来自数据帧的数据 table。

我们可以创建两个单独的 HTML 页面来执行此操作,如下所示:

library(timevis)
library(htmlwidgets)

data <- data.frame(
  id      = 1:4,
  content = c("Item one", "Item two",
              "Ranged item", "Item four"),
  start   = c("2016-01-10", "2016-01-11",
              "2016-01-20", "2016-02-14 15:00:00"),
  end     = c(NA, NA, "2016-02-04", NA)
)

timevis(data)
htmlwidgets::saveWidget(timevis(data), "timeline.html", selfcontained = F)

另一个widget是一个数据table如下:

acs <- read.csv(url("http://stat511.cwick.co.nz/homeworks/acs_or.csv"))
acs_temp <- datatable(acs, options = list(pageLength = 10))
htmlwidgets::saveWidget(acs_temp, "page2.html", selfcontained = F)

这将保存两个单独的网页,其中包含时间线可视化和 HTML 数据 table。我们想以这样一种方式编写 R 脚本,即在同一 HTML 页面上同时插入 table 和时间线可视化。我们该怎么做?

使用 R Markdown 创建 html 个包含多个 exhibits/widgets 的页面:

---
title: "Untitled"
output: html_document  
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(timevis)
library(DT)
data <- data.frame(
  id      = 1:4,
  content = c("Item one", "Item two",
              "Ranged item", "Item four"),
  start   = c("2016-01-10", "2016-01-11",
              "2016-01-20", "2016-02-14 15:00:00"),
  end     = c(NA, NA, "2016-02-04", NA)
)

acs <- read.csv(url("http://stat511.cwick.co.nz/homeworks/acs_or.csv"))
acs_temp <- DT::datatable(acs, options = list(pageLength = 10))
```

## R Markdown

```{r timeviz}
timevis(data)
acs_temp
```