-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_stats_v5.Rmd
417 lines (338 loc) · 12.6 KB
/
dynamic_stats_v5.Rmd
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
---
title: "BECCA"
output:
flexdashboard::flex_dashboard:
theme: cerulean
logo: undp48.png
orientation: rows
runtime: shiny
vertical_layout: fill
runtime: shiny
---
```{r Load Packages, include=FALSE}
require(flexdashboard)
require(tm)
require(shiny)
require(shinyBS)
require(quanteda)
require(RYandexTranslate)
require(stm)
require(wordcloud)
require(htmlwidgets)
require(stmBrowser)
require(devtools)
require(jsonlite)
require(SnowballC)
require(ggplot2)
require(cluster)
require(fpc)
require(skmeans)
require(knitr)
require(xlsx) # for reading original xls files
require(plotly) # for interactive map
require(likert) # for Likert plots
require(RColorBrewer) # for the Brewer color palette used in Triad plots
require(grid) # required by Triad and likert plots
require(gridExtra) # for arrangin the Triad plots
require(mixtools) # for computing ellipsoids from clustering
require(sp)
require(magrittr)
require(plotly)
require(data.table)
require(xtable)
devtools::install_github("timelyportfolio/stmBrowser@htmlwidget") # This needs to be installed once
```
```{r Translation Switch, include=FALSE, echo=TRUE}
## Switchboard
switch_t <- 0 # Translation
switch_c <- 1 # Corpus
save(list = c("switch_c","switch_t"), file = "switches.RData")
```
```{r Load Datasets, include = FALSE, echo = FALSE}
## File Structure - Pleeeeaeease tell me if you mess with this ~Charlie.
#/Working Dir
#/dynamic_stats_v3.R
#/factor_to_integer.R
#/sort_signifiers.R
#/namegetter.R
#/[dataset]_factornames.RData - {No longer needed, namegetter.R returns a function not a file.}.
#/clean_data
#/[dataset]_clean.RData
#/texts
#/[dataset]_translated_texts.RData
#/corpus/
#/[dataset]_corpus.RData
#/subsets
#/[dataset]_subsets.RData
knitr::opts_knit$set(root.dir = normalizePath('./'))
# print(opts_knit$get("root.dir"))
## Load All Datasets
wd <- getwd()
dataset <- c("kyrgyzstan", "moldova", "unicef", "serbia", "tajikistan", "yemen")
# print(dataset)
for (d in 1:length(dataset)){
load(paste0(wd,"/clean_data/",dataset[d],"_clean.RData"))
dataset <- c("kyrgyzstan", "moldova", "unicef", "serbia", "tajikistan", "yemen")
assign(paste0(dataset[d]),clean)
}
## Choose Dataset
dataset <- "moldova" ## Pick one!
load(paste0(wd,"/clean_data/",dataset,"_clean.RData"))
load(paste0(wd,"/clean_data/texts/",dataset,"_translated_texts.RData"))
load(paste0(wd,"/clean_data/subsets/",dataset,"_subsets.RData"))
save("dataset", file = "dataset.txt")
## Attach to search dir
search <- search()
if (!is.na(match("clean", search))) {detach(clean)}
if (!is.na(match("data", search))) {detach(data)}
search <- search()
if (is.na(match("clean", search))) {attach(clean)}
# knitr::opts_knit$set(root.dir = normalizePath('../'))
# print(opts_knit$get("root.dir"))
```
```{r Load All Corpuses and Texts}
## Load all Corpuses
load(paste0(wd,"/clean_data/corpus/moldova_corpus.RData"))
moldova_corpus <- corpus
load(paste0(wd,"/clean_data/corpus/kyrgyzstan_corpus.RData"))
kyrgyzstan_corpus <- corpus
load(paste0(wd,"/clean_data/corpus/serbia_corpus.RData"))
serbia_corpus <- corpus
load(paste0(wd,"/clean_data/corpus/tajikistan_corpus.RData"))
tajikistan_corpus <- corpus
load(paste0(wd,"/clean_data/corpus/yemen_corpus.RData"))
yemen_corpus <- corpus
load(paste0(wd,"/clean_data/corpus/unicef_corpus.RData"))
unicef_corpus <- corpus
# names(summary(yemen_corpus))
# names(summary(tajikistan_corpus))
load(paste0(wd,"/clean_data/texts/moldova_translated_texts.RData"))
moldova_texts_eng <- texts_eng
moldova_texts_org <- texts_org
moldova_titles_eng <- titles_eng
moldova_titles_org <- titles_org
load(paste0(wd,"/clean_data/texts/kyrgyzstan_translated_texts.RData"))
kyrgyzstan_texts_eng <- texts_eng
kyrgyzstan_texts_org <- texts_org
kyrgyzstan_titles_eng <- titles_eng
kyrgyzstan_titles_org <- titles_org
load(paste0(wd,"/clean_data/texts/serbia_translated_texts.RData"))
serbia_texts_eng <- texts_eng
serbia_texts_org <- texts_org
serbia_titles_eng <- titles_eng
serbia_titles_org <- titles_org
load(paste0(wd,"/clean_data/texts/tajikistan_translated_texts.RData"))
tajikistan_texts_eng <- texts_eng
tajikistan_texts_org <- texts_org
tajikistan_titles_eng <- titles_eng
tajikistan_titles_org <- titles_org
load(paste0(wd,"/clean_data/texts/yemen_translated_texts.RData"))
yemen_texts_eng <- texts_eng
yemen_texts_org <- texts_org
yemen_titles_eng <- titles_eng
yemen_titles_org <- titles_org
load(paste0(wd,"/clean_data/texts/unicef_translated_texts.RData"))
unicef_texts_eng <- texts_eng
unicef_texts_org <- texts_org
unicef_titles_eng <- titles_eng
unicef_titles_org <- titles_org
# sum(is.na(unicef_texts_eng))
```
```{r Dependencies, echo=FALSE}
wd <- getwd()
ds.env <- reactiveValues()
source("ds_preprocess.R")
source("factor_to_integer.R")
source("namegetter.R")
source("sort_signifiers.R")
source("unit2perc.R")
```
Descriptive Statistics {data-navmenu="STM"}
=====================================
Descriptive Statistics {.sidebar}
-------------------------------------
```{r Desc Stats: Dataset Builder, echo=FALSE}
# Choose Dataset
selectInput(inputId = "dataset",
label = "Select Dataset",
choices = c("Moldova","Kyrgyzstan UNDP","Kyrgyzstan Unicef","Serbia","Yemen","Tajikistan"),
selected = "Moldova")
# Process dataset
observeEvent(input$dataset,{
# Get dataset
dataset <- switch(input$dataset,
"Moldova" = moldova,
"Kyrgyzstan UNDP" = kyrgyzstan,
"Kyrgyzstan Unicef" = unicef,
"Serbia" = serbia,
"Yemen" = yemen,
"Tajikistan" = tajikistan)
ds.env$dataset <- ds.process(dataset)
ds.env$triads <- triads
ds.env$dyads <- dyads
ds.env$stones <- stones
ds.env$qnames <- qnames
ds.env$dqnames <- dqnames
})
```
```{r Row Selection, echo=FALSE}
# Choose Rows: Signifier Type
selectInput(inputId = "x1",
label = "Choose Rows",
choices = c("Select","Triads","Dyads","Stones","Questions","Descriptors"))
renderUI({
# Create covariate options
ds.env$x.choices <- switch(input$x1,
"Choose Signifier Type" = "Select",
"Triads" = names(ds.env$triads),
"Dyads" = names(ds.env$dyads),
"Stones" = names(ds.env$stones),
"Questions" = ds.env$qnames,
"Descriptors" = ds.env$dqnames)
# Choose Rows: Covariate dropdown
conditionalPanel(condition = "input.x1 != 'Select'",
selectInput(inputId = "x2",
label = "Choose Covariates",
choices = ds.env$x.choices))
})
renderUI({
# Choose Rows: Get factor levels
ds.env$x.levels <- if (req(input$x2) != "Select")
grep(input$x2, names(ds.env$dataset), value = TRUE)
# Choose Rows: factor level checkboxes
conditionalPanel(condition = "input.x1 == 'Questions' || input.x1 == 'Descriptors'",
checkboxInput(inputId = "x_all","All/None"),
checkboxGroupInput(inputId = "xlevel",
label = "Factor Level",
choices = ds.env$x.levels))
})
# Choose Rows: select all factor levels.
observe({
if (req(input$x_all))
updateCheckboxGroupInput(session,
inputId = "xlevel",
label = "Factor Level",
choices = ds.env$x.levels,
selected = ds.env$x.levels)
})
# Choose Rows: Continuous variable slider
conditionalPanel(condition = "input.x1 == 'Triads' || input.x1 == 'Dyads' || input.x1 == 'Stones'",
sliderInput(inputId = "x_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE))
actionButton("rows_go", "Select Rows")
```
```{r Column Selection, echo=FALSE}
# Choose Columns: Signifier Type
selectInput(inputId = "y1",
label = "Choose Columns",
choices = c("Select","Triads","Dyads","Stones","Questions","Descriptors"))
renderUI({
# Create covariate options
ds.env$y.choices <- switch(input$y1,
"Select" = "Choose Signifier Type",
"Triads" = names(ds.env$triads),
"Dyads" = names(ds.env$dyads),
"Stones" = names(ds.env$stones),
"Questions" = ds.env$qnames,
"Descriptors" = ds.env$dqnames)
# Choose Columns: Covariate dropdown
conditionalPanel(condition = "input.y1 != 'Select'",
selectInput(inputId = "y2",
label = "Choose Covariates",
choices = ds.env$y.choices))
})
renderUI({
# Choose Columns: Get factor levels
ds.env$y.levels <- if (req(input$y2) != "Select")
grep(input$y2, names(ds.env$dataset), value = TRUE)
# Choose Columns: factor level checkboxes
conditionalPanel(condition = "input.y1 == 'Questions' || input.y1 == 'Descriptors'",
checkboxInput(inputId = "y_all","All/None"),
checkboxGroupInput(inputId = "ylevel",
label = "Factor Level",
choices = ds.env$y.levels))
})
# Choose Columns: select all factor levels
observe({
if (req(input$y_all))
updateCheckboxGroupInput(session,
inputId = "ylevel",
label = "Factor Level",
choices = ds.env$y.levels,
selected = ds.env$y.levels)
})
# Choose Columns: Continuous variable slider
conditionalPanel(condition = "input.y1 == 'Triads' || input.y1 == 'Dyads' || input.y1 == 'Stones'",
sliderInput(inputId = "y_sld",
label = "Choose Range",
min = 0, max = 10, value = c(0,6), step = 0.5,
dragRange = TRUE))
actionButton("cols_go", "Select Columns")
hr()
radioButtons(inputId = "unit",
label = "Unit/Percentage",
choices = c("Unit", "Percentage"))
hr()
actionButton("ds_go", "Build Statistics")
```
```{r Variable Table, echo=FALSE}
ds.env$rows <- NULL
ds.env$cols <- NULL
observeEvent(input$rows_go,{
source("var_table.r")
rows <- ds.env$rows
rows <- var.table(rows, input$x2, input$x1, level = input$xlevel, range = input$x_sld)
ds.env$rows <- rows
output$row_selection <- renderTable(ds.env$rows, include.rownames = FALSE, align = rep("l",dim(ds.env$rows)[2] + 1))
})
observeEvent(input$cols_go,{
source("var_table.r")
cols <- ds.env$cols
cols <- var.table(cols, input$y2, input$y1, level = input$ylevel, range = input$y_sld)
ds.env$cols <- cols
output$col_selection <- renderTable(ds.env$cols, include.rownames = FALSE, align = rep("l",dim(ds.env$cols)[2] + 1))
})
observeEvent(input$ds_go,{
source("ds_builder.R")
ds.env$desc.stats <- ds.builder(ds.env$rows, ds.env$cols, ds.env$dataset)
ds.env$desc.percs <- ds.builder(ds.env$rows, ds.env$cols, ds.env$dataset, perc = TRUE)
if (input$unit == "Unit") {
output$stats <- renderTable(ds.env$desc.stats)
} else {
output$stats <- renderTable(ds.env$desc.percs)
}
})
observeEvent(input$unit,{
if (input$unit == "Unit") {
output$stats <- renderTable(ds.env$desc.stats)
} else {
output$stats <- renderTable(ds.env$desc.percs)
}
})
```
Descriptive Statistics {.tabset}
-------------------------------------
### Variable Selection and Summaries
```{r Descriptive Stats Tabs}
# Summary of currently selected variable
fluidRow(column("Summary", width = 12))
fluidRow(
column(strong("Selected Rows"), tableOutput("row_selection"), width = 6),
column(strong("Selected Columns"), tableOutput("col_selection"), width = 6)
)
```
### Descriptive Statistics
```{r DS Table}
tableOutput("stats")
```
### Debugging
```{r}
## Temporary Panel For Debugging
inputPanel(textInput(inputId = "debugger", "Print Console"), actionButton("run", "Execute"))
observeEvent(input$run,{
output$debug <- renderPrint(eval(parse(text=isolate(input$debugger)), envir = parent.frame(n=1)))
})
htmlOutput("debug")
```