Skip to content

Commit 9b57eef

Browse files
authored
chore: code style (#153)
1 parent 07dfa35 commit 9b57eef

33 files changed

+880
-531
lines changed

.github/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.html

.github/workflows/document.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
push:
5+
paths: ["R/**"]
6+
7+
name: Document
8+
9+
jobs:
10+
document:
11+
runs-on: ubuntu-latest
12+
env:
13+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
14+
steps:
15+
- name: Checkout repo
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup R
21+
uses: r-lib/actions/setup-r@v2
22+
with:
23+
use-public-rspm: true
24+
25+
- name: Install dependencies
26+
uses: r-lib/actions/setup-r-dependencies@v2
27+
with:
28+
extra-packages: any::roxygen2
29+
needs: roxygen2
30+
31+
- name: Document
32+
run: roxygen2::roxygenise()
33+
shell: Rscript {0}
34+
35+
- name: Commit and push changes
36+
run: |
37+
git config --local user.name "$GITHUB_ACTOR"
38+
git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
39+
git add man/\* NAMESPACE DESCRIPTION
40+
git commit -m "Update documentation" || echo "No changes to commit"
41+
git pull --ff-only
42+
git push origin

.github/workflows/style.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
push:
5+
paths: ["**.[rR]", "**.[qrR]md", "**.[rR]markdown", "**.[rR]nw", "**.[rR]profile"]
6+
7+
name: Style
8+
9+
jobs:
10+
style:
11+
runs-on: ubuntu-latest
12+
env:
13+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
14+
steps:
15+
- name: Checkout repo
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Setup R
21+
uses: r-lib/actions/setup-r@v2
22+
with:
23+
use-public-rspm: true
24+
25+
- name: Install dependencies
26+
uses: r-lib/actions/setup-r-dependencies@v2
27+
with:
28+
extra-packages: any::styler, any::roxygen2
29+
needs: styler
30+
31+
- name: Enable styler cache
32+
run: styler::cache_activate()
33+
shell: Rscript {0}
34+
35+
- name: Determine cache location
36+
id: styler-location
37+
run: |
38+
cat(
39+
"location=",
40+
styler::cache_info(format = "tabular")$location,
41+
"\n",
42+
file = Sys.getenv("GITHUB_OUTPUT"),
43+
append = TRUE,
44+
sep = ""
45+
)
46+
shell: Rscript {0}
47+
48+
- name: Cache styler
49+
uses: actions/cache@v3
50+
with:
51+
path: ${{ steps.styler-location.outputs.location }}
52+
key: ${{ runner.os }}-styler-${{ github.sha }}
53+
restore-keys: |
54+
${{ runner.os }}-styler-
55+
${{ runner.os }}-
56+
57+
- name: Style
58+
run: styler::style_pkg()
59+
shell: Rscript {0}
60+
61+
- name: Commit and push changes
62+
run: |
63+
git config --local user.name "$GITHUB_ACTOR"
64+
git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com"
65+
git add \*.[rR] \*.[qrR]md \*.[rR]markdown \*.[rR]nw \*.[rR]profile
66+
git commit -m "Style code" || echo "No changes to commit"
67+
git pull --ff-only
68+
git push origin

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
## Internals
1111

1212
* {opencage} now uses [{testthat} 3e](https://testthat.r-lib.org/articles/third-edition.html) for unit tests ([#141](https://github.com/ropensci/opencage/issues/141)).
13-
* GitHub actions workflows updated ([#142](https://github.com/ropensci/opencage/issues/142), [#149](https://github.com/ropensci/opencage/pull/149), [#152](https://github.com/ropensci/opencage/pull/152)).
13+
* GitHub action workflows have been updated ([#142](https://github.com/ropensci/opencage/issues/142), [#149](https://github.com/ropensci/opencage/pull/149), [#152](https://github.com/ropensci/opencage/pull/152)). Styler and document workflows have been added ([#153](https://github.com/ropensci/opencage/pull/153)).
1414
* Use [{lintr} version 3.0](https://www.tidyverse.org/blog/2022/07/lintr-3-0-0/) and add "package development" linters ([#144](https://github.com/ropensci/opencage/pull/144)).
1515
* `countrycodes` source and script were moved to `data-raw` ([#146](https://github.com/ropensci/opencage/pull/146)).
1616
* Add CITATION.cff and a corresponding GitHub action ([#148](https://github.com/ropensci/opencage/pull/148)).
1717
* Select expressions inside `oc_forward_df()` and `oc_reverse_df()` now use `"column"` instead of `.data$column`, because the latter is [deprecated as of tidyselect v1.2.0](https://tidyselect.r-lib.org/news/index.html#tidyselect-120) ([#150](https://github.com/ropensci/opencage/pull/150)).
18+
* The opencage code now uses a consistent style (`styler::tidyverse_style()`) and all internal functions are documented ([#153](https://github.com/ropensci/opencage/pull/153)).
1819

1920
# opencage 0.2.2
2021

R/oc_bbox.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ oc_bbox.data.frame <- function(data, xmin, ymin, xmax, ymax, ...) {
101101
xmax <- rlang::enquo(xmax)
102102
ymax <- rlang::enquo(ymax)
103103

104-
oc_bbox(xmin = rlang::eval_tidy(xmin, data = data),
105-
ymin = rlang::eval_tidy(ymin, data = data),
106-
xmax = rlang::eval_tidy(xmax, data = data),
107-
ymax = rlang::eval_tidy(ymax, data = data))
104+
oc_bbox(
105+
xmin = rlang::eval_tidy(xmin, data = data),
106+
ymin = rlang::eval_tidy(ymin, data = data),
107+
xmax = rlang::eval_tidy(xmax, data = data),
108+
ymax = rlang::eval_tidy(ymax, data = data)
109+
)
108110
}
109111

110112
#' @name oc_bbox
@@ -122,7 +124,15 @@ oc_bbox.bbox <- function(bbox, ...) {
122124
list(bbox)
123125
}
124126

125-
# check bbox
127+
#' Check whether a bounding box has valid longitude and latitude values
128+
#'
129+
#' @param xmin numeric minimum longitude
130+
#' @param ymin numeric minimum latitude
131+
#' @param xmax numeric maximum longitude
132+
#' @param ymax numeric maximum latitude
133+
#'
134+
#' @noRd
135+
126136
oc_check_bbox <- function(xmin, ymin, xmax, ymax) {
127137
if (anyNA(c(xmin, ymin, xmax, ymax))) {
128138
stop("Every `bbox` element must be non-missing.", call. = FALSE)

R/oc_check_query.R

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
oc_check_logical <- function(variable, check_length_one = FALSE) {
2-
if (!is.null(variable)) {
3-
if (!is.logical(variable)) {
4-
var_name <- deparse(substitute(variable)) # deparse only if check fails
5-
stop("`", var_name, "` must be a logical vector.", call. = FALSE)
6-
} else if (check_length_one && !identical(length(variable), 1L)) {
7-
var_name <- deparse(substitute(variable)) # deparse only if check fails
8-
stop("`", var_name, "` must be a vector of length one.", call. = FALSE)
9-
}
10-
}
11-
}
12-
131
#' Check OpenCage query arguments
142
#'
153
#' Function that checks the query arguments passed to OpenCage
@@ -21,23 +9,21 @@ oc_check_logical <- function(variable, check_length_one = FALSE) {
219
#' @noRd
2210

2311
oc_check_query <-
24-
function(
25-
placename = NULL,
26-
latitude = NULL,
27-
longitude = NULL,
28-
bounds = NULL,
29-
proximity = NULL,
30-
countrycode = NULL,
31-
language = NULL,
32-
limit = NULL,
33-
min_confidence = NULL,
34-
no_annotations = NULL,
35-
roadinfo = NULL,
36-
no_dedupe = NULL,
37-
abbrv = NULL,
38-
address_only = NULL,
39-
add_request = NULL
40-
) {
12+
function(placename = NULL,
13+
latitude = NULL,
14+
longitude = NULL,
15+
bounds = NULL,
16+
proximity = NULL,
17+
countrycode = NULL,
18+
language = NULL,
19+
limit = NULL,
20+
min_confidence = NULL,
21+
no_annotations = NULL,
22+
roadinfo = NULL,
23+
no_dedupe = NULL,
24+
abbrv = NULL,
25+
address_only = NULL,
26+
add_request = NULL) {
4127
arglist <-
4228
purrr::compact(
4329
list(
@@ -62,8 +48,10 @@ oc_check_query <-
6248
# ensure arguments are length one or match length of placename/latitude
6349
arglngths <- lengths(arglist)
6450
if (!all(arglngths == arglngths[1] | arglngths == 1, na.rm = TRUE)) {
65-
stop(call. = FALSE, "All arguments must be of length one \n",
66-
"or of the same length as `placename` or `latitude`.")
51+
stop(
52+
call. = FALSE, "All arguments must be of length one \n",
53+
"or of the same length as `placename` or `latitude`."
54+
)
6755
}
6856

6957
purrr::pwalk(
@@ -73,23 +61,21 @@ oc_check_query <-
7361
}
7462

7563
.oc_check_query <-
76-
function(
77-
placename = NULL,
78-
latitude = NULL,
79-
longitude = NULL,
80-
bounds = NULL,
81-
proximity = NULL,
82-
countrycode = NULL,
83-
language = NULL,
84-
limit = NULL,
85-
min_confidence = NULL,
86-
no_annotations = NULL,
87-
roadinfo = NULL,
88-
no_dedupe = NULL,
89-
abbrv = NULL,
90-
address_only = NULL,
91-
add_request = NULL
92-
) {
64+
function(placename = NULL,
65+
latitude = NULL,
66+
longitude = NULL,
67+
bounds = NULL,
68+
proximity = NULL,
69+
countrycode = NULL,
70+
language = NULL,
71+
limit = NULL,
72+
min_confidence = NULL,
73+
no_annotations = NULL,
74+
roadinfo = NULL,
75+
no_dedupe = NULL,
76+
abbrv = NULL,
77+
address_only = NULL,
78+
add_request = NULL) {
9379
# check placename
9480
if (!is.null(placename) && !is.character(placename)) {
9581
stop("`placename` must be a character vector.", call. = FALSE)
@@ -123,7 +109,7 @@ oc_check_query <-
123109
)
124110
}
125111
if (!utils::hasName(proximity, "latitude") ||
126-
!utils::hasName(proximity, "longitude")) {
112+
!utils::hasName(proximity, "longitude")) {
127113
stop(
128114
call. = FALSE,
129115
"The coordinates of every `proximity` point must be named ",
@@ -184,8 +170,37 @@ oc_check_query <-
184170
oc_check_logical(address_only)
185171

186172
oc_check_logical(add_request)
173+
}
174+
175+
176+
#' Check whether an argument is a boolean and (optionally) of length one
177+
#'
178+
#' @param variable argument to check
179+
#' @param check_length_one boolean whether to check if the argument is of length
180+
#' one
181+
#'
182+
#' @noRd
187183

184+
oc_check_logical <- function(variable, check_length_one = FALSE) {
185+
if (!is.null(variable)) {
186+
if (!is.logical(variable)) {
187+
var_name <- deparse(substitute(variable)) # deparse only if check fails
188+
stop("`", var_name, "` must be a logical vector.", call. = FALSE)
189+
} else if (check_length_one && !identical(length(variable), 1L)) {
190+
var_name <- deparse(substitute(variable)) # deparse only if check fails
191+
stop("`", var_name, "` must be a vector of length one.", call. = FALSE)
192+
}
188193
}
194+
}
195+
196+
197+
#' Check whether a value is between two values
198+
#'
199+
#' @param x numeric value to check
200+
#' @param left numeric lower bound
201+
#' @param right numeric upper bound
202+
#'
203+
#' @noRd
189204

190205
oc_check_between <- function(x, left, right) {
191206
if (!is.numeric(x)) {
@@ -197,10 +212,10 @@ oc_check_between <- function(x, left, right) {
197212
deparse(substitute(x)),
198213
"` must be between ",
199214
left,
200-
" and ",
201-
right,
202-
".",
203-
call. = FALSE
215+
" and ",
216+
right,
217+
".",
218+
call. = FALSE
204219
)
205220
}
206221
}

R/oc_config.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@
9090
#'
9191
#' @export
9292
oc_config <-
93-
function(
94-
key = Sys.getenv("OPENCAGE_KEY"),
95-
rate_sec = getOption("oc_rate_sec", default = 1L),
96-
no_record = getOption("oc_no_record", default = TRUE),
97-
show_key = getOption("oc_show_key", default = FALSE),
98-
...) {
99-
93+
function(key = Sys.getenv("OPENCAGE_KEY"),
94+
rate_sec = getOption("oc_rate_sec", default = 1L),
95+
no_record = getOption("oc_no_record", default = TRUE),
96+
show_key = getOption("oc_show_key", default = FALSE),
97+
...) {
10098
key_needed <-
10199
c(
102100
"Using the OpenCage Geocoder requires a valid API key.\n",

0 commit comments

Comments
 (0)