rCharts 和 DT 在 rmarkdown 中兼容吗? [英] Are rCharts and DT compatible in rmarkdown?

查看:48
本文介绍了rCharts 和 DT 在 rmarkdown 中兼容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rmarkdown 创建一个文档,其中包含来自 rCharts 包的图和使用 htmlwidgets 中包含的 DT 库的数据表.出于某种原因,我不能同时显示它们.

I am trying to create a document with rmarkdown that includes both plots from the rCharts package and a datatable using the DT library included in htmlwidgets. For some reason I cannot display both of them together.

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

```{r, echo=FALSE}
library(DT)
library(rCharts)

df<-data.frame(Name=c("a","Z","h","k","j"),Value=(sample(10^7,5)))

datatable(df, filter = 'top', options = list(
  pageLength = 10,iDisplaylength=10, autoWidth = TRUE
))
```

```{r, message=FALSE, echo=FALSE, results='asis'}
df<-data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),
               othera=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')

#Different options I tried

p1$print('inline', include_assets = TRUE, cdn = FALSE)
#p1$show('inline', include_assets = TRUE, cdn = FALSE)

#p1$print('inline', include_assets = TRUE)
#p1$show('inline', include_assets = TRUE)

#These provide an error
#p1$print('inline', include_assets = TRUE, cdn = TRUE)
#p1$show('inline', include_assets = TRUE, cdn = TRUE)

```

注释行是我尝试过的东西.

The commented lines are the things I have tried.

注意 I:如果 p1$print('inline', include_assets = TRUE, cdn = FALSE) 被注释,数据表将正确显示.

Note I: if p1$print('inline', include_assets = TRUE, cdn = FALSE) is commented the datatable is displayed properly.

注意二:我知道 p1$save() 函数与 iframe 结合使用,但是,我想使用内联图表.

Note II: I am aware of p1$save() function combined with an iframe, however, I would like to use the chart inline.

推荐答案

jQuery 库包含在页面顶部,当您在 printinclude_assets 时,再次包含它会导致问题.

The jQuery library is included at the top of the page and when you include_assets in the print, the it is included again which causes issues.

要解决此问题,您可以尝试将 include_assets 设置为 false 并手动"添加除 jQuery 之外的所需库.

To fix this, you can try setting include_assets to false and adding the required libraries except jQuery "by hand".

 p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')
    cat("<link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/nv.d3.css>
    <link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/rNVD3.css>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/d3.v3.min.js></script>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/nv.d3.min-new.js></script>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/fisheye.js></script> ")
    p1$print('inline', include_assets = F, cdn = FALSE)

您可以通过在 R 中运行 p1$print('inline', include_assets = T, cdn = FALSE) 找到所需的库和链接,它们将是输出的第一行.src 路径是绝对路径,所以我在上面的代码中用 ... 替换了其中的一部分.

You can find the required libraries and links by running p1$print('inline', include_assets = T, cdn = FALSE) in R, they will be the first lines of output. The src paths are absolute so I replaced some of it by ... in the code above.

这篇关于rCharts 和 DT 在 rmarkdown 中兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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