-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_async.R
86 lines (74 loc) · 1.85 KB
/
module_async.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
plotUI <- function(id) {
ns <- NS(id)
fluidRow(
column(width = 10,
echarts4r::echarts4rOutput(ns("plot")) %>%
shinycssloaders::withSpinner()
),
column(width = 2,
actionButton(
inputId = ns("bar"),
label = "",
icon = icon("chart-bar")
),
actionButton(
inputId = ns("pie"),
label = "",
icon = icon("chart-pie")
)
)
)
}
plotServer <- function(id, df) {
moduleServer(
id,
function(input, output, session) {
# Setting reactiveValues for changing plot types
rv <- reactiveValues(
plot_type = "bar",
plot = NULL
)
# Observer and main calculation
observeEvent(c(df(), rv$plot_type),{
# We can't use reactives inside of future
plot_type <- rv$plot_type
df <- df()
promises::future_promise({
# Timeout to show asyncronity
Sys.sleep(5)
if (plot_type == "bar") {
df %>%
plot_bar()
} else {
df %>%
plot_pie()
}
}) %...>%
(
function(result){
rv$plot <- result
}
) %...!% (
function(error){
rv$plot <- NULL
print("Plot Error")
print(error)
}
)
# hacking intra-session asyncronity
print(class(rv$plot))
})
# Observers for actionButtion clicks
observeEvent(input$bar, {
rv$plot_type <- "bar"
})
observeEvent(input$pie,{
rv$plot_type <- "pie"
})
# Render Plots
output$plot <- echarts4r::renderEcharts4r({
req(rv$plot)
})
}
)
}