In my shiny app, I have a function that uses a solver to estimate a value. While the user is waiting, I want them to see the guesses from the solver plotted as the solver tries them. I can do this in R by just putting points()
inside the function being solved.
When I put the function in a plotOutput
object in shiny, it shows the plot all at once in the app. Can anyone advise on the best way to get the points()
to show up as they're being generated? I'm open to anything!
Below is a toy example:
#> Toy example
library(shiny)
liveplot <- function(L = 5){
plot(1:L,1:L,type='n')
for(i in 1:L){
p <- rnorm(1, 5, 1)
Sys.sleep(time = 0.5)
points(i,p)
}
}
# to see the "animated" plotting I'm looking for, just run `liveplot()` alone.
library(shiny)
# UI
ui <- fluidPage(
titlePanel("Incremental plotting"),
sidebarLayout(
sidebarPanel(
sliderInput("length_i",
"Steps to plot:",
min = 1,
max = 10,
value = 6)
),
# Plot where I would like to show points being plotted as the function generates them:
mainPanel(
plotOutput("livePlot")
)
)
)
# Server
server <- function(input, output) {
output$livePlot <- renderPlot({
liveplot(L = input$length_i)
})
}
shinyApp(ui = ui, server = server)