File: entities.R

package info (click to toggle)
r-cran-rtweet 2.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,068 kB
  • sloc: sh: 13; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download
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
#' Extract methods
#'
#' Extract entities of the tweets linked to a tweet id.
#'
#' The position of where does this occur is not provided.
#' @param x A tweets object of the rtweet package.
#' @param entity A entity to extract data from.
#' @param ... Other possible arguments currently ignored.
#' @return Some information about those entities and the tweet id it comes from.
#' for users mentions the ids of the mentioned users are "user_id", "user_id_str"
#' (not "id_str")
#' @export
entity <- function(x, entity, ...) {
  UseMethod("entity")
}

#' @export
entity.default <- function(x, entity, ...) {
  entity <- match.arg(entity, c("urls", "hashtags", "symbols",
                                "user_mentions", "media"))
  l <- lapply(x, function(x){x[[entity]]})
  l
}


#' @export
entity.tweets <- function(x, entity, ...) {
  entity <- match.arg(entity, c("urls", "hashtags", "symbols",
                                "user_mentions", "media"))

  out <- .entity(x, entity)

  if (entity == "user_mentions") {
    colnames(out) <- c("id_str", "screen_name", "name", "user_id", "user_id_str")
  }
  out
}

.entity <- function(x, name = "urls") {
  ids <- ids(x)
  ent <- lapply(x$entities, function(x) {list_minus(x[[name]], "indices")})
  n_ent <- vapply(ent, NROW, numeric(1L))
  ids <- rep(ids, n_ent)
  ent <- do.call(rbind, ent)
  cbind.data.frame(id_str = ids, ent)
}