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

自古美人都是妖i 提交于 2019-12-11 17:11:40

问题


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.

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)

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?


回答1:


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


来源:https://stackoverflow.com/questions/46206021/how-to-export-two-html-widgets-in-the-same-html-page-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!