This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
methods.R
81 lines (72 loc) · 1.98 KB
/
methods.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
# rbind ####
#' @export
rbind.tweets <- function(..., deparse.level = 1) {
if (...length() == 1) return(..1)
rt <- list(...)
ud <- lapply(rt, users_data)
udm <- do.call(rbind, ud)
rt <- do.call(rbind.data.frame, rt)
rt <- tibble::as_tibble(rt)
attr(rt, "users") <- tibble::as_tibble(udm)
class(rt) <- c("tweets", class(rt))
rt
}
#' @export
rbind.users <- function(..., deparse.level = 1) {
if (...length() == 1) return(..1)
rt <- list(...)
td <- lapply(rt, tweets_data)
tdm <- do.call(rbind, td)
rt <- do.call(rbind.data.frame, rt)
rt <- tibble::as_tibble(rt)
attr(rt, "tweets") <- tibble::as_tibble(tdm)
class(rt) <- c("users", class(rt))
rt
}
#' @export
rbind.page <- function(..., deparse.level = 1) {
if (...length() == 1) return(..1)
rt <- list(...)
tdm <- do.call(rbind.data.frame, rt)
m <- lapply(rt, attr, which = "meta", exact = TRUE)
coln <- lapply(m, colnames)
coln <- unique(unlist(coln, FALSE, FALSE))
mm <- matrix(nrow = length(m), ncol = length(coln))
colnames(mm) <- coln
mm <- as.data.frame(mm)
for (i in seq_along(m)) {
mm[i, colnames(m[[i]])] <- m[[i]][1, ]
}
attr(tdm, "meta") <- as.data.frame(mm)
m <- lapply(rt, attr, which = "summary", exact = TRUE)
coln <- lapply(m, colnames)
coln <- unique(unlist(coln, FALSE, FALSE))
mm <- matrix(nrow = length(m), ncol = length(coln))
colnames(mm) <- coln
mm <- as.data.frame(mm)
for (i in seq_along(m)) {
mm[i, colnames(m[[i]])] <- m[[i]][1, ]
}
attr(tdm, "summary") <- as.data.frame(mm)
class(tdm) <- c("result", class(tdm))
return(tdm)
}
# subset ####
#' @export
`[.tweets` <- function(x, i, j, ..., drop) {
tweets <- NextMethod()
keep_users <- missing(drop) || !drop
if (keep_users) {
attr(tweets, "users") <- users_data(x)[i, ]
}
tweets
}
#' @export
`[.users` <- function(x, i, j, ..., drop) {
users <- NextMethod()
keep_tweets <- missing(drop) || !drop
if (keep_tweets) {
attr(users, "tweets") <- tweets_data(x)[i, ]
}
users
}