title: R Markdown Shiny renderPlot list of plots from lapply
body: <p>I am developing an R Markdown Shiny document to:</p>
  
  <ol>
  <li>Subset a data frame to include the "date" column and some numeric data columns. The way the shiny user input is set up, you select radio buttons for the data columns to include, then hit the "Subset Data" button to create <code>d()</code> - NO PROBLEM:)</li>
  <li>Generate a list of plots (<code>plotList</code>), one for each numeric data column (plotted against the date column). I am using the <code>openair</code> package <code>timePlot</code> function to generate the plots, and <code>lapply</code> to generate the list of plot objects (<code>plotList</code>) - NO PROBLEM:)</li>
  <li>use <code>renderPlot</code> to output all the plots in <code>plotList</code> to the R Markdown document - PROBLEM:(</li>
  </ol>
  
  <p>I know there have been similar questions (e.g <a href="https://gist.github.com/wch/5436415/" rel="nofollow noreferrer">https://gist.github.com/wch/5436415/</a>, <a href="https://stackoverflow.com/questions/27930144/reactivity-in-r-shiny-with-toy-example">Reactivity in R shiny with toy example</a>, and <a href="https://stackoverflow.com/questions/15875786/dynamically-add-plots-to-web-page-using-shiny">dynamically add plots to web page using shiny</a>), and please believe me I have tried and tried (e.g. using a for loop in stead of lapply-not my preference, but if it worked then who cares; adding local() and/or observe(); etc). No matter what I do I can't get it to to work. I am new to R Markdown and to Shiny, I just can't figure this out - please help!</p>
  
  <p>Here is a reproducible example (to be run as an R markdown shiny document). </p>
  
  <p>First the chunk that creates a reactive dataset <code>d()</code>:</p>
  
  <pre><code>```{r reactive-dataset, echo=FALSE,message=FALSE}
  library(openair)
  library(dplyr)
  data&lt;-mydata[1:50,]
  print(tbl_df(data))
  
  inputPanel(
    checkboxGroupInput(inputId="p",
                  label="select pollutants to plot",
                  choices=names(data)[-1]
                ),
    actionButton(inputId="import",
                 label="Subset Data")
  )
  
  
  d&lt;-eventReactive(input$import,{
    d&lt;-data %&gt;% select(date,one_of(input$p))
    })
  
  renderPrint({tbl_df(d())})
  ```
  </code></pre>
  
  <p>Now the second chunk, to create <code>plotList</code> and output it (PART THAT DOESN'T WORK):</p>
  
  <p>Attempt 1: only last plot is displayed</p>
  
  <pre><code> ```{r plot,echo=FALSE,message=FALSE}
   renderPlot({
    i&lt;-names(d())[-1]
    tp&lt;-function(x){
      p&lt;-timePlot(d(),
           pollutant=print(x),
           main="Minute Validation",
           ylab="Minute Conc. (ug/m3 or ppb)",
           key=T)
      p$plot
      }
    lapply(i,tp)
  
    }) 
    ``` 
  </code></pre>
  
  <p>Attempt 2 (based on <a href="https://stackoverflow.com/questions/27930144/reactivity-in-r-shiny-with-toy-example">Reactivity in R shiny with toy example</a>). No plots are displayed</p>
  
  <pre><code>```{r plot,echo=FALSE,message=FALSE}
  plotList&lt;-reactive({
    i&lt;-names(d())[-1]
    tp&lt;-function(x){
      p&lt;-timePlot(d(),
           pollutant=print(x),
           main="Minute Validation",
           ylab="Minute Conc. (ug/m3 or ppb)",
           key=T)
      p$plot
      }
    lapply(i,tp)
    })
  
  
  observe({
    for (j in 1:length(plotList())){
      local({
        my_j&lt;-j
        renderPlot({plotList()[[my_j]]})
  
        })#end local
      } #end for loop
  }) #end observe
  ```
  </code></pre>
  
  <p>I have fiddled with this endlessly, referring the to similar questions that I have linked to above.</p>