如何在R中的同一HTML页面中导出两个HTML小部件? [英] How to export two HTML widgets in the same HTML page in R?

查看:197
本文介绍了如何在R中的同一HTML页面中导出两个HTML小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We intend to create a HTML page using R that contains two (or more) widgets. One widget holds a timeline and the other holds data table from a dataframe.

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

We are able to create two separate HTML pages to do this as follows:

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)

另一个窗口小部件是一个数据表,如下所示:

The other widget is a data table as follows:

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)

Thi s保存两个独立的网页,其中包含时间线可视化和HTML数据表。我们希望以这种方式编写R脚本,将表和时间线可视化插入到同一个HTML页面上。我们如何做到这一点?

This saves two separate webpages that hold the timeline visualization and the HTML data table. We would like to write an R script in such a way that insert both the table and the timeline visualization on the same HTML page. How can we do this?

推荐答案

使用R Markdown创建包含多个展品/小部件的html页面:

Use R Markdown to create html pages with multiple 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
```

这篇关于如何在R中的同一HTML页面中导出两个HTML小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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