business-science / riingo
Showing 2 of 7 files from the diff.

@@ -16,9 +16,9 @@
Loading
16 16
# ------------------------------------------------------------------------------
17 17
# Error messages
18 18
19 -
glue_stop <- function(..., .sep = "") {
20 -
  msg <- glue::glue(..., .sep, .envir = parent.frame())
21 -
  stop(msg, call. = FALSE)
19 +
glue_stop <- function(..., .sep = "", .envir = parent.frame()) {
20 +
  msg <- glue::glue(..., .sep, .envir = .envir)
21 +
  abort(msg)
22 22
}
23 23
24 24
validate_not_all_null <- function(x) {

@@ -2,12 +2,16 @@
Loading
2 2
#'
3 3
#' There are two methods for setting your token, an environment variable in your
4 4
#' `.Renviron` file, or setting an option. If both are set, the environment
5 -
#' variable will always be used. See details for how to get started.
5 +
#' variable will always be used. It is encouraged to use the environment
6 +
#' variable approach, as this will persist between R sessions.
7 +
#' See details for how to get started.
6 8
#'
7 9
#' @param token Tiingo API token. A character.
8 10
#'
9 -
#' @details
11 +
#' @param inform A single logical. Should a message be displayed encouraging
12 +
#'   you to instead use an environment variable?
10 13
#'
14 +
#' @details
11 15
#' To use the Tiingo API, you must create an account and set an API token.
12 16
#' It is completely free to get started and use their free source of data.
13 17
#'
@@ -18,36 +22,72 @@
Loading
18 22
#'
19 23
#' With your API token in hand, you can do one of two things:
20 24
#'
21 -
#' * Set the API token with [riingo_set_token()]. This is only valid for the
22 -
#' current R session and must be done each time you open R.
23 -
#'
24 -
#' * Set the API token as the `RIINGO_TOKEN` environment variable in an `.Renviron` file.
25 -
#' This is what I recommend. The easiest way to access this file is with the `usethis` package.
26 -
#' Open it with `usethis::edit_r_environ()` and then add a line with `RIINGO_TOKEN = token_here`.
27 -
#' Do not put the token in quotes, and make sure to restart R once you have set it.
28 -
#' After that, you shouldn't have to worry about it again.
25 +
#' * Set the API token using the `RIINGO_TOKEN` environment variable in
26 +
#'   an `.Renviron` file. This is recommended. The easiest way to access
27 +
#'   this file is with `usethis::edit_r_environ()`. Add a line containing
28 +
#'   `RIINGO_TOKEN = <your-token-here>`. Do not put the token in quotes,
29 +
#'   and make sure to restart R once you have set it. After that, you shouldn't
30 +
#'   have to worry about it again.
29 31
#'
32 +
#' * Set the API token with [riingo_set_token()]. This is only valid for the
33 +
#'   current R session and must be done each time you open R.
30 34
#'
31 35
#' @rdname riingo_token
32 36
#' @export
33 -
#'
34 -
riingo_set_token <- function(token) {
37 +
riingo_set_token <- function(token, inform = TRUE) {
38 +
  if (!is_string(token)) {
39 +
    abort("`token` must be a single string.")
40 +
  }
41 +
42 +
  if (is_true(inform)) {
43 +
    msg <- glue(
44 +
      "Token has been set for this R session only. ",
45 +
      "It is recommended to instead set the `RIINGO_TOKEN` environment variable ",
46 +
      "in an `.Renviron` file using `usethis::edit_r_environ()`, ",
47 +
      "and then restart R."
48 +
    )
49 +
50 +
    inform(msg)
51 +
  }
52 +
35 53
  options(riingo_token = token)
36 54
}
37 55
38 56
#' @rdname riingo_token
39 57
#' @export
40 58
riingo_get_token <- function() {
41 -
  token <- Sys.getenv("RIINGO_TOKEN")
59 +
  token <- get_env_var("RIINGO_TOKEN")
42 60
43 -
  if(token == "") {
44 -
    token <- getOption("riingo_token")
45 -
    if(is.null(token)) token <- ""
61 +
  if (!is.null(token)) {
62 +
    return(token)
46 63
  }
47 64
48 -
  if(token == "") {
49 -
    stop("Please set a token using riingo_set_token() or by setting the variable RIINGO_TOKEN in a .Renviron file and then restarting R.")
65 +
  token <- get_option("riingo_token")
66 +
67 +
  if (!is.null(token)) {
68 +
    return(token)
69 +
  }
70 +
71 +
  msg <- glue(
72 +
    "Please set the `RIINGO_TOKEN` environment variable in an `.Renviron` ",
73 +
    "file using `usethis::edit_r_environ()`, and then restart R. ",
74 +
    "Alternatively, set a once-per-session token using `riingo_set_token()`."
75 +
  )
76 +
77 +
  glue_stop(msg)
78 +
}
79 +
80 +
81 +
get_env_var <- function(x) {
82 +
  out <- Sys.getenv(x, unset = "")
83 +
84 +
  if (identical(out, "")) {
85 +
    NULL
86 +
  } else {
87 +
    out
50 88
  }
89 +
}
51 90
52 -
  token
91 +
get_option <- function(x) {
92 +
  getOption(x, default = NULL)
53 93
}
Files Coverage
R 14.64%
Project Totals (14 files) 14.64%
1
comment: false
2

3
coverage:
4
  status:
5
    project:
6
      default:
7
        target: auto
8
        threshold: 1%
9
        informational: true
10
    patch:
11
      default:
12
        target: auto
13
        threshold: 1%
14
        informational: true
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading