File: utils.R

package info (click to toggle)
r-cran-profvis 0.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: javascript: 1,943; ansic: 41; sh: 13; makefile: 8
file content (61 lines) | stat: -rw-r--r-- 1,317 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Drop NULLs from a list
drop_nulls <- function(x) {
  x[!vapply(x, is.null, logical(1))]
}

# Everything above this function in the stack will be hidden by default in the
# flamegraph.
..stacktraceoff.. <- function(x) x

modal_value0 <- function(x) {
  if (!length(x)) {
    return(NULL)
  }

  if (is.list(x)) {
    abort("Can't use `modal_value()` with lists.")
  }
  self_split <- unname(split(x, x))

  lens <- lengths(self_split)
  max_locs <- which(lens == max(lens))

  if (length(max_locs) != 1) {
    return(NULL)
  }

  modal <- self_split[[max_locs]]
  modal[[1]]
}
modal_value <- function(x) {
  modal_value0(x) %||% abort("Expected modal value.")
}

enquo0_list <- function(arg) {
  quo <- inject(enquo0(!!substitute(arg)), caller_env())

  # Warn if there are any embedded quosures as these are not supported
  quo_squash(quo, warn = TRUE)

  list(
    expr = quo_get_expr(quo),
    env = quo_get_env(quo)
  )
}


split_in_half <- function(x, pattern, fixed = FALSE, perl = FALSE) {
  pos <- regexpr(pattern, x, fixed = fixed, perl = perl)
  
  start <- as.vector(pos) - 1
  length <- attr(pos, "match.length")
  
  no_match <- !is.na(pos) & pos == -1L
  length[no_match] <- 0
  start[no_match] <- nchar(x)[no_match]

  cbind(
    substr(x, 1, start),
    substr(x, start + length + 1, nchar(x))
  )
}