Commit 8aafec6
authored
Fix guard ordering and over-broad grep in native_anywhere_in_australia() (#288)
Closes #280.
## 1. `is.null(resources)` guard ran too late
The guard sat *below* the call to
`create_species_state_origin_matrix()`. Because that function has its
own guard, an offline call didn't error outright — it did a pointless
call and then printed *"Not finding taxonomic resources; check internet
connection?"* twice, once from each function giving up. Moving the guard
to the top of the function makes it one message and no wasted work.
## 2. `apply(..., grepl("native", x))` scanned every column
`apply(full_lookup, 1, ...)` coerces the whole frame to a character
matrix, so the grep ran over `family`, `species` and `taxon_ID` as well
as the state columns. Any taxon whose name contained the substring
"native" would have been classified native regardless of its actual
distribution.
That is not hypothetical: APC 2024-10-11 has three taxa with a
`nativitatis` epithet — *Flickingeria nativitatis*, *Pittosporum
nativitatis* and *Ischaemum nativitatis*, all Christmas Island endemics.
All three happen to be genuinely native, so **no results change**, but
the check shouldn't depend on that coincidence.
The logic now lives in a small internal `is_native_anywhere()` that
selects the state columns explicitly and tests them column-wise.
Column-wise `grepl` also avoids the frame-to-matrix coercion, which
matters over the 30k+ rows of the infrataxa matrix.
## Verification
- Confirmed old and new native flags are `identical()` over the full
2024-10-11 state-origin matrix, so `benchmarks/native_check.csv` is
untouched.
- New test `is_native_anywhere() reads only the state columns` (in
`test-state_diversity.R`) uses a synthetic lookup with a `nativitatis`
name marked `naturalised`, which the old code got wrong. Needs no
resources.
- New test in `test-connection.R` pins the guard: `resources = NULL`
returns `NULL` with exactly one message.
- `test-state_diversity.R` and `test-connection.R` pass: 36 passing, 0
failures.
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent bb51629 commit 8aafec6
4 files changed
Lines changed: 51 additions & 11 deletions
File tree
- R
- tests/testthat
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 32 | + | |
36 | 33 | | |
37 | 34 | | |
38 | 35 | | |
39 | 36 | | |
40 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
51 | 49 | | |
52 | 50 | | |
53 | 51 | | |
| |||
65 | 63 | | |
66 | 64 | | |
67 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
83 | 94 | | |
84 | 95 | | |
85 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
68 | 83 | | |
69 | 84 | | |
70 | 85 | | |
| |||
0 commit comments