I have written a simulation in R that I want to visualize with shiny now. I have put the main part of the simulation into an observe block in order to be evaluated. During this evaluation process, i.e. for every iteration I want to plot the current status. The question is how do I achive this, since in my actual code the rendering of the plot is just executed after the main observer has been evaluated. Is there a way to, for instance, suspend the execution of the observe block and resume it after the plot has been updated?
Shouldn't there be some more functionality from shiny to address such a case, since I could imagine that I'm not the only one who would like to do something like this?!
It would be nice if you can help me with this :)
Below is some skeleton code for the server and ui.
ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("... Simulation"),
sidebarPanel(
sliderInput("epochs",
"Number of Epochs:",
min = 1,
max = 100,
value = 10),
verbatimTextOutput("curr.iter"),
actionButton("actionB", "Action!")
),
mainPanel(
plotOutput("distPlot")
)
))
server.R:
library(shiny)
sinus <- data.frame()
shinyServer(function(input, output) {
dummy <- reactiveValues(iter=0)
obsMain <- observe({
for (i in 1:input$epochs) {
cat(i, " ")
x <- seq(1:input$epochs)
y <- sin(x)
sinus <<- data.frame(x, y)
dummy$iter <- i
#
# At this time I want distPlot & curr.iter to be evaluated/updated!
#
Sys.sleep(1)
}
}, suspended=TRUE)
obsAction <- observe({ if(input$actionB > 0) obsMain$resume() }) # Helps to avoid initial evaluation of obsMain...
output$curr.iter <- renderText({ as.numeric(dummy$iter) })
output$distPlot <- renderPlot({ if (dummy$iter > 1) plot(sinus, type="l") })
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…