-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathapp.R
61 lines (47 loc) · 1.42 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
library(shiny)
source("create_forked_task.R")
ui <- fluidPage(
shinyjs::useShinyjs(), # Initialize shinyjs library
# Buttons to control job
actionButton("start", "Start"),
shinyjs::disabled(actionButton("stop", "Stop")),
# This will display the job output
tableOutput("out")
)
server <- function(input, output, session) {
# Make "task" behave like a reactive value
makeReactiveBinding("task")
task <- NULL
output$out <- renderTable({
# The task starts out NULL but is required. The req() takes
# care of ensuring that we only proceed if it's non-NULL.
req(task)$result()
})
observeEvent(input$start, {
shinyjs::enable("stop")
shinyjs::disable("start")
task <<- create_forked_task({
# Pretend this takes a long time
Sys.sleep(5)
cars[sample(nrow(cars), 10),]
})
# Show progress message during task start
prog <- Progress$new(session)
prog$set(message = "Executing task, please wait...")
o <- observe({
# Only proceed when the task is completed (this could mean success,
# failure, or cancellation)
req(task$completed())
# This observer only runs once
o$destroy()
# Close the progress indicator and update button state
prog$close()
shinyjs::disable("stop")
shinyjs::enable("start")
})
})
observeEvent(input$stop, {
task$cancel()
})
}
shinyApp(ui, server)