以编程方式生成的Rmarkdown中的“打印" HTML窗口小部件 [英] 'printing' HTML widgets in programmatically generated Rmarkdown

查看:87
本文介绍了以编程方式生成的Rmarkdown中的“打印" HTML窗口小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式生成一些Rmarkdown,并且其中一部分包含一个HTML小部件.如果它们在我的函数中的最后位置,这些输出就很好.但是,如果我将它们包装在print中,则可以像在作图时一样在它们之后放置其他内容,它们不会产生任何输出.

我不确定这与knitr处理打印的方式有关.但是,有谁知道我如何使HTML小部件的行为像在程序生成的Rmarkdown中的情节一样?

示例.Rmd

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
ex_plot <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length,Sepal.Width)) + 
    ggplot2::geom_point()

gen_rmarkdown_widget_last <- function() {
    cat("# Head 1\n\n")
    DT::datatable(iris)
}

gen_rmarkdown_plots <- function() {
    cat("# Head 1\n\n")
    print(ex_plot)
    cat("# Head 2\n\n")
}

gen_rmarkdown_widgets <- function() {
    cat("# Head 1\n\n")
    print(DT::datatable(iris))

    # tried loading from file
    # tmp <- tempfile()
    # htmlwidgets::saveWidget(DT::datatable(iris), tmp)
    # knitr::include_url(tmp)
    
    # tried a different widget
    # print(plotly::ggplotly(ex_plot))

    cat("# Head 2\n\n")
}

```

```{r, results='asis'}
# works fine
gen_rmarkdown_widget_last()
```

```{r, results='asis'}
# works fine
gen_rmarkdown_plots()
```

```{r, results='asis'}
# Can't have an HTML widget if it is followed by other things
gen_rmarkdown_widgets()
```

解决方案

已在此处讨论了该问题.. >

手动添加依赖项是通过以下方式完成的:

data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

在您提供的示例中:

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
library(dplyr)
#load dependencies
data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

ex_plot <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length,Sepal.Width)) + 
    ggplot2::geom_point()



gen_rmarkdown_widget_last <- function() {
    cat("# Head 1\n\n")
    DT::datatable(iris)
}

gen_rmarkdown_plots <- function() {
    cat("# Head 1\n\n")
    print(ex_plot)
    cat("# Head 2\n\n")
}

gen_rmarkdown_widgets <- function() {
    cat("# Head 1\n\n")
    cat(knitr::knit_print(DT::datatable(iris)))
    cat("\n  \n")
    cat("# Head 2\n\n")
}

```


```{r, results='asis'}
gen_rmarkdown_widgets()
```

I'm trying to programmatically generate some Rmarkdown and one of the sections contains an HTML widget. These are output fine if they are last in my function. However, if I wrap them in a print so I can put something else after them as you would do for a plot they do not produce any output.

Perhaps this some something to do with the way knitr handles printing i'm not sure. But does anyone know how I can get HTML widgets to behave like plots do in programmatically generated Rmarkdown?

Example .Rmd

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
ex_plot <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length,Sepal.Width)) + 
    ggplot2::geom_point()

gen_rmarkdown_widget_last <- function() {
    cat("# Head 1\n\n")
    DT::datatable(iris)
}

gen_rmarkdown_plots <- function() {
    cat("# Head 1\n\n")
    print(ex_plot)
    cat("# Head 2\n\n")
}

gen_rmarkdown_widgets <- function() {
    cat("# Head 1\n\n")
    print(DT::datatable(iris))

    # tried loading from file
    # tmp <- tempfile()
    # htmlwidgets::saveWidget(DT::datatable(iris), tmp)
    # knitr::include_url(tmp)
    
    # tried a different widget
    # print(plotly::ggplotly(ex_plot))

    cat("# Head 2\n\n")
}

```

```{r, results='asis'}
# works fine
gen_rmarkdown_widget_last()
```

```{r, results='asis'}
# works fine
gen_rmarkdown_plots()
```

```{r, results='asis'}
# Can't have an HTML widget if it is followed by other things
gen_rmarkdown_widgets()
```

解决方案

The issue has been discussed here.

Adding the dependencies manually is done with:

data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

In the example you provided:

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
library(dplyr)
#load dependencies
data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

ex_plot <- ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length,Sepal.Width)) + 
    ggplot2::geom_point()



gen_rmarkdown_widget_last <- function() {
    cat("# Head 1\n\n")
    DT::datatable(iris)
}

gen_rmarkdown_plots <- function() {
    cat("# Head 1\n\n")
    print(ex_plot)
    cat("# Head 2\n\n")
}

gen_rmarkdown_widgets <- function() {
    cat("# Head 1\n\n")
    cat(knitr::knit_print(DT::datatable(iris)))
    cat("\n  \n")
    cat("# Head 2\n\n")
}

```


```{r, results='asis'}
gen_rmarkdown_widgets()
```

这篇关于以编程方式生成的Rmarkdown中的“打印" HTML窗口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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