Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port write_dwc() to use the API #302

Draft
wants to merge 23 commits into
base: vcr
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
eff907b
split into sql helper and api forward
PietrH Sep 12, 2024
ee49b2d
devtools::document()
PietrH Sep 12, 2024
a096a2c
directory is no longer deprecated, but is not passed to API
PietrH Sep 13, 2024
626c51b
allow not passing partent function arguments to sql/api helper
PietrH Sep 13, 2024
7467dee
devtools::document()
PietrH Sep 13, 2024
d8950fa
Style
PietrH Sep 13, 2024
a55e0ec
Setup for writing out
PietrH Sep 13, 2024
7152d3d
allow non existing paths
PietrH Sep 13, 2024
06e2bfc
also allow no ignored arguments
PietrH Sep 13, 2024
3761337
implement NULL fix from etn, write out csv:not list, return list over…
PietrH Sep 13, 2024
0109932
don't use con in tests: deprecated
PietrH Sep 13, 2024
ab36d0b
devtools::document()
PietrH Sep 13, 2024
91ec15b
split test into API and SQL file
PietrH Sep 13, 2024
66251d6
set usage of api explicitly: retain test behaviour even if default be…
PietrH Sep 13, 2024
e4e71c4
use cached api response for demer
PietrH Sep 13, 2024
4e05e01
Add cached response for `write_dwc()`
PietrH Sep 13, 2024
f088ad8
filter opencpu temp key in attempt to get vcr to work
PietrH Sep 13, 2024
d2b9cf9
Filtering opencpu temp key doesn't stop mismatching on uri
PietrH Sep 13, 2024
7d6c319
encapsulate all tests in single casette
PietrH Sep 13, 2024
8829b8f
cache request with writing and without writing to disk
PietrH Sep 13, 2024
35d095e
Close connection after query, avoid connection leak, free resources
PietrH Sep 16, 2024
4672ca3
restore default directory so write to wd: e7c090e
PietrH Sep 16, 2024
4c5dd9c
update news on invisible return
PietrH Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ LazyData: true
Encoding: UTF-8
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

* Added a `NEWS.md` file to track changes to the package.
* Deprecated functions `get_deployments()`, `get_detections()`, `get_projects()`, `get_receivers()`, `list_network_project_codes()` and `list_tag_ids()` are now end of life and no longer included in etn 3.0.0
* `write_dwc()` now invisibly returns a list of data.frames even when writing out to a file (with `directory` set to a path)

17 changes: 12 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,26 @@ forward_to_api <- function(

#' Conductor Helper: point the way to API or SQL helper
#'
#' Helper that conducts it's parent function to either use a helper to query the api,
#' or a helper to query a local database connection using SQL.
#' Helper that conducts it's parent function to either use a helper to query the
#' api, or a helper to query a local database connection using SQL.
#'
#' @param api Logical, Should the API be used?
#' @param ... options on how to fetch the response.
#' Forwarded to `forward_to_api()`
#' @param ignored_arguments Character vector of arguments not to pass to the API
#' or SQL helper
#' @param ... options on how to fetch the response. Forwarded to
#' `forward_to_api()`
#'
#' @return parsed R object as resulting from the API
#'
#' @family helper functions
#' @noRd
conduct_parent_to_helpers <- function(api, ...) {
conduct_parent_to_helpers <- function(api,
ignored_arguments = NULL,
...) {
# Check arguments
assertthat::assert_that(assertthat::is.flag(api))
assertthat::assert_that(is.character(ignored_arguments) |
is.null(ignored_arguments))

# Lock in the name of the parent function
function_identity <-
Expand All @@ -357,6 +363,7 @@ conduct_parent_to_helpers <- function(api, ...) {
!names(return_parent_arguments(depth = 2)) %in% c(
"api",
"connection",
ignored_arguments,
"function_identity"
)
]
Expand Down
77 changes: 54 additions & 23 deletions R/write_dwc.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param directory Path to local directory to write file(s) to.
#' If `NULL`, then a list of data frames is returned instead, which can be
#' useful for extending/adapting the Darwin Core mapping before writing with
#' [readr::write_csv()].
#' [readr::write_csv()]. If the directory does not exist, it will be created.
#' @param rights_holder Acronym of the organization owning or managing the
#' rights over the data.
#' @param license Identifier of the license under which the data will be
Expand Down Expand Up @@ -43,11 +43,54 @@
#' Duplicate detections (same animal, tag and timestamp) are excluded.
#' It is possible for a deployment to contain no detections, e.g. if the
#' tag malfunctioned right after deployment.
write_dwc <- function(connection = con,
animal_project_code,
write_dwc <- function(animal_project_code,
directory = ".",
rights_holder = NULL,
license = "CC-BY") {
license = "CC-BY",
api = TRUE,
connection) {
# Check arguments
# The connection argument has been depreciated
if (lifecycle::is_present(connection)) {
deprecate_warn_connection()
}

# Either use the API, or the SQL helper.
out <- conduct_parent_to_helpers(api,
json = FALSE,
ignored_arguments = "directory")

# Return the object or write out to file
if (is.null(directory)) {
## Return a list of dataframes
return(out)
} else {
## Write to file
dwc_occurrence_path <- file.path(directory, "dwc_occurrence.csv")
message(glue::glue(
"Writing data to:",
dwc_occurrence_path,
.sep = "\n"
))
if (!dir.exists(directory)) {
dir.create(directory, recursive = TRUE)
}
readr::write_csv(x = out$dwc_occurrence, file = dwc_occurrence_path, na = "")
}

invisible(out)
}

#' write_dwc() sql helper
#'
#' @inheritParams write_dwc()
#' @noRd
#'
write_dwc_sql <- function(animal_project_code,
rights_holder = NULL,
license = "CC-BY") {
# Create connection
connection <- do.call(connect_to_etn, get_credentials())
# Check connection
check_connection(connection)

Expand Down Expand Up @@ -85,28 +128,16 @@ write_dwc <- function(connection = con,
message("Reading data and transforming to Darwin Core.")
dwc_occurrence_sql <- glue::glue_sql(
readr::read_file(system.file("sql/dwc_occurrence.sql", package = "etn")),
.con = connection
.con = connection,
.null = "NULL"
)
dwc_occurrence <- DBI::dbGetQuery(connection, dwc_occurrence_sql)
# Close connection

## Close the database connection, it's recreated on every function call
DBI::dbDisconnect(connection)

# Return object or write files
if (is.null(directory)) {
list(
dwc_occurrence = dplyr::as_tibble(dwc_occurrence)
# Return list of dataframes
return(
list(dwc_occurrence = dplyr::as_tibble(dwc_occurrence))
)
} else {
dwc_occurrence_path <- file.path(directory, "dwc_occurrence.csv")
message(glue::glue(
"Writing data to:",
dwc_occurrence_path,
.sep = "\n"
))
if (!dir.exists(directory)) {
dir.create(directory, recursive = TRUE)
}
readr::write_csv(dwc_occurrence, dwc_occurrence_path, na = "")
}
}
13 changes: 7 additions & 6 deletions man/write_dwc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading