title: Dynamically create interactive plots with shiny
body: <p>I created a shiny app with a dynamic number of plots, similar to <a href="https://stackoverflow.com/questions/15875786/dynamically-add-plots-to-web-page-using-shiny">this</a> question. It should serve as an overview over a number of results - and now I would like to add the possibility to (double) click on one plot, in order to do something (e.g. display only this one with more details). </p>
  
  <p>I had a look at the tutorial for <a href="http://shiny.rstudio.com/articles/plot-interaction.html" rel="nofollow noreferrer">interactive plots</a>. However, I cannot figure out where to put the <em>listener</em> for the click input events in my code. They always return NULL, which I presume is due to the fact that the code is executed, before the plots are being generated. Here is how I create the plots:</p>
  
  <pre><code>createPlots &lt;- reactive ({
    numberOfFiles &lt;- length(densityResult)
  
    if (numberOfFiles == 0)
      return(NULL)
    # Call renderPlot for each one. Plots are only actually generated when they
    # are visible on the web page.
    for (i in filesAnalyzed) {
      # Need local so that each item gets its own number. Without it, the value
      # of i in the renderPlot() will be the same across all instances, because
      # of when the expression is evaluated.
      local({
        my_i &lt;- i
        plotname &lt;- paste("densityPlot", my_i, sep="")
        result &lt;- densityResult[[my_i]]
        output[[plotname]] &lt;- renderPlot({
          plot(result$data, main=my_i, pch=19,cex=0.2, col= ColoursUsed[result$clusters])
          dev.off()
        })
      }
    })
  })
  
  output$plots &lt;- renderUI({
  
    createPlots()
  
    plot_output_list &lt;- lapply(1:length(filesAnalyzed), function(i) {
      plotname &lt;- paste("densityPlot", filesAnalyzed[i], sep="")
      plotOutput(plotname, dblclick = dblclickOpts(id = paste(plotname, "_click", sep = "")))
    })
  
    # Convert the list to a tagList - this is necessary for the list of items
    # to display properly.
    do.call(tagList, plot_output_list)
  })
  </code></pre>
  
  <p>I created a text output for testing, which should display the <em>id</em> of the selected plot:</p>
  
  <pre><code>  output$click_info &lt;- renderUI({
      numberOfPlots &lt;- length(densityResult)
      lapply(1:numberOfPlots, function(i) {
        renderText(input[[paste("densityPlot", names(densityResult)[i], "_click", sep = "")]])
      }) 
    })
  </code></pre>
  
  <p>However, as I said before, the result is either NULL or, like in this case, the element is just not being diplayed at all. (I tried different versions, even hard-coding the name of the first input element). Using RStudios debugging, I could follow up that the code is run before the plots are created. How can I access the input elements <strong>after</strong> the plots have been generated?</p>
  
  <p><em>Edit: A minimal example can be found <a href="https://benedikt.shinyapps.io/Test/" rel="nofollow noreferrer">here</a>.</em></p>