2

I am unable to generate an html file, from rmarkdown, which displays two or more ggplotly plots created inside an if block in a given code chunk.

My MWE rmarkdown source code follows:

---
title: Several ggplotly figures from within if block in rmarkdown
author: "Mauricio Calvao"
date: "February 27, 2017"
output:
    html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(ggplot2)
library(plotly)
```


## Outside if block:

I can have rmarkdown generate an html document with two ggplotly plots, outside of an if block in a given chunk:

```{r}
ggplotly(qplot(data = pressure, x = temperature, y = pressure))
ggplotly(qplot(data = pressure, x = pressure, y = temperature))
```

## Inside if block:

However, when I try the same code inside an if block, only the last plot shows up in the html document:

```{r}
if (TRUE) {
ggplotly(qplot(data = pressure, x = temperature, y = pressure))
ggplotly(qplot(data = pressure, x = pressure, y = temperature))
}
```



**How do I print two (or more plots, for that matter) from within an if block in the html document???**

The above is a MWE, but, in fact, in a more general setting I would have a large number of plots to be printed depending on several if else blocks...

Any suggestions are welcome!!

Mauricio Calvao
  • 475
  • 5
  • 14
  • Maybe you could name each of your plot and use subplot to display two graphs for each of your page – MLavoie Feb 28 '17 at 07:52
  • Could [this](http://stackoverflow.com/questions/37013876/interactive-ggplotly-graph-is-not-ploted-from-inside-for-loop-in-rmd-file?rq=1) question be relevant? Does the plot appear if you call it from within an `if` in a simple R script, not an Rmd chunk? – juod Feb 28 '17 at 09:22
  • @MLavoie I tried subplot but could not manage for it to have a working layout: the legends of the 2 plots got mixed up> Besides, I would really like to have separate plots shown... – Mauricio Calvao Feb 28 '17 at 14:26
  • @juod: outside the if block both plots show up in the Viewer pane of RStudio (the environment I use), whereas inside the if block only the last one shows up in the Viewer :-( – Mauricio Calvao Feb 28 '17 at 14:30
  • @juod: the link you referred to above has a very contrived workaround. If no other cleaner suggestion appears, I might try it, though unwillingly... – Mauricio Calvao Feb 28 '17 at 14:33
  • I see. I don't have any specific solution for `plotly` in mind. Maybe splitting the plot into two chunks could be a non-elegant option? i.e. `{r} if (){ ggplotly } {r} if (){ ggplotly }`? – juod Feb 28 '17 at 14:40
  • Worst case scenario would be making the plots separately, saving them somewhere, and retrieving the PNGs - sounds ugly, but I find that in practice it sometimes gives easier control on the size and placement of the resulting images, esp. if you want some tricky arrangement of figure parts. – juod Feb 28 '17 at 14:45
  • I think the workaround described in the github issue [comments](https://github.com/ropensci/plotly/issues/273) would likely do the trick. Something like: `l <- htmltools::tagList() for (i in 1:3) { l[[i]] <- plot_ly(x = rnorm(10)) } l`, but generalized to your context with an if statement? – corinne r Feb 28 '17 at 18:09

1 Answers1

1

The answer is now available from the plotly reference. In short, you either use:

  • subplot:
    if (TRUE) {
     p1= ggplotly(qplot(data = pressure, x = temperature, y = pressure))
     p2= ggplotly(qplot(data = pressure, x = pressure, y = temperature))
     subplot(p1, p2)
    }
    
  • or taglist
    if (TRUE) {
     p1= ggplotly(qplot(data = pressure, x = temperature, y = pressure))
     p2= ggplotly(qplot(data = pressure, x = pressure, y = temperature))
     htmltools::tagList(list(p1, p2))
    }
    
rvezy
  • 555
  • 4
  • 15
  • I do not understand the reason why not the whole block is executed. Moreover, the given link does not explain anything regarding the OP question. – meolic Sep 18 '19 at 07:25