-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
163 lines (121 loc) · 4.63 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#### simple shiny app to demonstrate using the Python package ASDM in R
# ASDM is being developed by Wang Zhao. More info at https://pypi.org/project/asdm/
library(dplyr)
library(tidyr)
library(janitor)
library(DT)
library(plotly)
library(reticulate)
library(shiny)
### set up ----
use_virtualenv('./.venv', required=TRUE)
# source ASDM python package
asdm <- import("ASDM")
# load stella model
pathway_model <- asdm$asdm$sdmodel(from_xmile = "capacity constrained service pathway.stmx")
# function to run a simulation and return results to a dataframe
run_sim <- function(mod){
mod$simulate()
res <- mod$export_simulation_result(format='df',
dt = TRUE,
to_csv = FALSE)
return(res)
}
#### Define UI for application ----
ui <- fluidPage(
# Application title
titlePanel("Open Source System Dynamics"),
# Sidebar with slider inputs
sidebarLayout(
sidebarPanel(
sliderInput("places",
"Number of places:",
min = 0,
max = 300,
value = 130,
step = 5),
sliderInput("los",
"Length of service (weeks):",
min = 0,
max = 12,
value = 7,
step = 0.25),
hr(),
p("Chart of weekly referral rate for reference"),
plotlyOutput("pl_refs")
),
# Show a plot of the generated distribution
mainPanel(
plotlyOutput("pl_flow"),
plotlyOutput("pl_usage")
)
)
)
# Define server logic ----
server <- function(input, output) {
## update model based on silder and table inputs, return results
update_model <- reactive({
pathway_model$clear_last_run()
pathway_model$replace_element_equation('total_places', input$places)
pathway_model$replace_element_equation('length_of_service_wks', input$los)
results <- run_sim(pathway_model) |>
clean_names()
return(results)
})
### outputs for UI ----
output$pl_refs <- renderPlotly({
update_model() |>
drop_na() |>
plot_ly(x = ~time, y = ~referrals_per_week, name = "Weekly referrals",
type="scatter", mode="lines",
line = list(color ="#5881c1")) |>
layout(
hovermode = "x",
xaxis = list(title = "Days since simualtion start"),
yaxis = list(title = "Weekly referrals",
rangemode = "tozero",
hoverformat = ".0f")
)
})
output$pl_flow <- renderPlotly({
update_model() |>
drop_na() |>
plot_ly(x = ~time, y = ~service_users, name = "Service Users",
type="scatter", mode="lines",
line = list(color ="#5881c1"),
stackgroup = 'one', fillcolor = "#d4dff0") |>
add_trace(y = ~unused_places, name = "Unused places",
line = list(color= "#686f73"),
fillcolor= "#d8dadb") |>
add_trace(y =~waiting, name = "Waiting for service",
line = list(color= "#ec6555"),
fillcolor= "#fbd7d3") |>
layout(
hovermode = "x",
xaxis = list(title = ""),
yaxis = list(title = "Occupancy status",
rangemode = "tozero",
hoverformat = ".0f")
)
})
output$pl_usage <- renderPlotly({
update_model() |>
drop_na() |>
plot_ly(x = ~time) |>
add_lines(y = ~referrals,
name = "New referrals per day",
line = list(color ="#5881c1")) |>
add_lines(y = ~finish_service,
name = "Finish service",
line = list(color= "#ec6555")) |>
layout(
hovermode = "x",
xaxis = list(title = ""),
yaxis = list(title = "Daily flows in and out",
rangemode = "tozero",
hoverformat = ".1f")
)
})
}
# Run the application
shinyApp(ui = ui, server = server)