title: How to print HTML interactive plotly graphs within a lapply loop using knitr render from a .R source file ?
body: <p>Within Rstudio, I try to dynamically render plotly plots into an HTML report using knitr render from a .R source file. knitr renders the plotly plots as long as they are not within a lapply loop. But once I try to output them from within a lapply loop they are note rendered. I need to use a loop because I need to programatically build my HTML reports with various numbers of plots with various names.</p>
  
  <p>Here is the command that I execute in the console to create the HTML report from my <code>plotlytest.R</code>  source file : </p>
  
  <p><code>render("plotlytest.R")</code></p>
  
  <p>and here is my example source file called <code>plotlytest.R</code> : </p>
  
  <pre><code>#'---
  #'author: "pokyah"
  #'output: 
  #'  html_document:
  #'    theme: flatly
  #'    toc: true
  #'    toc_depth: 6
  #'    toc_float:
  #'      collapsed: false
  #'      smooth_scroll: true
  #'title: "Plotly test"
  #'date: \`r format(Sys.Date(), " %d-%m-%Y")`\
  #'---
  #'
  #
  
  #+ ---------------------------------
  #' ## Loading libraries
  library(plotly)
  library(ggplot2)
  
  #+ ---------------------------------
  #' ## plotly example
  #+ plotly,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE,results='asis', plotly=TRUE
  
  mtcars.p &lt;- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point() +
    labs(colour = "Cylinders")
  
  
  iris.p &lt;- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) + geom_point() +
    labs(colour = "Cylinders")
  
  myPlots.l &lt;- list(mtcars.p, iris.p)
  names(myPlots.l) &lt;- c("mtcars", "iris")
  
  printPlots &lt;- function(p, plots){
    cat("\n")
    cat("###", names(plots[p]))
    cat("\n")
    ggplotly(plots[[p]]) # this is not rendered
    cat("\n")
    print (plots[[p]]) # this is rendered, but of course this is not an interactive plot
    cat("\n")
  }
  
  cat("\n")
  cat("## printing out of lapply -&gt; plotly working")
  
  cat("\n")
  ggplotly(myPlots.l$mtcars) # this is rendered
  
  cat("\n")
  ggplotly(myPlots.l$iris) # this is also rendered
  cat("\n")
  
  cat("\n")
  cat("## printing inside of lapply -&gt; plotly not working")
  cat("\n")
  lapply(seq_along(myPlots.l), printPlots, myPlots.l )
  </code></pre>
  
  <p>The closer question to my problem is <a href="https://stackoverflow.com/questions/45742115/embedding-plotly-graphs-in-a-rmarkdown-document-using-sourcefilename-r?rq=1">this</a> one but I'm still stuck with this problem. Any idea how to solve this ? </p>
  
  <p>here is my config : </p>
  
  <pre><code>R version 3.3.1 (2016-06-21)
  Platform: x86_64-suse-linux-gnu (64-bit)
  Running under: openSUSE Leap 42.2
  </code></pre>