Skip to content

Commit

Permalink
Moved URL creation from server to galah_download (#89)
Browse files Browse the repository at this point in the history
* Code to generate link in processing #87

* Renames Establishment means earlier in workflow to remove processing in server.R

* Moved renaming of voucher location to repository to earlier workflow and out of server. Removed creation of URL from server and directly using link field.

* Suppressed warnings from APCalign #87

* Removed URL creaton in downloadhandler

* Updated snapshot
  • Loading branch information
fontikar authored Mar 1, 2024
1 parent b510a3a commit 32fedc6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 42 deletions.
15 changes: 10 additions & 5 deletions R/galah_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,20 @@ retrieve_data <- function(taxon,
get_establishment_status <- function(ala_cleaned, taxon = taxon) {
if (taxon == "Plantae") {
resources <- APCalign::load_taxonomic_resources()
suppressWarnings(
lookup <-
APCalign::native_anywhere_in_australia(unique(ala_cleaned$Species), resources = resources)
)

lookup <- dplyr::rename(lookup, Species = species)

ala_cleaned <-
ala_cleaned |> dplyr::left_join(lookup, by = dplyr::join_by("Species"))

return(ala_cleaned)
}
if (taxon %in% c("Cicadoidea", "Marsupialia", "Odonata", "Papilionoidea")) {
ala_cleaned$native_anywhere_in_aus <- "native"
ala_cleaned$native_anywhere_in_aus[ala_cleaned$Species %in% c("Danaus plexippus", "Pieris rapae")] <-
"introduced"
return(ala_cleaned)
}
if (!taxon %in% c("Cicadoidea",
"Marsupialia",
Expand All @@ -189,6 +188,9 @@ get_establishment_status <- function(ala_cleaned, taxon = taxon) {
"Plantae")) {
ala_cleaned$native_anywhere_in_aus <- "unknown"
}
# Rename native_anywhere_in_aus
ala_cleaned <- dplyr::rename(ala_cleaned,"Establishment means" = native_anywhere_in_aus)

return(ala_cleaned)
}

Expand All @@ -211,7 +213,7 @@ process_data <- function(data) {
!stringr::str_detect(species, "spec.$")
) |>
dplyr::mutate(
voucher_location = dplyr::if_else(!is.na(references), references, institutionCode),
repository = dplyr::if_else(!is.na(references), references, institutionCode),
voucher_type = dplyr::case_when(
basisOfRecord == "PRESERVED_SPECIMEN" ~ "Collection",
!is.na(sounds) ~ "Audio",
Expand All @@ -232,10 +234,13 @@ process_data <- function(data) {
lat,
long,
voucher_type,
voucher_location,
repository,
recordedBy,
recordID
) |>
dplyr::mutate(link = dplyr::case_when(grepl("https", repository) ~ repository,
TRUE ~ paste0("https://biocache.ala.org.au/occurrences/", recordID))
) |>
janitor::clean_names("title")
}

Expand Down
5 changes: 4 additions & 1 deletion R/infinitylists-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ utils::globalVariables(
"str_detect",
"voucher_location",
"voucher_type",
"write.csv"
"write.csv",
"Link",
"Repository",
"Establishment means"
)
)
34 changes: 10 additions & 24 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ infinity_server <- function(...) {
total_family <- length(unique(data$Family))

native <-
dplyr::filter(data, native_anywhere_in_aus == "native")
dplyr::filter(data, `Establishment means` == "native")
if (nrow(native) > 0)
total_native_species <- length(unique(native$Species))
else
Expand Down Expand Up @@ -242,7 +242,7 @@ infinity_server <- function(...) {
N = integer(0),
Long = numeric(0),
Lat = numeric(0),
`Voucher location` = character(0),
`Repository` = character(0),
`Recorded by` = character(0),
Native = character(0)
)
Expand All @@ -266,28 +266,27 @@ infinity_server <- function(...) {
},
Lat = Lat[1],
Long = Long[1],
`Repository` = ifelse(
grepl("https", `Voucher Location`[1]),
Repository = ifelse(
grepl("https", Repository[1]),
paste0(
"<a href='",
`Voucher Location`[1],
Link[1],
"' target='_blank'>",
"iNat",
"</a>"
),
paste0(
"<a href='",
"https://biocache.ala.org.au/occurrences/",
`Record Id`[1],
Link[1],
"' target='_blank'>",
`Voucher Location`[1],
Repository[1],
"</a>"
)
),
`Recorded by` = `Recorded by`[1]
),
by = .(Species,
`Establishment means` = native_anywhere_in_aus,
`Establishment means`,
`Voucher type` = `Voucher Type`)]


Expand Down Expand Up @@ -354,25 +353,12 @@ infinity_server <- function(...) {
},
content = function(file) {
data <- intersect_data()
data$`Voucher Location` = ifelse(
grepl("https", data$`Voucher Location`),
data$`Voucher Location`
,
paste0(
"https://biocache.ala.org.au/occurrences/",
data$`Record Id`
)
)
data <-
dplyr::rename(data,
'Establishment means' = native_anywhere_in_aus,
'Repository' = `Voucher Location`)
# Fixing the date
collectionDate_partial = lubridate::ymd_hms(data$`Collection Date`, tz = "UTC", quiet = TRUE)
collectionDate_all = dplyr::if_else(
is.na(collectionDate_partial),
lubridate::ymd(data$`Collection Date`, tz = "UTC", quiet = TRUE),
collectionDate_partial
)
collectionDate_partial)
data$`Collection Date` <-
paste(
lubridate::year(collectionDate_all),
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/_snaps/galah_download.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Code
odonata
Output
# A tibble: 7 x 11
Species Genus Family `Collection Date` Lat Long `Voucher Type`
<chr> <chr> <chr> <dttm> <dbl> <dbl> <chr>
1 Adversaeschna bre~ Adve~ Aeshn~ 1924-10-01 00:00:00 -33.9 151. Collection
2 Diplacodes bipunc~ Dipl~ Libel~ 1924-02-23 00:00:00 -37.8 145. Collection
3 Austrolestes leda Aust~ Lesti~ 1924-11-06 00:00:00 -28.7 152. Collection
4 Synthemis tasmani~ Synt~ Synth~ 1924-01-01 00:00:00 -41.9 145. Collection
5 Austroaeschna par~ Aust~ Telep~ 1924-02-01 00:00:00 -42.1 145. Collection
6 Hemicordulia tau Hemi~ Cordu~ 1924-10-10 00:00:00 -28.7 152. Collection
7 Synthemis tasmani~ Synt~ Synth~ 1924-01-01 00:00:00 -41.9 145. Collection
# i 4 more variables: `Voucher Location` <chr>, `Recorded by` <chr>,
# `Record Id` <chr>, native_anywhere_in_aus <chr>
# A tibble: 7 x 12
Species Genus Family `Collection Date` Lat Long `Voucher Type` Repository
<chr> <chr> <chr> <dttm> <dbl> <dbl> <chr> <chr>
1 Advers~ Adve~ Aeshn~ 1924-10-01 00:00:00 -33.9 151. Collection AM
2 Diplac~ Dipl~ Libel~ 1924-02-23 00:00:00 -37.8 145. Collection NMV
3 Austro~ Aust~ Lesti~ 1924-11-06 00:00:00 -28.7 152. Collection QM
4 Synthe~ Synt~ Synth~ 1924-01-01 00:00:00 -41.9 145. Collection QM
5 Austro~ Aust~ Aeshn~ 1924-02-01 00:00:00 -42.1 145. Collection QM
6 Hemico~ Hemi~ Cordu~ 1924-10-10 00:00:00 -28.7 152. Collection QM
7 Synthe~ Synt~ Synth~ 1924-01-01 00:00:00 -41.9 145. Collection QM
# i 4 more variables: `Recorded by` <chr>, `Record Id` <chr>, Link <chr>,
# `Establishment means` <chr>

0 comments on commit 32fedc6

Please sign in to comment.