-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync-async-callr.R
120 lines (98 loc) · 2.3 KB
/
sync-async-callr.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
library(shiny)
library(callr)
# functions
long_job <- function() {
Sys.sleep(10)
return(TRUE)
}
# modules
# 1. sync
sync_ui <- function(id) {
ns <- NS(id)
tagList(
wellPanel(
h4("Run long job:"),
actionButton(ns("start"), "synchronously"),
textOutput(ns("did_it_work"))
)
)
}
sync_srv <- function(input, output, session) {
long_run <- eventReactive(input$start, {
long_job()
return("Sync job completed")
})
output$did_it_work <- renderText({
long_run()
})
}
# 2. async background
background_ui <- function(id) {
ns <- NS(id)
tagList(
wellPanel(
h4("Run long job:"),
actionButton(ns("start"), "in background"),
textOutput(ns("did_it_work"))
)
)
}
background_srv <-
function(input, output, session) {
long_run <- eventReactive(input$start, {
x <- r_bg(
func = long_job,
supervise = TRUE
)
return(x)
})
check <- reactive({
if (long_run()$is_alive()) {
invalidateLater(millis = 1000, session = session)
x <- "Job running in background"
} else {
x <- "Async job in background completed"
}
return(x)
})
output$did_it_work <- renderText({
check()
})
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
tags$hr(),
sync_ui("sync"),
tags$hr(),
background_ui("bg")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
callModule(sync_srv, id = "sync")
callModule(background_srv, id = "bg")
}
# Run the application
shinyApp(ui = ui, server = server)