File: utils.R

package info (click to toggle)
r-cran-xml2 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 976 kB
  • sloc: cpp: 1,826; xml: 333; javascript: 238; ansic: 178; sh: 71; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 2,119 bytes parent folder | download | duplicates (2)
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
`%||%` <- function(a, b) if (is.null(a)) b else a

is_named <- function(x) {
  all(has_names(x))
}

has_names <- function(x) {
  nms <- names(x)
  if (is.null(nms)) {
    rep(FALSE, length(x))
  } else {
    !(is.na(nms) | nms == "")
  }
}

# non smart quote version of sQuote
quote_str <- function(x, quote = "\"") {
  if (!length(x)) {
    return(character(0))
  }

  paste0(quote, x, quote)
}

# Format the C bitwise flags for display in Rd. The input object is a named
# integer vector with a 'descriptions' character vector attribute that
# corresponds to each flag.
describe_options <- function(x) {
  paste0(
    "\\describe{\n",
    paste0("  \\item{", names(x), "}{", attr(x, "descriptions"), "}", collapse = "\n"),
    "\n}"
  )
}

s_quote <- function(x) paste0("'", x, "'")

# Similar to match.arg, but returns character() with NULL or empty input and
# errors if any of the inputs are not found (fixing
# https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16659)
parse_options <- function(arg, options, error_call = caller_env()) {
  if (is.numeric(arg)) {
    return(as.integer(arg))
  }

  if (is.null(arg) || !any(nzchar(arg))) {
    return(0L)
  }

  # set duplicates.ok = TRUE so any duplicates are counted differently than
  # non-matches, then take only unique results
  i <- pmatch(arg, names(options), duplicates.ok = TRUE)
  if (anyNA(i)) {
    cli::cli_abort(c(
      x = "{.arg options} {.val {arg[is.na(i)][1L]}} is not a valid option.",
      i = "Valid options are one of {.or {.val {names(options)}}}.",
      i = "See {.help [read_html](xml2::read_html)} for all options."
    ),
    call = error_call)
  }
  sum(options[unique(i)])
}

#' Get path to a xml2 example
#'
#' xml2 comes bundled with a number of sample files in its \sQuote{inst/extdata}
#' directory. This function makes them easy to access.
#' @param path Name of file. If `NULL`, the example files will be listed.
#' @export
xml2_example <- function(path = NULL) {
  if (is.null(path)) {
    dir(system.file("extdata", package = "xml2"))
  } else {
    system.file("extdata", path, package = "xml2", mustWork = TRUE)
  }
}