File: utils.R

package info (click to toggle)
r-cran-rstudioapi 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,517 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

`%||%` <- function(x, y) {
  if (is.null(x)) y else x
}

renderTemplate <- function(template, data) {

  rendered <- template

  for (i in seq_along(data)) {
    key <- names(data)[[i]]
    val <- data[[i]]
    fkey <- sprintf("${%s}", key)
    rendered <- gsub(fkey, val, rendered, fixed = TRUE)
  }

  rendered

}

checkApiArguments <- function(fname, f, args) {

  # allow opt-out just in case
  enabled <- getOption("rstudioapi.checkApiArguments", default = TRUE)
  if (identical(enabled, FALSE))
    return(args)

  # check for arguments supplied by the user that aren't available
  # in the current version of RStudio
  unsupportedArgNames <- setdiff(names(args), names(formals(f)))
  if (length(unsupportedArgNames) == 0L)
    return(args)

  # check which of these arguments is NULL; if all arguments are NULL,
  # then we accept the call
  isNullArg <- vapply(args[unsupportedArgNames], is.null, FUN.VALUE = logical(1))
  badArgNames <- names(isNullArg)[!isNullArg]
  if (length(badArgNames) == 0L) {
    args[unsupportedArgNames] <- NULL
    return(args)
  }

  # if we get here, the user tried to supply a value for a parameter that isn't
  # supported in this version of RStudio; emit an error
  fmt <- ifelse(
    length(badArgNames) == 1L,
    "Parameter %s is not supported by %s in this version of RStudio.",
    "Parameters %s are not supported by %s in this version of RStudio."
  )

  msg <- sprintf(fmt, paste(shQuote(badArgNames), collapse = ", "), shQuote(fname))
  stop(msg, call. = FALSE)

}