-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_ex.R
79 lines (68 loc) · 1.71 KB
/
async_ex.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
library("shiny")
library("promises")
library("dplyr")
library("future")
plan(multiprocess)
# A function to simulate a long running process
# write.csv(mtcars, "data.csv")
read_csv_async = function(sleep, path){
log_path = "./mylog.log"
pid = Sys.getpid()
write(
x = paste(format(Sys.time(), "%Y-%m-%d %H:%M:%OS"), "pid:", pid, "Async process started"), file = log_path, append = TRUE)
Sys.sleep(sleep)
df = read.csv(path)
write(
x = paste(format(Sys.time(), "%Y-%m-%d %H:%M:%OS"), "pid:", pid, "Async process work completed\n"), file = log_path, append = TRUE)
df = read.csv(path)
df
}
ui <- fluidPage(
actionButton(
inputId = "submit_and_retrieve",
label = "Submit short async analysis"
),
br(),
br(),
tableOutput("user_content"),
br(),
br(),
br(),
hr(),
sliderInput(
inputId = "hist_slider_val",
label = "Histogram slider",
value = 25,
min = 1,
max = 100
),
plotOutput("userHist")
)
server <- function(input, output){
parent_pid = Sys.getpid()
# When button is clicked
# load csv asynchronously and render table
data <- reactiveVal()
observeEvent(input$submit_and_retrieve, {
future({ read_csv_async(2, "./data.csv") }) %...T>%
{data(NULL)} %...>%
data() %...!% # Assign to data
(function(e) {
data(NULL)
warning(e)
session$close()
})
# Hide the async operation from Shiny by not having the promise be
# the last expression.
NULL
})
output$user_content <- renderTable({
req(data()) %>% sample_n(5)
})
# Render a new histogram
# every time the slider is moved
output$userHist = renderPlot({
hist(rnorm(input$hist_slider_val))
})
}
shinyApp(ui, server)