/
Merge.R
208 lines (168 loc) · 6.33 KB
/
Merge.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
#' @title Merge collected data files
#'
#' @param path Directory path of Collect data to merge. Default is the working directory.
#' @param pattern Regular expression (regex) for matching file names to merge.
#' @param unique Logical. Remove duplicates based on observation id. Default is \code{TRUE}.
#' @param rev Logical. Reverses order of observations before removing duplicates. If collect data is provided
#' chronologically then this should ensure the most recent copy of a duplicate is kept. Default is \code{TRUE}.
#' @param writeToFile Logical. Save data to a file in the current working directory. Default is \code{FALSE}.
#' @param verbose Logical. Output additional information. Default is \code{FALSE}.
#'
#' @return A merged Collect object.
#'
#' @aliases MergeFiles
#' @name MergeFiles
#' @export
MergeFiles <-
function(path = ".",
pattern = "(?-i).+?\\.rds$",
unique = TRUE,
rev = TRUE,
writeToFile = FALSE,
verbose = FALSE) {
msg <- f_verbose(verbose)
ts_ <- Sys.time()
merge_log <- c(
paste0("merge.files"),
paste0(format(ts_, "%a %b %d %X %Y")),
paste0(format(ts_, tz = "UTC", usetz = TRUE)), "",
paste0("path = ", path),
paste0("pattern = ", pattern),
paste0("unique = ", unique),
paste0("rev = ", rev),
paste0("writeToFile = ", writeToFile),
paste0("verbose = ", verbose), ""
)
msg("Merging collect files...\n")
if (!dir.exists(path)) stop("Path does not exist.")
files <- list.files(path = path, pattern = pattern, full.names = TRUE)
if (length(files) < 1) stop("No matching files found.")
merge_log <- append(merge_log, files)
msg(paste0("Matching files:\n", paste0(paste0("- ", files), collapse = "\n"), "\n"))
data <- purrr::reduce(lapply(files, readRDS), .f = Merge)
merge_log <- c(merge_log, paste0(format(Sys.time(), "%a %b %d %X %Y")))
if (writeToFile) write_output_file(data, "rds", "DataMergeFile", verbose = verbose, log = merge_log)
msg("Done.\n")
data
}
#' @title Merge collected data
#'
#' @param ... Collect data to merge.
#' @param unique Logical. Remove duplicates based on observation id. Default is \code{TRUE}.
#' @param rev Logical. Reverses order of observations before removing duplicates. If collect data is provided
#' chronologically then this should ensure the most recent copy of a duplicate is kept. Default is \code{TRUE}.
#' @param writeToFile Logical. Save data to a file in the current working directory. Default is \code{FALSE}.
#' @param verbose Logical. Output additional information. Default is \code{FALSE}.
#'
#' @return A merged Collect object.
#'
#' @aliases Merge
#' @name Merge
#' @export
Merge <- function(..., unique = TRUE, rev = TRUE, writeToFile = FALSE, verbose = FALSE) {
msg <- f_verbose(verbose)
ts_ <- Sys.time()
merge_log <- c(
paste0("merge.data"),
paste0("time: ", format(ts_, "%a %b %d %X %Y")),
paste0("utc: ", format(ts_, tz = "UTC", usetz = TRUE)), "",
paste0("unique = ", unique),
paste0("rev = ", rev),
paste0("writeToFile = ", writeToFile),
paste0("verbose = ", verbose), "",
paste0(as.list(match.call()), collapse = "\n")
)
msg("Merging collect data...\n")
merge_dots <- list(...)
merge_cls_list <- lapply(merge_dots, class)
if (!identical(unname(merge_cls_list[-length(merge_cls_list)]), unname(merge_cls_list[-1]))) {
stop("Collect data must be of the same class.", call. = FALSE)
}
merge_cls_data <- unlist(merge_cls_list[1])
class(merge_dots) <- merge_cls_data
UseMethod("Merge", merge_dots)
}
#' @noRd
#' @export
Merge.default <- function(...) {
stop("Unknown data type passed to merge.", call. = FALSE)
}
#' @noRd
#' @method Merge twitter
#' @export
Merge.twitter <- function(..., unique = TRUE, rev = TRUE, writeToFile = FALSE, verbose = FALSE) {
merge_dots <- merge_dots
merge_cls_data <- merge_cls_data
data <- purrr::reduce(merge_dots, .f = merge_twitter)
class(data) <- merge_cls_data
rev_twitter_df <- function(x) {
x$tweets <- rev_row_order(x$tweets)
x$users <- rev_row_order(x$users)
x
}
if (unique) {
if (rev) data <- rev_twitter_df(data)
data$tweets <- data$tweets |> dplyr::distinct(.data$status_id, .keep_all = TRUE)
data$users <- data$users |> dplyr::distinct(.data$id_str, .keep_all = TRUE)
if (rev) data <- rev_twitter_df(data)
}
if (writeToFile) write_output_file(data, "rds", "TwitterDataMerge", verbose = verbose, log = merge_log)
msg("Done.\n")
data
}
#' @noRd
#' @method Merge youtube
#' @export
Merge.youtube <- function(..., unique = TRUE, rev = TRUE, writeToFile = FALSE, verbose = FALSE) {
merge_cls_data <- merge_cls_data
data <- dplyr::bind_rows(...)
class(data) <- merge_cls_data
if (unique) {
if (rev) data <- rev_row_order(data)
data <- data |> dplyr::distinct(.data$VideoID, .data$CommentID, .keep_all = TRUE)
if (rev) data <- rev_row_order(data)
}
if (writeToFile) write_output_file(data, "rds", "YoutubeDataMerge", verbose = verbose, log = merge_log)
msg("Done.\n")
data
}
#' @noRd
#' @method Merge reddit
#' @export
Merge.reddit <- function(..., unique = TRUE, rev = TRUE, writeToFile = FALSE, verbose = FALSE) {
merge_cls_data <- merge_cls_data
data <- dplyr::bind_rows(...)
class(data) <- merge_cls_data
if (unique) {
if (rev) data <- rev_row_order(data)
data <- data |> dplyr::distinct(.data$subreddit, .data$thread_id, .data$comm_id, .keep_all = TRUE)
if (rev) data <- rev_row_order(data)
}
if (writeToFile) write_output_file(data, "rds", "RedditDataMerge", verbose = verbose, log = merge_log)
msg("Done.\n")
data
}
# #' @noRd
# #' @method Merge web
# #' @export
# Merge.web <- function(..., unique = TRUE, rev = TRUE, writeToFile = FALSE, verbose = FALSE) {
# merge_cls_data <- merge_cls_data
#
# data <- dplyr::bind_rows(...)
# class(data) <- merge_cls_data
#
# if (writeToFile) write_output_file(data, "rds", "WebDataMerge", verbose = verbose)
# msg("Done.\n")
#
# data
# }
merge_twitter <- function(x, y) {
# sometimes these logical twitter data columns are found as lists
x$tweets <- x$tweets |> twitter_fix_col_types()
y$tweets <- y$tweets |> twitter_fix_col_types()
list(tweets = dplyr::bind_rows(x$tweets, y$tweets),
users = dplyr::bind_rows(x$users, y$users))
}
rev_row_order <- function(x) {
x[nrow(x):1, ]
}