-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 18 KB
/
.Rhistory
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
} else {
updated$reason_correct <- input$change
updated$ra_name <- user()
updated$date_shiny_correct <- lubridate::now()
# connect to database
con <- connect_cnetdb()
# write data to database
write_cnetdb(con, updated, "corupdate")
#write_csv(updated,"../data/updates.csv")
shinyalert(title = "Success!",
text = "Data successfully uploaded.",
type = "success")
# disconnect now done updating data
dbDisconnect(con)
}
}
})
## -----------< input$clear -- 'Clear records selected' pushed >------------
observeEvent(input$clear, {
record_select$id <- numeric()
})
## -----------< input$corviz_selected -- Timeline event selected >-----------
observeEvent(input$corviz_selected, {
# if selected something from timeline, add to record_select$id
record_select$id <- unique(c(record_select$id, input$corviz_selected))
})
## -----------< input$upload -- 'Upload corrections' button pushed >----------
# once user pushes the button, check for why they want to do a change
observeEvent(input$upload, {
shinyalert(
title = "Update Warning",
text = "Are you sure you want to upload these records?",
showConfirmButton = TRUE, showCancelButton = TRUE,
inputId = "okbutt",
callbackR = function(x) {
if (x) {
shinyalert(
title = "Reason for Record Update",
text = "Please input your reason for modifying record(s):",
type = "input", inputId = "change",
callbackR = function(x) {
if (str_trim(x) == "") {
shinyalert(
title = "Warning: No Reason Given",
text = "You did not enter a reason for the change,
so no update will be processed.",
type = "error")
}
})
}
})
})
#### OUTPUTS -----------------------------------------------------------------
## --------------------< output$corviz -- timeline viz >---------------------
output$corviz <- renderTimevis({
timevis(timeline_df(), groups = group_df())
})
## --------------------< output$policy -- policy tab >---------------------
output$policy <- renderRHandsontable({
policydf <- full_df() %>%
select(all_of(input$col_names)) %>%
rhandsontable(height = 800) %>%
hot_cols(columnSorting = TRUE) %>%
hot_context_menu(
customOpts = list(
csv = list(name = "Download to CSV",
callback = htmlwidgets::JS(
"function (key, options) {
var csv = csvString(this);
var link = document.createElement('a');
link.setAttribute('href', 'data:text/plain;charset=utf-8,' +
encodeURIComponent(csv));
link.setAttribute('download', 'data.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}"))))
# read only columns
if ("record_id" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("policy_id" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("corrected" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("ra_name" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("recorded_date" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("source_file_Type" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("source_file_2_Type" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
if ("link_type" %in% input$col_names) {
policydf <- policydf %>%
hot_col("record_id", readOnly = TRUE)
}
# columns with features
if ("Correct" %in% input$col_names) {
policydf <- policydf %>%
hot_col(c("Correct"),
renderer = htmlwidgets::JS("safeHtmlRenderer"),
readOnly = TRUE)
}
if ("Update" %in% input$col_names) {
policydf <- policydf %>%
hot_col(c("Update"),
renderer = htmlwidgets::JS("safeHtmlRenderer"),
readOnly = TRUE)
}
if ("type" %in% input$col_names) {
policydf <- policydf %>%
hot_col("type", type = "dropdown",
strict = TRUE, source = source_cols$type)
}
if ("country" %in% input$col_names) {
policydf <- policydf %>%
hot_col("country", type = "autocomplete",
strict = TRUE, source = source_cols$country)
}
if ("province" %in% input$col_names) {
policydf <- policydf %>%
hot_col("province", type = "autocomplete",
strict = TRUE, source = source_cols$province)
}
if ("target_geog_level" %in% input$col_names) {
policydf <- policydf %>%
hot_col("target_geog_level", type = "dropdown",
strict = TRUE, source = source_cols$target_geog_level)
}
if ("target_who_what" %in% input$col_names) {
policydf <- policydf %>%
hot_col("target_who_what", type = "dropdown",
strict = TRUE, source = source_cols$target_who_what)
}
if ("target_direction" %in% input$col_names) {
policydf <- policydf %>%
hot_col("target_direction", type = "dropdown",
strict = TRUE, source = source_cols$target_direction)
}
if ("compliance" %in% input$col_names) {
policydf <- policydf %>%
hot_col("compliance", type = "dropdown",
strict = TRUE, source = source_cols$compliance)
}
return(policydf)
})
## -----------------< output$text -- selected record IDs >--------------------
output$text <- renderText({
recordstext <- ifelse(length(record_select$id) == 0, 'None selected.',
paste0(unique(record_select$id), collapse = ', '))
paste0("<b>Records Selected from timeline viz:</b> ", recordstext)
})
# upload information to SQL table whenever outcome button clicked
# observeEvent(input$qualtrics,{
# tibble(userid=this_user,
# clicktime=lubridate::now()) %>%
# dbWriteTable(con,"webout",value=.,overwrite=F,append=T,row.names=F)
# })
# updateSelectizeInput(session, 'ind', choices = bea_desc$NAICS.Desc, server = TRUE,
# selected=bea_desc$NAICS.Desc[1])
}
###################################################
# CoronaNet Data Validation Web App
# global file for loading global data/options
# Author(s): Robert Kubinec, Clara Wang
# Last updated: June 23, 2020
###################################################
## ---------------------< load data >--------------------------------
# database connection
con <- connect_cnetdb()
# read in data from AWS
raw_data <- dbReadTable(con, "cordata")
## ---------------------< format AWS tables >--------------------------------
# need to check and see if all column names exist in the update table
exist_update <- dbGetQuery(con, "SELECT * FROM public.corupdate LIMIT 1")
missing_cols <- names(raw_data)[!(names(raw_data) %in% names(exist_update))]
# loop over missing cols and add them to table corupdate
fix_miss <- lapply(missing_cols, function(m) {
# determine column type in SQL
this_col_type <- dbDataType(dbDriver("PostgreSQL"), raw_data[[m]])
dbSendQuery(con, paste0("ALTER TABLE corupdate ADD COLUMN ",
make.db.names(dbDriver("PostgreSQL"), m),
" ", this_col_type, ";"))
})
## -------------------< format policy tab data >--------------------------------
# data for populating the table
full_data <- raw_data %>%
mutate(group_id = as.numeric(factor(type)),
type2 = str_wrap(type, width = 15),
start = ymd(start),
end = ymd(end),
date_announced = ymd(date_announced),
type2 = str_replace_all(type2, "\\n","<br>")) %>%
# grab corrections and updates to policies
mutate(
Correct = paste0(
'<a href="https://tummgmt.eu.qualtrics.com/jfe/form/SV_bf6YMWbTpYJAW4l?Q_R=',
record_id,
'&Q_R_DEL=1&record_id=',
policy_id, '&link_type=C"',
' target="_blank">Correct</a>'),
# only create update link if it's a new entry
Update = ifelse(
entry_type == "new_entry",
paste0('<a href="https://tummgmt.eu.qualtrics.com/jfe/form/SV_bf6YMWbTpYJAW4l?Q_R=',
record_id, '&record_id=',
policy_id,'&link_type=U"',' target="_blank">Update</a>'), ""),
recorded_date = as.character(recorded_date)) %>%
select(Correct, Update,
corrected,
start, end,
record_id, policy_id,
recorded_date,
ra_name, country, province,
init_country_level, entry_type,
type, description,
target_geog_level, target_region, target_province, target_city,
target_other, target_who_what, target_direction, travel_mechanism,
compliance, enforcer, everything())
## -------------------< format timeline data >--------------------------------
# policy type information
group_data <- select(full_data, id = "group_id", content = "type2", type) %>%
distinct() %>%
filter(!is.na(id))
# timeline data
qdata <- full_data %>%
select(start,
end,
ra_name,
policy_id,
title = "description",
content = "init_country_level",
group = "group_id",
id = "record_id",
country,
province,
city,
entry_type) %>%
mutate(
content = recode(
content,
`No, it is at the national level` = "National",
`Yes, it is at the province/state level` = "Provincial",
`Yes, it is at the city/municipal level` = "City",
`Yes, it is at another governmental level (e.g. county)` = "District"),
end = coalesce(end, lubridate::today()),
content = case_when(
!is.na(province) & is.na(city) ~ paste(content, country,
province, title, sep = "-"),
!is.na(province) & !is.na(city) ~ paste(content, country,
province, title, city, sep = "-"),
is.na(province) & !is.na(city) ~ paste(content, country,
city, sep = "-"),
TRUE ~ paste(content, country, title, sep = "-"))) %>%
filter(!is.na(start), !is.na(group))
# set up policy types for drop downs to ensure data entry is safe
source_cols <- purrr::map(
.x = purrr::set_names(c("type", "country", "province", "target_geog_level",
"compliance", "target_who_what", "target_direction")),
.f = function(x) unique(full_data[[x]]))
# grab most recent entry in data
mostrecent_date <- max(lubridate::as_datetime(full_data$recorded_date),
na.rm = TRUE)
# disconnect from database
dbDisconnect(con)
# user_access_data <- tibble(userid=session$user,
# accessdate=as.character(lubridate::now()))
# dbWriteTable(con,"corona_users",value=user_access_data,append=T,
# row.names=F)
update <- environment()
update$count <- 0
###################################################
# CoronaNet Data Validation Web App
# RShiny app for updating RA entries
# Author(s): Robert Kubinec, Clara Wang
# Last updated: June 23, 2020
###################################################
#### SET UP --------------------------------------------------------------------
# set working directory
#wd <- "C:/Users/clara/Documents/corona_private/data_validate_shiny" # Clara wd
#setwd(wd)
# load libraries
library(shiny)
library(shinythemes)
library(dplyr)
library(tidyr)
library(shinyWidgets)
library(RPostgreSQL)
library(timevis)
library(stringr)
library(lubridate)
library(rhandsontable)
library(shinyalert)
library(readr)
require(DBI)
# source required files
source("utils.R") # load functions
source("global.R") # load data
source("ui.R") # app UI
source("server.R") # app server
#### RUN APP -------------------------------------------------------------------
# disconnect from AWS database when Shiny app closes
onStop(function() {
dbDisconnect(con)
})
# run Shiny App
shinyApp(
ui = ui,
server = server
)
###################################################
# CoronaNet Data Validation Web App
# global file for loading global data/options
# Author(s): Robert Kubinec, Clara Wang
# Last updated: June 23, 2020
###################################################
## ---------------------< load data >--------------------------------
# database connection
con <- connect_cnetdb()
# read in data from AWS
raw_data <- dbReadTable(con, "cordata")
## ---------------------< format AWS tables >--------------------------------
# need to check and see if all column names exist in the update table
exist_update <- dbGetQuery(con, "SELECT * FROM public.corupdate LIMIT 1")
missing_cols <- names(raw_data)[!(names(raw_data) %in% names(exist_update))]
# loop over missing cols and add them to table corupdate
fix_miss <- lapply(missing_cols, function(m) {
# determine column type in SQL
this_col_type <- dbDataType(dbDriver("PostgreSQL"), raw_data[[m]])
dbSendQuery(con, paste0("ALTER TABLE corupdate ADD COLUMN ",
make.db.names(dbDriver("PostgreSQL"), m),
" ", this_col_type, ";"))
})
## -------------------< format policy tab data >--------------------------------
# data for populating the table
full_data <- raw_data %>%
mutate(group_id = as.numeric(factor(type)),
type2 = str_wrap(type, width = 15),
start = ymd(start),
end = ymd(end),
date_announced = ymd(date_announced),
type2 = str_replace_all(type2, "\\n","<br>")) %>%
# grab corrections and updates to policies
mutate(
Correct = paste0(
'<a href="https://tummgmt.eu.qualtrics.com/jfe/form/SV_bf6YMWbTpYJAW4l?Q_R=',
record_id,
'&Q_R_DEL=1&record_id=',
policy_id, '&link_type=C"',
' target="_blank">Correct</a>'),
# only create update link if it's a new entry
Update = ifelse(
entry_type == "new_entry",
paste0('<a href="https://tummgmt.eu.qualtrics.com/jfe/form/SV_bf6YMWbTpYJAW4l?Q_R=',
record_id, '&record_id=',
policy_id,'&link_type=U"',' target="_blank">Update</a>'), ""),
recorded_date = as.character(recorded_date)) %>%
select(Correct, Update,
corrected,
start, end,
record_id, policy_id,
recorded_date,
ra_name, country, province,
init_country_level, entry_type,
type, description,
target_geog_level, target_region, target_province, target_city,
target_other, target_who_what, target_direction, travel_mechanism,
compliance, enforcer, everything())
## -------------------< format timeline data >--------------------------------
# policy type information
group_data <- select(full_data, id = "group_id", content = "type2", type) %>%
distinct() %>%
filter(!is.na(id))
# timeline data
qdata <- full_data %>%
select(start,
end,
ra_name,
policy_id,
title = "description",
content = "init_country_level",
group = "group_id",
id = "record_id",
country,
province,
city,
entry_type) %>%
mutate(
content = recode(
content,
`No, it is at the national level` = "National",
`Yes, it is at the province/state level` = "Provincial",
`Yes, it is at the city/municipal level` = "City",
`Yes, it is at another governmental level (e.g. county)` = "District"),
end = coalesce(end, lubridate::today()),
content = case_when(
!is.na(province) & is.na(city) ~ paste(content, country,
province, title, sep = "-"),
!is.na(province) & !is.na(city) ~ paste(content, country,
province, title, city, sep = "-"),
is.na(province) & !is.na(city) ~ paste(content, country,
city, sep = "-"),
TRUE ~ paste(content, country, title, sep = "-"))) %>%
filter(!is.na(start), !is.na(group))
# set up policy types for drop downs to ensure data entry is safe
source_cols <- purrr::map(
.x = purrr::set_names(c("type", "country", "province", "target_geog_level",
"compliance", "target_who_what", "target_direction")),
.f = function(x) unique(full_data[[x]]))
# grab most recent entry in data
mostrecent_date <- max(lubridate::as_datetime(full_data$recorded_date),
na.rm = TRUE)
# disconnect from database
dbDisconnect(con)
# user_access_data <- tibble(userid=session$user,
# accessdate=as.character(lubridate::now()))
# dbWriteTable(con,"corona_users",value=user_access_data,append=T,
# row.names=F)
update <- environment()
update$count <- 0
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp()
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
runApp('C:/Users/August/OneDrive - Københavns Universitet/Documents/Uni/corona_private/data_validate_shiny')
if(!require("devtools")) install.packages("devtools")
devtools::install_github("Guscode/Sentida")
devtools::install_github("Guscode/Sentida")
install.packages("devtools")
devtools::install_github("Guscode/Sentida")
uninstall.packages("glue")
remove.packages("glue")
remove.packages("tidyverse")
library("tidyverse")
library(tidyverse)
devtools::install_github("Guscode/Sentida")
install.packages("Rtools")
devtools::install_github("tidyverse/glue")
writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")
pwd
pwd()
gwd()
getwd()
library(Sentida)
library(Sentida)
library(openxlsx)
library(svMisc)
setwd("C:\\Users\\August\\OneDrive - Københavns Universitet\\Documents\\Uni\\Kandidat i Statskundskab\\4. semester kandidat\\Projekt\\FT-project")
Sentida::sentida("Abort er mord", output = "mean")
sentida("Abort er mord", output = "total")
sentida("Abort er mord", output = "mean")
df <- read.csv("short_comment_df.csv", encoding = "UTF-8")
df$sentiment_mean = Sentida::sentida(df$tale,output = "mean")
df$sentiment_total = Sentida::sentida(df$tale,output = "total")
for( i in 1:nrow(df)){
progress(i,nrow(df))
df$sentiment_mean[i] <- Sentida::sentida(df$tale[i],output = "mean")
df$sentiment_total[i] <- Sentida::sentida(df$tale[i],output = "total")
}
write.csv(df,"df_sentiment_short_comment.csv")
library(Sentida)
library(openxlsx)
library(svMisc)
setwd("C:\\Users\\August\\OneDrive - Københavns Universitet\\Documents\\Uni\\Kandidat i Statskundskab\\4. semester kandidat\\Projekt\\FT-project")
Sentida::sentida("Abort er mord", output = "mean")
sentida("Abort er mord", output = "total")
sentida("Abort er mord", output = "mean")
df <- read.csv("short_comment_df.csv", encoding = "UTF-8")
df$sentiment_mean = Sentida::sentida(df$tale,output = "mean")
df$sentiment_total = Sentida::sentida(df$tale,output = "total")
for( i in 1:nrow(df)){
progress(i,nrow(df))
df$sentiment_mean[i] <- Sentida::sentida(df$tale[i],output = "mean")
df$sentiment_total[i] <- Sentida::sentida(df$tale[i],output = "total")
}
write.csv(df,"df_sentiment_short_comment.csv")
#if(!require("devtools")) install.packages("devtools")
#install.packages("glue")
#devtools::install_github("Guscode/Sentida")
library(Sentida)
library(openxlsx)
library(svMisc)
setwd("C:\\Users\\August\\OneDrive - Københavns Universitet\\Documents\\Uni\\Kandidat i Statskundskab\\4. semester kandidat\\Projekt\\FT-project")
Sentida::sentida("Abort er mord", output = "mean")
sentida("Abort er mord", output = "total")
sentida("Abort er mord", output = "mean")
df <- read.csv("short_comment_df.csv", encoding = "UTF-8")
df$sentiment_mean = Sentida::sentida(df$tale,output = "mean")
df$sentiment_total = Sentida::sentida(df$tale,output = "total")
for( i in 1:nrow(df)){
progress(i,nrow(df))
df$sentiment_mean[i] <- Sentida::sentida(df$tale[i],output = "mean")
df$sentiment_total[i] <- Sentida::sentida(df$tale[i],output = "total")
}
write.csv(df,"df_sentiment_short_comment.csv")