Skip to content

Commit

Permalink
adding messages to functions for backwards compatibility and converti…
Browse files Browse the repository at this point in the history
…ng secchi_estimated column to logical
  • Loading branch information
sbashevkin committed Jul 11, 2024
1 parent b44ec25 commit 8a8e44f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
33 changes: 27 additions & 6 deletions R/open.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ open_database <- function() {
#' @export

open_fish <- function(con, quiet = FALSE) {
if (is.null(con)) {
if (any(lapply(show_cache(), tools::file_ext) == "parquet")) {
stop("Parquet files detected in cache. This version of deltafish uses SQLite as the backend. Please run clear_cache(), then create_fish_db() to update your local copy.")
}

if (missing(con)) {
stop("argument 'con' must be provided. This should be the object created by open_database()")
}

Expand All @@ -67,7 +71,11 @@ open_fish <- function(con, quiet = FALSE) {
#' @export

open_survey <- function(con) {
if (is.null(con)) {
if (any(lapply(show_cache(), tools::file_ext) == "parquet")) {
stop("Parquet files detected in cache. This version of deltafish uses SQLite as the backend. Please run clear_cache(), then create_fish_db() to update your local copy.")
}

if (missing(con)) {
stop("argument 'con' must be provided. This should be the object created by open_database()")
}

Expand All @@ -85,7 +93,11 @@ open_survey <- function(con) {
#' @export

open_length_conv <- function(con) {
if (is.null(con)) {
if (any(lapply(show_cache(), tools::file_ext) == "parquet")) {
stop("Parquet files detected in cache. This version of deltafish uses SQLite as the backend. Please run clear_cache(), then create_fish_db() to update your local copy.")
}

if (missing(con)) {
stop("argument 'con' must be provided. This should be the object created by open_database()")
}

Expand All @@ -98,9 +110,11 @@ open_length_conv <- function(con) {
#' Collect data into R and convert dates/datetimes into the correct data types with the correct time zone.
#' It is recommended to use this function instead of \code{collect} because the database RSQLite does not
#' support date and time data types, so they are stored as character vectors.
#' Although some date and time operations are still posssible, when you \code{collect} the dataset, the
#' Although some date and time operations are still possible, when you \code{collect} the dataset, the
#' Date and Datetime columns will be character vectors. This function will convert those columns
#' (if they exist in your collected dataset) into the correct date and datetime format.
#' RSQLite also does not have a logical data type and logical values are stored as integers.
#' Thus, the Secchi_estimated column is converted to logical by this function as well.
#'
#' @param data A DBI table that can be treated like a data.frame. See `open_fish()` and `open_survey()`
#' @importFrom magrittr %>%
Expand All @@ -122,6 +136,13 @@ collect_data <- function(data) {
} else {
.
}
} %>%
{
if ("Secchi_estimated" %in% names(.)) {
dplyr::mutate(., Secchi_estimated = as.logical(.data$Secchi_estimated))
} else {
.
}
}
}

Expand All @@ -132,8 +153,8 @@ collect_data <- function(data) {
#' @param con A DBI connection object from open_database()
#'
#' @export
close_database <- function(con) {
if (is.null(con)) {
close_database <- function(con = NULL) {
if (missing(con)) {
stop("argument 'con' must be provided. This should be the object created by open_database()")
}
DBI::dbDisconnect(con)
Expand Down
2 changes: 1 addition & 1 deletion man/close_database.Rd

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

4 changes: 3 additions & 1 deletion man/collect_data.Rd

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

20 changes: 13 additions & 7 deletions tests/testthat/test-07_collect_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ raw <- open_survey(con) %>%
summarise(
N = n(),
N_NA_Date = sum(as.integer(is.na(Date)), na.rm = T),
N_NA_Datetime = sum(as.integer(is.na(Datetime)), na.rm = T)
N_NA_Datetime = sum(as.integer(is.na(Datetime)), na.rm = T),
N_SE_1 = sum(Secchi_estimated, na.rm = T)
) %>%
collect()

raw_no_dates <- open_survey(con) %>%
select(-Date, -Datetime) %>%
raw_no_dates_SE <- open_survey(con) %>%
select(-Date, -Datetime, -Secchi_estimated) %>%
collect()

col <- open_survey(con) %>%
collect_data() %>%
summarise(
N = n(),
N_NA_Date = sum(as.integer(is.na(Date))),
N_NA_Datetime = sum(as.integer(is.na(Datetime)))
N_NA_Datetime = sum(as.integer(is.na(Datetime))),
N_SE_T = sum(Secchi_estimated, na.rm = T)
)

col_no_dates <- open_survey(con) %>%
select(-Date, -Datetime) %>%
col_no_dates_SE <- open_survey(con) %>%
select(-Date, -Datetime, -Secchi_estimated) %>%
collect_data()

post_filt_date <- open_survey(con) %>%
Expand Down Expand Up @@ -56,7 +58,7 @@ test_that("collect_data does not change number of rows of survey dataset", {
})

test_that("collect_data does not change the dataset when the date and datetime columns are missing", {
expect_equal(raw_no_dates, col_no_dates)
expect_equal(raw_no_dates_SE, col_no_dates_SE)
})

test_that("collect_data does not change number of rows with NA dates", {
Expand All @@ -67,6 +69,10 @@ test_that("collect_data does not change number of rows with NA datetimes", {
expect_equal(raw$N_NA_Datetime, col$N_NA_Datetime)
})

test_that("collect_data does not change number of TRUE values for Secchi_estimated", {
expect_equal(raw$N_SE_1, col$N_SE_T)
})

test_that("Filtering by date works correctly", {
expect_equal(pre_filt_date, post_filt_date)
expect_equal(pre_filt_date2, post_filt_date)
Expand Down

0 comments on commit 8a8e44f

Please sign in to comment.