I am trying to dynamically render multiple plots in a tab (better if this could be done across multiple tabs). After some search, I found this post is very helpful. But in my case, the number of plots is determined by the uploaded CSV file. So I think the question is how to call plotInput()$n_plot
in a for loop? I appreciate any suggestions!
At this moment, I am able to create multiples <div>s
by calling renderUI
.
<div id="plot1" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
<div id="plot2" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
But I could not properly call the reactive function in the for (i in 1:plotInput()$n_plot)
loop. The error message is:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context.
shinyServer(function(input, output) {
### This is the function to break the whole data into different blocks for each page
plotInput <- reactive({
get_the_data()
return (list("n_plot"=n_plot, "total_data"=total_data))
})
##### Create divs######
output$plots <- renderUI({
plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
do.call(tagList, plot_output_list)
})
# Call renderPlot for each one.
####This is the place caused the error##
for (i in 1:plotInput()$n_plot) {
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
hist(plotInput()$total_data[i])
})
})
}
})
If I wrap the for loop inside a reactive
function and call it as part of renderUI
, the loop works, but the plot is missing, I guess this is because a renderUI
only create HTML tags, it will not assign images to the generated tags.
output$plots <- renderUI({
plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
do.call(tagList, plot_output_list)
plot_concent()
})
# Call renderPlot for each one.
####This is the place caused the error##
plot_concent<-reactive({
for (i in 1:plotInput()$n_plot) {
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
hist(plotInput()$total_data[i])
})
})
}
})
If anyone's still interested in an answer, try this:
library(shiny)
runApp(shinyApp(
ui = shinyUI(
fluidPage(
numericInput("number", label = NULL, value = 1, step = 1, min = 1),
uiOutput("plots")
)
),
server = function(input, output) {
### This is the function to break the whole data into different blocks for each page
plotInput <- reactive({
n_plot <- input$number
total_data <- lapply(1:n_plot, function(i){rnorm(500)})
return (list("n_plot"=n_plot, "total_data"=total_data))
})
##### Create divs######
output$plots <- renderUI({
plot_output_list <- lapply(1:plotInput()$n_plot, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
do.call(tagList, plot_output_list)
})
observe({
lapply(1:plotInput()$n_plot, function(i){
output[[paste("plot", i, sep="") ]] <- renderPlot({
hist(plotInput()$total_data[[i]], main = paste("Histogram Nr", i))
})
})
})
}
))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With