最近我一直在试验slidify和rCharts.使用slidify生成简单图表的教程是解释性的,但我无法找到任何关于rCharts的教程.
例如,我知道以下内容会生成交互式图
data(mtcars)
r1<- rPlot(mpg ~ wt | am + vs, data=mtcars, type="point")
data(iris)
hair_eye = as.data.frame(HairEyeColor)
rPlot(Freq ~ Hair | Eye,color = 'Eye', data = hair_eye, type = 'bar')
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何将结果绘图合并到我的幻灯片中slidify.
我在Ramnath的git上看到了以下内容:
---
title : Practice
subtitle : makes perfect
author : Noob
job :
framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
widgets : [nvd3] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
---
```{r setup, message = F, echo = F}
require(rCharts)
options(RCHART_WIDTH = 800, RCHART_HEIGHT = 500)
knitr::opts_chunk$set(comment = NA, results = 'asis', tidy = F, message = F)
```
## NVD3 Scatterplot
```{r echo = F}
data(mtcars)
n1 <- nPlot(mpg ~ wt, group = 'gear', data = mtcars, type = 'scatterChart')
n1$print('chart1')
```
Run Code Online (Sandbox Code Playgroud)
但最终出现了这个错误:
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'libraries/widgets/nvd3/nvd3.html': No such file or directory
Run Code Online (Sandbox Code Playgroud)
之后我决定将nvd3文件夹从Ramnath的小部件直接复制到我的,希望这可以解决问题.然而,这最终疯狂地展示了Ramnath的git页面以及我在背景中的幻灯片!
该怎么办?我非常感谢有关如何完成此任务的任何指导/指示/建议.而且,我希望这个问题可以帮助像我这样的其他新手使用精彩的rCharts.
注意:我使用R的标准编辑器,而不是R-studio.我觉得前者不那么杂乱.
下面的所有说明都假定您已dev安装软件包的分支(slidify,slidifyLibraries和rCharts).你可以用它完成这个install_github.
pkgs <- c("slidify", "slidifyLibraries", "rCharts")
devtools::install_github(pkgs, "ramnathv", ref = "dev")
Run Code Online (Sandbox Code Playgroud)
rCharts在slidify文档中包含一个viz 有两种方法,下面的套牌说明了两种方式.如果您在代码块中打印绘图,就像在R控制台中一样,slidify会自动检测到您在knitr会话中运行它,因此将生成的html保存到iframe并将其嵌入到套牌中.或者,您可以指定内联图表,在这种情况下您必须使用n1$show("inline")并包含ext_widgets: {rCharts: libraries/nvd3}在您的YAML前端内容中.
iframe方法是默认和推荐的方法,以避免各种JavaScript文件和CSS之间的冲突.内联方法适用于多个rCharts库,但请务必在使用前进行检查.
---
title : rCharts Integration
ext_widgets : {rCharts: libraries/nvd3}
mode: selfcontained
---
## NVD3 Plot Inline
```{r nvd3plot, results = 'asis', comment = NA, message = F, echo = F}
require(rCharts)
n1 <- nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
n1$show('inline')
```
---
## NVD3 Plot Iframe
```{r nvd3plot2, results = 'asis', comment = NA, message = F, echo = F}
require(rCharts)
n1 <- nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
n1
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3801 次 |
| 最近记录: |