-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoderRequest.R
More file actions
56 lines (44 loc) · 2.31 KB
/
coderRequest.R
File metadata and controls
56 lines (44 loc) · 2.31 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
# EVENT: Coder requests batch of articles
# 1. Download the Articles sheet.
# 2. Filter so we only have article ids where the codingStatus = 'inPool'. This is the current Coding Pool.
# 3. Randomly select the batch. Send ids to Coder.
# 4. Update codingStatus column for these ids to 'outForCoding'.
# 5. Update 'coder' column with coder's initials.
coderRequest <- function(batchSize, thisCoder){
# show warning if batch size greater than 20
if(batchSize > 20) warning('batch size exceeds 20!')
# show warning if coder name exceeds 3 characters
if(length(thisCoder) > 3) warning('coder initials exceed 3 characters!')
# load packages and custom function
library(googlesheets)
library(tidyverse)
pull <- function(x,y) {x[,if(is.name(substitute(y))) deparse(substitute(y)) else y, drop = FALSE][[1]]}
# 1. Download the Articles sheet and extract the data
articlesSheet <- gs_title("Articles")
articlesData <- articlesSheet %>% gs_read()
# 2. Filter so we only have article ids where the codingStatus = 'inPool'. This is the current Coding Pool.
codingPool <- articlesData %>%
filter(codingStatus == 'inPool') %>%
pull(id)
# throw an error if batch size exceeds number of available articles
if(batchSize > length(codingPool)) stop('Batch size exceeds number of available articles!')
# 3. Randomly select the batch.
thisBatch <- sample(codingPool, batchSize)
# 4. Update codingStatus column for these ids to 'withCoder'.
# 5. and update 'coder' column with coder's initials.
# first make changes
articles_mod <- articlesData %>%
mutate(codingStatus = ifelse(id %in% thisBatch, 'withCoder', codingStatus),
coder = ifelse(id %in% thisBatch, thisCoder, coder))
# now write new csv file
write.csv(articles_mod, 'articles_mod.csv', row.names = F)
# now remove old sheet and upload new sheet
gs_upload("articles_mod.csv", sheet_title = 'Articles', overwrite = T)
# 6. Output .csv file containing batch of article ids to send to coder
filename <- paste0("codingBatches/", thisCoder, "_", format(Sys.time(), "%d-%b_%H-%M-%S"), "_batch", ".csv")
coderInfo <- articlesData %>%
filter(id %in% thisBatch) %>%
select(id, authors, title, doi)
write.csv(coderInfo, filename, row.names = F)
print(paste0('Now e-mail the .csv file ', filename, ' to the coder.'))
}