title: Plotly charts in a for loop
body: <p>I am converting ggplot2 charts using plotly and putting them in a knitr html output.
  Here is a working copy of the report. 
  <a href="http://rpubs.com/kausti/NSEFO-23-Mar-2016" rel="noreferrer">http://rpubs.com/kausti/NSEFO-23-Mar-2016</a>
  The first chart is plotly chart and the following ones are not. I would like them to be plotly charts but I am stuck.</p>
  
  <p>In the code below, plotChart function returns a ggplot2 plot. This code works!</p>
  
  <pre><code>for (tick in tradeOps$ticker)
  {
    tmpDF = filter(tradeOps, ticker == tick)
    p = plotChart(tick = tick,Entry = tmpDF$Entry, Target =     tmpDF$Target, SL= tmpDF$SL, TradeType = tmpDF$TradeType, data = wd)
    p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y =   tmpDF$Entry, label = tmpDF$Entry, colour = "blue")
    p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$Target, label = tmpDF$Target)
    p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$SL, label = tmpDF$SL)
  
    print(p)
  
  }
  </code></pre>
  
  <p>Now if I change the last print statement to <code>print(ggplotly(p))</code> it just won't print the charts in html.
  The code works outside knitr perfectly. </p>
  
  <p>Also, <code>print(ggplotly(p))</code> works perfectly outside loops.
  Am I missing something?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Including a reproducible example below. 
  Following is my rmd file.</p>
  
  <pre><code>---
  title: "tmp"
  output: html_document
  ---
  
  ```{r, echo=FALSE, message=FALSE, fig.width=8, fig.height=6}
  library(ggplot2)
  library(plotly)
  library(dplyr)
  s = NULL
  a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
  for(i in unique(a$z)){
    s = a[a$z == i,]
    print(ggplot(s,aes(x = x, y = y)) + geom_point())
  }
  
  ```
  </code></pre>
  
  <p>This works perfectly with the output html showing three graphs.
  Now, just changing the last print statement to 
  <code>print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))</code> produces a blank html without any errors.</p>
  
  <p>Running the same code in terminal works perfectly though</p>
  
  <pre><code>library(ggplot2)
  library(plotly)
  library(dplyr)
  s = NULL
  a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
  for(i in unique(a$z)){
    s = a[a$z == i,]
    print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))
  }
  </code></pre>
  
  <p>Appreciate any help.</p>
  
  <p>Thanks,
  Kaustubh</p>