-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
385 lines (297 loc) · 10.9 KB
/
functions.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
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
#Functions
library(stopwords)
#Remove custom words
custom_remove_words<-function(text, pattern) {
text <- unlist(strsplit(text, " ")) # Split text into words
text <- text[!str_detect(text, pattern)] # Remove words matching the pattern
text <- paste(text, collapse = " ") # Reconstruct the text
return(text)
}
#remove punctuation unless the period is next to a number
custom_remove_punctuation_everything_else <- function(text) {
# Define a regular expression to match and remove periods (.) unless they are next to numbers
pattern <- "(?<![0-9])\\.(?![0-9])|([^A-Za-z0-9.-])"
# Use gsub to replace the matched characters with spaces
cleaned_text <- gsub(pattern, " ", text, perl = TRUE)
return(cleaned_text)
}
#remove punctuation unless it's next to a hyphen or a number
custom_remove_punctuation_preseve_number<- function(text) {
# Define a regular expression to match and remove punctuation but preserve hyphens and numbers
pattern <- "([^A-Za-z0-9.-])"
# Use gsub to replace the matched characters with spaces
cleaned_text <- gsub(pattern, " ", text)
return(cleaned_text)
}
## create custom function to remove other misc characters
text_preprocessing<- function(x)
{gsub('http\\S+\\s*','',x) # remove URLs
gsub('wwww\\S+\\s*','',x) # remove URLs
gsub('#\\S+','',x) # remove hashtags
gsub('[[:cntrl:]]','',x) # remove controls and special characters
gsub("^[[:space:]]*","",x) # remove leading whitespaces
gsub("[[:space:]]*$","",x) # remove trailing whitespaces
gsub(' +', ' ', x) # remove extra whitespaces
}
#Specifying stop words
stop_words<-stopwords("english")
# Custom function to remove stopwords unless they are part of hyphenated words
custom_remove_stopwords <- function(text, stopwords) {
words <- unlist(str_split(text, "\\s+"))
cleaned_words <- character(0)
for (word in words) {
if (str_detect(word, "-")) {
# If the word contains a hyphen, keep it
cleaned_words <- c(cleaned_words, word)
} else if (!(word %in% stopwords)) {
# If the word is not a stop word, keep it
cleaned_words <- c(cleaned_words, word)
}
}
# Reconstruct the text
cleaned_text <- paste(cleaned_words, collapse = " ")
return(cleaned_text)
}
#Removing numbers without hyphens
remove_standalone_numbers <- function(text) {
words <- unlist(strsplit(text, "\\s+"))
words <- Filter(function(word) !grepl("^\\d+$", word), words)
cleaned_text <- paste(words, collapse = " ")
return(cleaned_text)
}
#make text lower case and change _ into a space
clean_up<-function(text=type){
new_text<-str_replace_all(tolower(text), "_", " ")
}
#Make the first letter capital
title<-function(text=type){
new_text<-StrCap(text)
}
# Define a function to remove duplicates within a cell
remove_duplicates_within_cell <- function(cell_text) {
words <- unlist(strsplit(cell_text, ","))
unique_words <- unique(words)
return(paste(unique_words, collapse = ", "))
}
#Extract cleaning from mural data for those with only one level
extract_cleaning_one_level<-function (raw=data_applied){
df<-raw %>%
clean_names()
colnames <-df[1,] %>%
t() %>%
as.data.frame()
colnames<-colnames %>%
mutate(cat=row.names(colnames)) %>%
rename(group=V1) %>%
mutate(cat=gsub("[0-9]","",cat))
rownames(colnames)<-NULL
tab_df<-df %>%
row_to_names(1) %>%
pivot_longer(everything(),names_to="group", values_to="desc") %>%
separate(desc, into=c("study_id", "desc"), sep="]-") %>%
mutate(study_id=(gsub("\\[|\\]", "", study_id))) %>%
mutate(study_id=(gsub("['\"]", "", study_id))) %>%
filter(!is.na(study_id)) %>%
left_join(colnames)
print(tab_df)
table_findings<-tab_df %>%
left_join(colnames) %>%
group_by(study_id) %>%
# mutate(desc=paste(desc,collapse=".")) %>%
distinct(group, cat, desc, .keep_all = TRUE) %>%
mutate_at(vars(!starts_with(c("a", "d"))), list(clean_up)) %>%
mutate_at(vars(!starts_with(c("a","d"))), list(title)) %>%
group_by(study_id) %>%
mutate(group=paste(group, collapse= ","),
cat=paste(cat, collapse=",")) %>%
distinct(study_id, .keep_all = TRUE) %>%
left_join(demog, by=c("study_id"="covidence_number")) %>%
mutate_if(is.character, funs(remove_duplicates_within_cell)) %>%
ungroup() %>%
select(author, type, collection, country, speciality, cat, group, desc)
print(table_findings)
# Save the result into the global environment
assign("table_findings", table_findings, envir = .GlobalEnv)
}
#extract cleaning from mural data for those with two levels (those with barriers and facilitators)
extract_cleaning_twolevels<-function(raw=barriers) {
df<-raw %>%
clean_names()
colnames <-df[1:2,] %>%
t() %>%
as.data.frame()
colnames<-colnames %>%
mutate(cat=row.names(colnames)) %>%
rename(group=V1, lowergroup=V2) %>%
mutate(cat=gsub("[0-9]","",cat))
rownames(colnames)<-NULL
tab_df<-df %>%
row_to_names(2) %>%
pivot_longer(everything(),names_to="lowergroup", values_to="desc") %>%
separate(desc, into=c("study_id", "desc"), sep="]-") %>%
mutate(study_id=(gsub("\\[|\\]", "", study_id))) %>%
mutate(study_id=(gsub("['\"]", "", study_id))) %>%
filter(!is.na(study_id))
print(tab_df)
table_findings<-tab_df %>%
left_join(colnames) %>%
group_by(study_id) %>%
mutate(desc=paste(desc,collapse=".")) %>%
distinct(group, cat, desc, .keep_all = TRUE) %>%
mutate_at(vars(!starts_with(c("a", "d"))), list(clean_up)) %>%
mutate_at(vars(!starts_with(c("a","d"))), list(title)) %>%
group_by(study_id) %>%
mutate(lowergroup=paste(lowergroup, collapse = ","),
group=paste(group, collapse= ","),
cat=paste(cat, collapse=",")) %>%
distinct(study_id, .keep_all = TRUE) %>%
left_join(demog, by=c("study_id"="covidence_number")) %>%
mutate_if(is.character, funs(remove_duplicates_within_cell)) %>%
ungroup() %>%
select(author, type, collection, country, speciality, cat, group, lowergroup, desc)
print(table_findings)
# Save the result into the global environment
assign("table_findings", table_findings, envir = .GlobalEnv)
}
# start landscape
start.landscape=function(doc){
doc=body_end_section_continuous(doc)
return("landscape orientation started")
}
# end landscape
end.landscape=function(doc){
doc=body_end_section_landscape(doc)
return("landscape orientation ended")
}
split_into_sentences <- function(text) {
# Define a regular expression pattern for sentence splitting
sentence_pattern <- "\\s*[.!?]\\s*"
# Split the text into sentences using the pattern
sentences <- unlist(strsplit(text, sentence_pattern))
# Remove empty sentences
sentences <- sentences[sentences != ""]
return(sentences)
}
#Colours
THF_red <- '#dd0031'
THF_50pct_light_blue <- '#53a9cd'
# Secondary palette
THF_1_purple <- '#744284'
THF_2_yellow <- '#ffd412'
THF_3_teal <- '#2a7979'
THF_4_coral <- '#ee9b90'
THF_5_darkgreen <- '#0c402b'
THF_6_turquoise <- '#a6d7d3'
THF_7_blue <- '#005078'
THF_8_orange <- '#f39214'
THF_9_green <- '#2ca365'
# Tertiary palette
# to show changes of scale within data category
THF_75pct_rose <- '#ee7375'
THF_50pct_rose <- '#f2a0a2'
THF_75pct_light_blue <- '#7fbfda'
pal_THF_cont <- c(THF_red, THF_50pct_rose, THF_75pct_rose, THF_50pct_light_blue, THF_75pct_light_blue, THF_7_blue)
#table from mural
#table from mural
tally_one_level<-function (raw=data_applied){
df<-raw %>%
clean_names()
colnames <-df[1,] %>%
t() %>%
as.data.frame()
colnames<-colnames %>%
mutate(cat=row.names(colnames)) %>%
rename(group=V1) %>%
mutate(cat=gsub("[0-9]","",cat))
rownames(colnames)<-NULL
tab_df<-df %>%
row_to_names(1) %>%
pivot_longer(everything(),names_to="group", values_to="desc") %>%
separate(desc, into=c("study_id", "desc"), sep="]-") %>%
mutate(study_id=(gsub("\\[|\\]", "", study_id))) %>%
mutate(study_id=(gsub("['\"]", "", study_id))) %>%
filter(!is.na(study_id)) %>%
left_join(colnames) %>%
select(study_id, cat, group) %>%
group_by(study_id, cat) %>%
mutate(count=1,
dups_cat=cumsum(count)) %>%
left_join (demog %>%
ungroup() %>%
select(study_id=covidence_number, type, author, title)) %>%
ungroup()
tally_findings<-tab_df %>%
filter(dups_cat<2) %>%
select(cat, type) %>%
tbl_summary(by=type) %>%
add_overall() %>%
as_tibble()
# Save the result into the global environment
assign("tally_findings", tally_findings, envir = .GlobalEnv)
}
references_one_level<-function(raw=results){
df<-raw %>%
clean_names()
colnames <-df[1,] %>%
t() %>%
as.data.frame()
colnames<-colnames %>%
mutate(cat=row.names(colnames)) %>%
rename(group=V1) %>%
mutate(cat=gsub("[0-9]","",cat))
rownames(colnames)<-NULL
tab_df<-df %>%
row_to_names(1) %>%
pivot_longer(everything(),names_to="group", values_to="desc") %>%
separate(desc, into=c("study_id", "desc"), sep="]-") %>%
mutate(study_id=(gsub("\\[|\\]", "", study_id))) %>%
mutate(study_id=(gsub("['\"]", "", study_id))) %>%
filter(!is.na(study_id)) %>%
left_join(colnames) %>%
select(study_id, cat, group) %>%
group_by(study_id, cat) %>%
mutate(count=1,
dups_cat=cumsum(count)) %>%
left_join (demog %>%
ungroup() %>%
select(study_id=covidence_number, type, author, title)) %>%
ungroup()
assign("tab_df", tab_df, envir = .GlobalEnv)
}
references_two_level<-function(raw=barriers){
df<-raw %>%
clean_names()
colnames <-df[1:2,] %>%
t() %>%
as.data.frame()
colnames<-colnames %>%
mutate(cat=row.names(colnames)) %>%
rename(group=V1, lowergroup=V2) %>%
mutate(cat=gsub("[0-9]","",cat))
rownames(colnames)<-NULL
tab_df<-df %>%
row_to_names(2) %>%
pivot_longer(everything(),names_to="lowergroup", values_to="desc") %>%
separate(desc, into=c("study_id", "desc"), sep="]-") %>%
mutate(study_id=(gsub("\\[|\\]", "", study_id))) %>%
mutate(study_id=(gsub("['\"]", "", study_id))) %>%
filter(!is.na(study_id)) %>%
left_join(colnames) %>%
select(study_id, cat, group, lowergroup) %>%
group_by(study_id, cat, group, lowergroup) %>%
mutate(dups_lowergroups=ifelse(n()>1, 1,0)) %>%
ungroup() %>%
group_by(study_id, cat, group) %>%
mutate(dups_groups=ifelse(n()>1,1,0)) %>%
ungroup() %>%
group_by(study_id, cat) %>%
mutate(dups_cat=ifelse(n()>1,1,0)) %>%
ungroup %>%
filter(dups_lowergroups==0) %>%
distinct() %>%
left_join (demog %>%
ungroup() %>%
select(study_id=covidence_number, author)) %>%
ungroup()
assign("tab_df", tab_df, envir = .GlobalEnv)
}