-
Notifications
You must be signed in to change notification settings - Fork 0
/
app10.R
206 lines (156 loc) · 7.09 KB
/
app10.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
### current issues:
# - everything
# - hacky way of doing rowselect :: somehow not same order in dump()$Profile objects as in live_data_infomat()$Profile, really annoying
### current todo:
# - make code readable
# - fix layouting
# - add shit
# - fix/implement plot adding again
# - add readability features to app thing (ugh)
# install.packages('rhandsontable')
library(shiny)
library(tableHTML)
source("plotfunction.R")
library(rhandsontable)
htmlOutput("summary")
empty_dat = matrix(nrow = 1,ncol = 5)
colnames(empty_dat) = c("Profile","FTE","MVT","Pattr","RT")
# create empty dataframe placeholder for retiremat to show before the file upload
empty_dat2 = matrix(nrow = 1,ncol = 10)
colnames(empty_dat2) = c("Profile",paste0('Y',rep(0:8)))
dumpthing <- list()
# number of runs for the binomial stuff
nruns = 10000
timescale = 8
ui <- fluidPage(
titlePanel("Stochastic forecast of likely shortage scenarios"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Upload infomat CSV file here", accept = c(".csv")),
rHandsontableOutput('infomat'),
fileInput("file2", "Upload retiremat CSV file here", accept = c('.csv')),
rHandsontableOutput('retiremat'),
actionButton("go", "Data Update / generate plot"),
numericInput("profsel", "Row of profile to analyze", value = 0, min = 0, max = 0)
),
mainPanel(
tabsetPanel(
tabPanel("Plot",
tableOutput("plottable"),
plotOutput("binplot")),
tabPanel("whatif",
sliderInput('attrslider', label = "Range of attrition rates" ,min = 0.01, max = 0.3, value = c(0.05,0.1), step = 0.01),
actionButton("go_df", "Generate / refresh whatif table"),
titlePanel(textOutput('tabletext')),
tableHTML_output('whatiftable')
),
tabPanel("Info and readme",includeHTML("info.html"))
)
)
)
)
server <- function(input, output, session) {
indat_info <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(rhandsontable(empty_dat))
raw_input = read.csv(inFile$datapath, header=T, sep = ",")
raw_input = raw_input[order(raw_input$Profile),]
return(rhandsontable(raw_input, rowHeaders = NULL))
})
output$infomat <- renderRHandsontable({
indat_info()
})
livedata_info <- eventReactive(input$go, {
live_data_info = hot_to_r(input$infomat)
return(live_data_info)
})
indat_retire <- reactive({
inFile <- input$file2
if (is.null(inFile))
return(rhandsontable(empty_dat))
raw_input = read.csv(inFile$datapath, header=T, sep = ",")
raw_input = raw_input[order(raw_input$Profile),]
return(rhandsontable(raw_input, rowHeaders = NULL))
})
output$retiremat <- renderRHandsontable({
indat_retire()
})
livedata_retire <- eventReactive(input$go, {
live_data_retire = hot_to_r(input$retiremat)
return(live_data_retire)
})
dumpmat <- reactive({
for (profile in livedata_info()$Profile){
#dumpthing[[profile]] = NA
nfte <- as.numeric(livedata_info()[livedata_info()$Profile==profile,'FTE'])
MVT <- as.numeric(livedata_info()[livedata_info()$Profile==profile,'MVT'])
attrP <- as.numeric(livedata_info()[livedata_info()$Profile==profile,'Pattr'])
ub <- 0.95
lb <- 0.05
df <- data.frame('Y0' = rep(nfte,nruns))
df2 <- data.frame(row.names = c('ub','lb','probcross','naive'))
df2$T0 <- c(nfte,nfte,0,nfte)
df3 <- data.frame('Y0' = nfte)
for (y in 1:timescale){
col <- paste0('Y',y)
df[[col]] = NA
df3[[col]] = NA
df[df[,y]-livedata_retire()[livedata_retire()$Profile==profile,col] < 1,y+1] = 0
df[df[,y]-livedata_retire()[livedata_retire()$Profile==profile,col] >= 1,y+1] = df[df[,y]-livedata_retire()[livedata_retire()$Profile==profile,col] >= 1,y] - livedata_retire()[livedata_retire()$Profile==profile,col] - rbinom(length(df[df[,y]-livedata_retire()[livedata_retire()$Profile==profile,col] >= 1,y]),as.numeric(df[df[,y]-livedata_retire()[livedata_retire()$Profile==profile,col] >= 1,y] - livedata_retire()[livedata_retire()$Profile==profile,col]),attrP)
df3[,y+1] = df3[,y]*((1-attrP))-livedata_retire()[livedata_retire()$Profile==profile,col]
colval <- c(quantile(df[[col]],c(ub,lb)),
sum(df[[col]] < MVT)/nruns,
ifelse(df3[[col]]<0,0,df3[[col]]))
df2[[col]] = colval
}
tdf2 <- data.frame(t(df2))
dumpthing[[profile]] = tdf2
}
return(dumpthing)
})
observe({
updateNumericInput(inputId = "profsel", min = 1, max = nrow(livedata_info()), value = 1)
})
selectedrow <- reactive(livedata_info()[input$profsel,])
output$binplot <- renderPlot(
binplot(df = dumpmat()[[selectedrow()$Profile]], ts = timescale, nrep = nruns, selrow = selectedrow()), width = 900, height = 600
)
plottable.t <- reactive({
df <- t(dumpmat()[[selectedrow()$Profile]])
rownames(df) <- c('lower bound','upper bound','probability of shortage','constant attrition')
return(df)
})
output$plottable <- renderTable(plottable.t(),rownames=T)
output$tabletext <- renderText(paste('Shortage probabilities across different attrition rates for',selectedrow()$Profile))
output$whatiftable <- render_tableHTML({
data = whatifdf(selectedrow = selectedrow(), retiremat = livedata_retire(), attrlow = input$attrslider[1], attrhigh = input$attrslider[2]);
data %>%
tableHTML(rownames = T)%>%
add_css_conditional_column(conditional = 'between',
between = c(selectedrow()$RT, 999),
css = list(c('font-weight'),
c('700')),
columns = 1:ncol(data)) %>%
add_css_conditional_column(conditional = 'between',
between = c(0, 999),
css = list(c('width'),
c('100px')),
columns = 1:ncol(data))
# add_css_conditional_column(conditional = 'between',
# between = c(0, 990),
# css = list(c('background-color'),
# c('lightblue')),
# columns = 1:ncol(data))
# add_css_conditional_column(conditional = 'between',
# between = c(0, 5),
# css = list(c('background-color'),
# c('#f6f6f6'))) %>%
# add_css_conditional_column(conditional = 'between',
# between = c(10, 20),
# css = list(c('background-color'),
# c('lightblue')),
# columns = 1:ncol(data))
})
}
shinyApp(ui, server)