I am trying to develop a shiny app where the user can upload csv files, which are subsequently analyzed by my R script. Therefore, I would like display a dynamic number of plots, based on the number of files that are processed (one plot for each file).
I found this question extremly helpful, but I don't know the maximum number of plots in advance. Here is what I tried:
shinyServer(function(input, output) {
# Insert the right number of plot output objects into the web page
output$densityPlots <- renderUI({
plot_output_list <- lapply(1:length(input$file1$datapath), function(i) {
plotname <- paste("densityPlot", i, sep="")
plotOutput(plotname)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:length(input$file1$datapath)) {
# 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="")
output[[plotname]] <- renderPlot({
plot(1)
})
})
}
}
However, it gives me this error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
I tried to put the for-loop inside the output function, but then no plots are created at all.