I created a shiny app with a dynamic number of plots, similar to this 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).
I had a look at the tutorial for interactive plots. However, I cannot figure out where to put the listener 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:
createPlots <- reactive ({
numberOfFiles <- 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 <- i
plotname <- paste("densityPlot", my_i, sep="")
result <- densityResult[[my_i]]
output[[plotname]] <- renderPlot({
plot(result$data, main=my_i, pch=19,cex=0.2, col= ColoursUsed[result$clusters])
dev.off()
})
}
})
})
output$plots <- renderUI({
createPlots()
plot_output_list <- lapply(1:length(filesAnalyzed), function(i) {
plotname <- 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)
})
I created a text output for testing, which should display the id of the selected plot:
output$click_info <- renderUI({
numberOfPlots <- length(densityResult)
lapply(1:numberOfPlots, function(i) {
renderText(input[[paste("densityPlot", names(densityResult)[i], "_click", sep = "")]])
})
})
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 after the plots have been generated?
Edit: A minimal example can be found here.