1010# ' please consider [registering for an API
1111# ' key](https://api.delphi.cmu.edu/epidata/admin/registration_form).
1212# '
13- # ' API keys are strings and can be set in two ways. If the environment variable
14- # ' `DELPHI_EPIDATA_KEY` is set, it will be used automatically. Environment
15- # ' variables can be set either in the shell or by editing your `.Renviron` file,
16- # ' which will ensure the setting applies to all R sessions. See `?Startup` for a
17- # ' description of `Renviron` files and where they can be placed.
18- # '
19- # ' Alternately, the API key can be set from within a session by using
20- # ' `set_api_key()`, which sets the R option `delphi.epidata.key` using
21- # ' `options()`. If this option is set, it is used in preference to the
22- # ' environment variable, so you may change keys within an R session. R options
23- # ' are not preserved between sessions, so `set_api_key()` must be run every time
24- # ' you open R. Alternately, you can have R set the option at startup by adding
25- # ' it to your `.Rprofile`; see `?Startup` for a description of `Rprofile` files
26- # ' and where they can be placed.
13+ # ' API keys are strings read from the environment variable `DELPHI_EPIDATA_KEY`.
14+ # ' We recommend setting your key with `save_api_key()`, which will modify an
15+ # ' applicable `.Renviron` file, which will be read in automatically when you
16+ # ' start future R sessions (see [`?Startup`][base::Startup] for details on
17+ # ' `.Renviron` files). Alternatively, you can modify the environment variable at
18+ # ' the command line before/while launching R, or inside an R session with
19+ # ' [`Sys.setenv()`], but these will not persist across sessions.
2720# '
2821# ' Once an API key is set, it is automatically used for all requests made by
2922# ' functions in this package.
3023# '
3124# ' @return For `get_api_key()`, returns the current API key as a string, or
3225# ' `""` if none is set.
3326# '
34- # ' @seealso [usethis::edit_r_environ()] to automatically edit the `.Renviron`
35- # ' file; [usethis::edit_r_profile()] to automatically edit the `.Rprofile`
36- # ' file
37- # '
38- # ' @references Delphi Epidata API Keys documentation.
39- # ' <https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html>
27+ # ' @references
28+ # ' - [Delphi Epidata API Keys
29+ # ' documentation](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html).
30+ # ' - [Delphi Epidata API Registration
31+ # ' Form](https://api.delphi.cmu.edu/epidata/admin/registration_form).
4032# '
41- # ' Delphi Epidata API Registration Form.
42- # ' <https://api.delphi.cmu.edu/epidata/admin/registration_form>
4333# ' @export
4434get_api_key <- function () {
45- key <- getOption(" delphi.epidata.key" , default = " " )
46- if (key != " " ) {
47- return (key )
48- }
49-
5035 key <- Sys.getenv(" DELPHI_EPIDATA_KEY" , unset = " " )
5136 if (key != " " ) {
5237 return (key )
@@ -55,7 +40,7 @@ get_api_key <- function() {
5540 cli :: cli_warn(
5641 c(
5742 " No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed." ,
58- " i" = " See {.help set_api_key } for details on obtaining and setting API keys."
43+ " i" = " See {.help save_api_key } for details on obtaining and setting API keys."
5944 ),
6045 .frequency = " regularly" ,
6146 .frequency_id = " delphi.epidata.key"
@@ -64,8 +49,29 @@ get_api_key <- function() {
6449}
6550
6651# ' @rdname get_api_key
67- # ' @param key API key to use for future requests
6852# ' @export
69- set_api_key <- function (key ) {
70- options(delphi.epidata.key = key )
53+ save_api_key <- function () {
54+ cli :: cli_inform(
55+ c(
56+ " i" = " This function will open your {.code .Renviron} file in a text editor. You will need to
57+ write {.code DELPHI_EPIDATA_KEY=yourkeyhere} (without quotes) in the file and save it. If the editor
58+ does not open, you will need to edit the file manually." ,
59+ " i" = " Press enter to continue."
60+ )
61+ )
62+ readline()
63+
64+ if (file.exists(usethis :: proj_path(" .Renviron" ))) {
65+ usethis :: edit_r_environ(scope = " project" )
66+
67+ cat(" \n\n " )
68+ cli :: cli_inform(
69+ c(
70+ " i" = " Your project {.code .Renviron} file has been updated. Make sure not to share this
71+ file (add it to .gitignore or equivalents)."
72+ )
73+ )
74+ } else {
75+ usethis :: edit_r_environ(scope = " user" )
76+ }
7177}
0 commit comments