hrbrmstr/vegalite

Rather a question than an issue

Closed this issue · 6 comments

I wonder how to "print" several vegalite plots in a loop in RMarkdown? I tried using print() and results='asis' without success.

Hrm. I'll take a look later today. Thx for testing it out!

I am admitting defeat. Shining the Bat-Signal to see if @timelyportfolio has any ideas.

I'm sorry but at the same time I'm happy to see I wasn't missing something obvious. ;-) Thanks a lot for trying this! And the pkg is great anyway!

Hard to say without any code, but my guess is wrapping in a tagList from htmltools will potentially solve the problem.

---
title: "vegalite multiple"
author: "Kent Russell"
date: "February 29, 2016"
output: html_document
---
Looping through `htmlwidgets` in `rmarkdown` often confounds users even some `R` greats (see [pull](https://github.com/ramnathv/htmlwidgets/pull/110)).  `tagList` from `htmltools` is the secret, and hopefully it won't stay "secret".
Let's answer `vegalite` [issue #3](https://github.com/hrbrmstr/vegalite/issues/3)
```{r}
library(htmltools)
library(vegalite)
```
The issue did not include code so let's make something up.
```{r}
# split mtcars by cyl
#  we'll plot each of these in a loop
split_cyl <- split(mtcars,mtcars$cyl)
tagList(
  lapply(
    split_cyl,
    function(cyl){
      vegalite() %>%
        add_data(cyl) %>%
        encode_x("mpg", "quantitative") %>%
        encode_y("hp", "quantitative") %>%
        mark_point()
    }
  )
)
```
Spread the word, use `tagList` :)

Many thanks @timelyportfolio, it works! (but @hrbrmstr I'll let you close this)

How to spread the word? ;-)

(a) Nice find @timelyportfolio (and I see yihui is saying this is the generalized answer as well)
(b) @masalmon I think I'll update the docs (in all my widgets :-) with this.
(c) Kenton: we should get all the folks who blog on widgets to do a mass spread-the-word via blogs :-)