Skip to content

Commit 4042a5e

Browse files
committed
✨ vegetation_transect validation and injection
1 parent a45bc75 commit 4042a5e

10 files changed

+333
-29
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: rcoleo
22
Title: rcoleo
3-
Version: 2.2.0
3+
Version: 2.3.0
44
Authors@R: c(
55
person("Steve", "Vissault", , "steve.vissault@usherbrooke.ca", role = "aut",
66
comment = c(ORCID = "0000-0002-0866-4376")),

R/coleo_get_required_tables.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ tibble::tribble(
1313
"cells", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1414
"sites", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1515
"campaigns", 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1,
16-
"efforts", 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
16+
"efforts", 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
1717
"environments", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1818
"devices", 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1919
"lures", 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2020
"traps", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21-
"landmarks", 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1,
21+
"landmarks", 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1,
2222
"samples", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2323
"thermographs", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
2424
"observations", 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1,
25-
"observations_efforts_lookup", 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
26-
"observations_landmarks_lookup", 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1,
25+
"observations_efforts_lookup", 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
26+
"observations_landmarks_lookup", 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1,
2727
"obs_species", 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
2828
"attributes", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2929
"obs_soil_decomposition", 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,

R/coleo_inject.R

+104-19
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,41 @@ coleo_inject <- function(df, media_path = NULL, schema = 'public') {
4646
return(df_id)
4747
}
4848

49-
#--------------------------------------------------------------------------
49+
#==========================================================================
5050
# 1. Extract tables to be injected
51-
#--------------------------------------------------------------------------
51+
#==========================================================================
5252
campaign_type <- unique(df$campaigns_type)
5353
tables <- coleo_return_required_tables(campaign_type)
5454

55-
# Remove observations_lookup table for vegetation campaigns that do not have efforts
56-
# or landmarks
57-
## observations_efforts_lookup
58-
if (campaign_type == "v\u00e9g\u00e9tation_transect" & !any(grepl("efforts", names(df)))) {
59-
tables <- tables[!tables %in% "observations_efforts_lookup"]
60-
}
61-
## observations_landmarks_lookup
62-
if (campaign_type == "v\u00e9g\u00e9tation_transect" & !any(grepl("landmarks", names(df)))) {
63-
tables <- tables[!tables %in% "observations_landmarks_lookup"]
64-
}
65-
66-
#--------------------------------------------------------------------------
55+
#==========================================================================
6756
# 2. Inject campaigns table
68-
#--------------------------------------------------------------------------
69-
df_id <- coleo_inject_table(df, "campaigns", schema = schema)
57+
#
58+
# - Campaigns table are injected differently for mammifères campaigns
59+
#==========================================================================
60+
if (campaign_type == "v\u00e9g\u00e9tation_transect") {
61+
# Remove observations_lookup table for vegetation campaigns that do not have efforts
62+
# or landmarks
63+
## observations_efforts_lookup
64+
if (!any(grepl("efforts", names(df)))) {
65+
tables <- tables[!tables %in% "observations_efforts_lookup"]
66+
}
67+
## observations_landmarks_lookup
68+
if (!any(grepl("landmarks", names(df)))) {
69+
tables <- tables[!tables %in% "landmarks"]
70+
tables <- tables[!tables %in% "observations_landmarks_lookup"]
71+
}
72+
df_id <- coleo_inject_vegetation_transect_campaigns(df)
73+
} else {
74+
df_id <- coleo_inject_table(df, "campaigns", schema = schema)
75+
}
7076

7177
if(!any(sapply(df_id$campaign_error, is.null))) {
7278
cat("Only data for successfully injected campaigns are injected in the next tables. These following lines failed to inject: ", paste0(which(!sapply(df_id$campaign_error, is.null)), collapse = ", "), "\n")
73-
}
79+
}
7480

75-
#--------------------------------------------------------------------------
81+
#==========================================================================
7682
# 3. Inject other tables
77-
#--------------------------------------------------------------------------
83+
#==========================================================================
7884
for (table in tables[-1]) {
7985

8086
# Injection of taxa_name in ref_species table
@@ -729,4 +735,83 @@ coleo_inject_ref_species <- function(taxa_col, schema = 'public') {
729735
coleo_injection_prep(db_table = "ref_species", schema = schema) |>
730736
coleo_injection_execute()
731737
}
738+
}
739+
740+
741+
#' Injection des campagnes végétation_transect dans la table campaigns de coleo.
742+
#'
743+
#' Les campagnes de transects de végétation nécessitent tout d'abord de vérifier si les campagnes existent.
744+
#'
745+
#' Accepte un data.frame validé par \code{\link[rcoleo]{coleo_validate}} et performe
746+
#' l'injection de la table campaigns.
747+
#'
748+
#' La fonction est utilisée par \code{\link[rcoleo]{coleo_inject}}.
749+
#'
750+
#' @param df_id Un data.frame contenant les données de végétation_transect à injecter.
751+
#' @param schema Schéma sur lequel faire la requête.
752+
#'
753+
#' @return Une data.frame avec une colonne campaign_id et une colonne pour
754+
#' les campaign_error.
755+
#'
756+
coleo_inject_vegetation_transect_campaigns <- function(df_id, schema = 'public') {
757+
# La distinction entre les nouvelles campagnes et celles déjà existantes est effectuée en utilisant les colonnes site_id, campaigns_opened_at et sites_site_code. On suppose que les techniciens sont les mêmes, car cette colonne n'est pas utilisée dans les critères d'unicité. Cela vise à limiter la duplication des campagnes dans coleo en cas de changements dans le formatage des noms.
758+
# L'injection se fait en trois étapes :
759+
# 1. Check if campaigns already exists in coleo
760+
# 2. Inject campaigns that are not in coleo
761+
# 3. Inject other tables
762+
763+
#-------------------------------------------------------------------------
764+
# 1. Check if campaigns already exists in coleo
765+
#-------------------------------------------------------------------------
766+
# Get veg_campaigns in coleo
767+
veg_campaigns <- coleo_request_general(endpoint = "campaigns", response = TRUE, schema = schema, "type" = "eq.végétation_transect")
768+
769+
# If no campaigns in coleo, inject all campaigns
770+
if (nrow(veg_campaigns) == 0) return(coleo_inject_table(df_id, "campaigns", schema = schema))
771+
772+
# If campaigns in coleo, check if any in df_id
773+
veg_campaigns <- subset(veg_campaigns, select = c(id, site_id, opened_at))
774+
775+
# Add site_code to veg_campaigns
776+
site_code <- coleo_request_general(endpoint = "sites", response = TRUE, schema = schema, "id" = paste0("in.(",paste(veg_campaigns$site_id, collapse = ","), ")")) |>
777+
dplyr::select(id, site_code)
778+
779+
# Join veg_campaigns and site_code
780+
veg_campaigns <- veg_campaigns |>
781+
dplyr::left_join(site_code, by = c("site_id" = "id")) |>
782+
dplyr::rename(campaign_id = id, campaigns_opened_at = opened_at, sites_site_code = site_code)
783+
784+
# Check if existing veg_campaigns in df
785+
df_c_id <- df_id |>
786+
dplyr::left_join(veg_campaigns, by = c("sites_site_code", "campaigns_opened_at")) |>
787+
as.data.frame()
788+
789+
#-------------------------------------------------------------------------
790+
# 2. Isolate campaigns to be injected
791+
#-------------------------------------------------------------------------
792+
## Isolate campaigns that are already in coleo
793+
df_camp <- df_c_id[!is.na(df_c_id$campaign_id),] |>
794+
dplyr::mutate(campaign_error = NA) |>
795+
dplyr::relocate(campaign_id, site_id, campaign_error)
796+
797+
## Isolate campaigns that are not yet in coleo
798+
df <- df_c_id[is.na(df_c_id$campaign_id),] |> subset(select=-campaign_id) |> subset(select=-site_id)
799+
800+
#-------------------------------------------------------------------------
801+
# 3. Inject campaigns that are not in coleo
802+
#-------------------------------------------------------------------------
803+
if (any(is.na(df$campaign_id))) df_id <- coleo_inject_table(df, "campaigns", schema = schema)
804+
805+
#-------------------------------------------------------------------------
806+
# 4. Bind all campaigns
807+
#-------------------------------------------------------------------------
808+
# Join df_id (injected campaigns) to df_camp (existing campaigns)
809+
if (nrow(df) > 0) {
810+
df_id <- dplyr::bind_rows(df_id, df_camp)
811+
} else {
812+
df_id <- df_camp
813+
}
814+
815+
# Return the results
816+
return(df_id)
732817
}

R/coleo_prep_input_data.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
coleo_prep_input_data <- function(df, db_table) {
1818

1919
# Convert character NAs to actual NAs
20-
df[df == "NA"] <- NA
20+
# df[df == "NA"] <- NA
2121

2222
# Add cell_id to sites table
2323
if (db_table == "sites") {
@@ -41,7 +41,8 @@ coleo_prep_input_data <- function(df, db_table) {
4141
dplyr::select(-sites_site_code, -coleo_id) |>
4242
dplyr::relocate(site_id) |>
4343
tidyr::unnest(cols = c(data)) |>
44-
dplyr::ungroup()
44+
dplyr::ungroup() |>
45+
suppressMessages()
4546
}
4647

4748
# Format extra columns

0 commit comments

Comments
 (0)