forked from netterie/HIVBackCalc_App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
213 lines (169 loc) · 6.9 KB
/
server.R
File metadata and controls
213 lines (169 loc) · 6.9 KB
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
207
208
209
210
211
212
213
# By default, the file size limit is 5MB. It can be changed by
# setting this option. Here we'll raise limit to 9MB.
options(shiny.maxRequestSize = 9*1024^2)
# Source code (eventually load a package)
library(rootSolve)
library(ggplot2)
library(reshape)
library(Hmisc)
library(scales)
library(plyr)
library(xtable)
source('development/model.R')
source('development/other.R')
shinyServer(function(input, output, session) {
##################################################
# LOAD INPUT CSV
##################################################
# Access as rawdata()
rawdata <- reactive({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
##################################################
# VIEW INPUT CSV
##################################################
output$data_10rows <- renderTable({
data <- rawdata()
#data <- read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
data[1:10,]
})
##################################################
# SELECT SUBSET - NOT CODED YET
##################################################
dataf <- reactive({
dataf <- rawdata()
})
##################################################
# DESCRIBE SAMPLE
##################################################
output$describe_sample <- renderTable({
dataf <- dataf()
variables <- c(`Age Group`='agecat5',
`Race/Ethnicity`='race',
`Mode of Transmission`='mode')
everHadNegTest_subgrouptab <- tabulate_everHadNegTest(dataf,
variables,
supercolumn=TRUE)
},
caption='Column % sums to 100 within each characteristic. Availability of testing history data within each subgroup level is shown as row percents of %Yes, %No, and %Missing',
label='tab:sample',
digits=0,
table.placement='!h',
caption.placement='top',
include.rownames=FALSE,
size='small',
sanitize.text.function=function(str) { gsub('(\\.)*Percent(\\.)*', ' \\% ',str); }
)
##################################################
# PLOT DIAGNOSES
##################################################
output$diagnoses_plot <- renderPlot({
dataf <- dataf()
plot_qtrDx(dataf)
})
##################################################
# PLOT TESTING HISTORIES
##################################################
output$testinghistories_plot <- renderPlot({
dataf <- dataf()
everHadNegTest_time <- tabulate_everHadNegTest(dataf,'yearDx')
plot_everHadNegTest(everHadNegTest_time)
})
##################################################
# PLOT TID
##################################################
output$tid_plot <- renderPlot({
dataf <- dataf()
fig1combined(dataf, legendposition='right')
})
##################################################
# RUN BACKCALCULATION
##################################################
results <- reactive({
# Goal is to produces a list that contains everything
# needed to get the stats object and summaries_both to work
dataf <- dataf()
##### DEFINE DIAGNOSED COUNTS PER TIME UNIT (ASSUMED QTR, NOW)
time_min <- min(dataf$timeDx)
time_max <- max(dataf$timeDx)
allTimes <- seq(time_min, time_max, by=0.25)
obsCounts <- table(dataf$timeDx)
allCounts <- structure(rep(0,length(allTimes)),
class='table',
names=allTimes)
allCounts[names(allCounts)%in%names(obsCounts)] <- obsCounts
##### RUN BACKCALCULATION
withProgress(message = 'Calculating, please wait', value=0, {
all_noimpute <- runBackCalc(TID=dataf$infPeriod,
impute=FALSE,
age=dataf$hdx_age,
diagnosedCounts=allCounts,
upperBound=FALSE,
runBoth=TRUE,
intervalLength=0.25,
printProgress=FALSE)
incProgress(detail='50% complete...')
all_impute <- runBackCalc(TID=dataf$infPeriod,
impute=TRUE,
age=dataf$hdx_age,
diagnosedCounts=allCounts,
upperBound=FALSE,
runBoth=TRUE,
intervalLength=0.25,
printProgress=FALSE)
summaries_noimpute <- summarize_runBackCalc(results=all_noimpute,
diagnosedCounts=allCounts,
times=allTimes)
summaries_impute <- summarize_runBackCalc(results=all_impute,
diagnosedCounts=allCounts,
times=allTimes)
summaries_both <- summarize_runBackCalc_combined(
results=list(noimpute=all_noimpute,
impute=all_impute),
diagnosedCounts=allCounts,
times=allTimes)
stats = data.frame(imputed=c(rep('Yes',
nrow(summaries_impute[['stats']])),
rep('No',
nrow(summaries_noimpute[['stats']]))),
rbind(summaries_impute[['stats']],
summaries_noimpute[['stats']]))
stats <- format_stats(stats)
return(list(summaries_both=summaries_both, stats=stats))
}) # end withProgress
}) # end reactive
##################################################
# PLOT BACKCALCULATION RESULTS
##################################################
output$results_plot <- renderPlot({
# Don't display if backcalculation wasn't started
if (input$go == 0) return()
# Goal is to plot summaries_both object from run_main.R
results <- results()
results[['summaries_both']]
})
##################################################
# TABULATE BACKCALCULATION RESULTS
##################################################
output$results_table <- renderTable({
# Don't display if backcalculation wasn't started
if (input$go == 0) return()
# Goal is to plot summaries_both object from run_main.R
results <- results()
results[['stats']]
},
label='tab:res_main',
digits=0,
size='small',
include.rownames=FALSE
)
})