动态遍历htmlwidgets并为RMarkdown添加编织格式 [英] Dynamically loop through htmlwidgets and add knitr formatting for RMarkdown

查看:61
本文介绍了动态遍历htmlwidgets并为RMarkdown添加编织格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建并循环遍历htmlwidgets(例如DTplotlyrbokeh)以生成自动编织报告.有没有一种方法可以将knitr格式(例如tabset)添加到此github问题tagList方法中rel ="nofollow"> https://github.com/ramnathv/htmlwidgets/pull/110 ?我也在这里张贴了这个问题.

I am trying to dynamically create and loop through htmlwidgets such as DT, plotly, or rbokeh to generate an automated knitr report. Is there a way to add knitr formatting, such as the tabset, into the tagList approach outlined in this github question https://github.com/ramnathv/htmlwidgets/pull/110? I have also posted this question there.

下面是我在想的一些示例代码,但效果不佳.我想做的是创建10个选项卡,每个选项卡都包含从plot_list生成的绘图的副本.现在发生的是所有图都进入最后一个选项卡.实际上,plot_list将具有不同的图/表.

Below is some example code of what I am thinking, but it does not quite work. What I am trying to do is create 10 tabs, each with a copy of the plot generated from plot_list. What happens right now is all of the plots go into the last tab. In practice, plot_list would have different plots/tables.

#' ---
#' title: htmltools::tagList test
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
library(plotly)
library(printr)

plot_list = lapply(1:10, 
                   function(i){ 
                     as.widget(plot_ly(iris, 
                                       x = iris[["Sepal.Length"]],
                                       y = iris[["Sepal.Width"]], 
                                       mode = "markers")) 
                    } 
                  )

htmltools::tagList( lapply(1:10, 
                            function(i) {
                              pandoc.header(paste0("Tab",i,' {.tabset}'), 2)
                              plot_list[[i]]
                            } 
                          )
                   )

# rmarkdown::render("your_path/htmltoolsTagList_test.r")

之前,我已经使用嵌套的for循环成功完成了类似的操作,但是一旦我尝试使用具有HTML依赖项的图形,这些图形当然就不再呈现,因为它们不再是顶级表达式. knitr是否可以像这样循环?

Before, I was successfully doing something like this with nested for-loops, but once I tried using figures with HTML dependencies, the figures of course do not render as they are no longer top level expressions. Is it possible in knitr to loop like this?

我有一个后续问题:假设我想将这些选项卡嵌套到以相同方式创建的另一组选项卡中,这可能吗?我要问的是,是否可以使用类似于嵌套的for循环的这种方法动态嵌套选项卡?

A follow up question I have is: suppose I wanted to nest these tabs into another set of tabs created the same way, is that possible? What I mean to ask is, can I nest tabs dynamically using a method like this, analogous to a nested for-loop?

我仍在学习如何使用knitr,希望对您有所帮助! 谢谢!

I am still learning how to use knitr, and would appreciate any help! Thank you!

推荐答案

我将在下面复制对Github问题的答复.

I'll copy my response to the Github issue below.

很好的问题,我认为其他人将从这次讨论中获得帮助.可能最简单的方法是从头开始构建类似您在没有rmarkdown的情况下提出的建议.

Good question, and I think others will be helped by this discussion. It might be easiest to start by building something like what you propose from scratch without the aid of rmarkdown.

# https://github.com/ramnathv/htmlwidgets/pull/110#issuecomment-216562703

library(plotly)
library(htmltools)
library(markdown)
library(shiny)

browsable(
  attachDependencies(
    tagList(
      tags$div(
        class="tabs",
        tags$ul(
          class="nav nav-tabs",
          role="tablist",
          tags$li(
            tags$a(
              "data-toggle"="tab",
              href="#tab-1",
              "Iris"
            )
          ),
          tags$li(
            tags$a(
              "data-toggle"="tab",
              href="#tab-2",
              "Cars"
            )
          )
        ),
        tags$div(
          class="tab-content",
          tags$div(
            class="tab-pane active",
            id="tab-1",
            as.widget(
              plot_ly(
                iris,
                x = iris[["Sepal.Length"]],
                y = iris[["Sepal.Width"]], 
                mode = "markers"
              )
            )
          ),
          tags$div(
            class="tab-pane",
            id="tab-2",
            as.widget(
              plot_ly(
                cars,
                x = speed,
                y = dist, 
                mode = "markers"
              )
            )
          )
        )
      )
    ),
    # attach dependencies
    #  see https://github.com/rstudio/rmarkdown/blob/master/R/html_document.R#L235
    list(
      rmarkdown::html_dependency_jquery(),
      shiny::bootstrapLib()
    )
  )
)

在rmarkdown中

也许有更好的方法可以完成这项工作,但是直到有人将我弄清楚之前,我们可以从上面采用此方法,并在rmarkdown中使用它.不幸的是,这仍然是非常手动的.有关更多参考,请参见代码 RStudio用于构建tabsets.

in rmarkdown

There is probably a better way to make this work, but until someone sets me straight, we can take the approach from above and use it in rmarkdown. Unfortunately, this is still very manual. For more reference, here is the code that RStudio uses to build tabsets.

---
title: "tabs and htmlwidgets"
author: "Kent Russell"
date: "May 3, 2016"
output: html_document
---

```{r echo=FALSE, message=FALSE, warning=FALSE}
library(plotly)
library(htmltools)
library(magrittr)

# make a named list of plots for demonstration
#  the names will be the titles for the tabs
plots <- list(
  "iris" = plot_ly(
    iris,
    x = iris[["Sepal.Length"]],
    y = iris[["Sepal.Width"]], 
    mode = "markers"
  ),
  "cars" = plot_ly(
    cars,
    x = speed,
    y = dist, 
    mode = "markers"
  )
)

# create our top-level div for our tabs
tags$div(
  # create the tabs with titles as a ul with li/a
  tags$ul(
    class="nav nav-tabs",
    role="tablist",
    lapply(
      names(plots),
      function(p){
        tags$li(
          tags$a(
            "data-toggle"="tab",
            href=paste0("#tab-",p),
            p
          )
        )
      }
    )
  ),
  # fill the tabs with our plotly plots
  tags$div(
    class="tab-content",
    lapply(
      names(plots),
      function(p){
         tags$div(
          #  make the first tabpane active
          class=ifelse(p==names(plots)[1],"tab-pane active","tab-pane"),
          #  id will need to match the id provided to the a href above
          id=paste0("tab-",p),
          as.widget(plots[[p]])
        )
      }
    )
  )
) %>%
  # attach the necessary dependencies
  #  since we are manually doing what rmarkdown magically does for us
  attachDependencies(
    list(
      rmarkdown::html_dependency_jquery(),
      shiny::bootstrapLib()
    )
  )
```

这篇关于动态遍历htmlwidgets并为RMarkdown添加编织格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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