From dd7a15c557e207f5f00785d5132da7ac1e1cc4c3 Mon Sep 17 00:00:00 2001 From: iskandari Date: Thu, 19 Oct 2023 14:26:14 -0400 Subject: [PATCH 1/7] drop DBZH column in cases where it is created --- R/utils.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/utils.R b/R/utils.R index 468cfc32..7646f087 100644 --- a/R/utils.R +++ b/R/utils.R @@ -301,6 +301,7 @@ df_to_mat_list <- function(data, maskvars, schema) { dplyr::select(c(setdiff(colnames(data), maskvars), "datetime", "height")) %>% dplyr::mutate(DBZH = ifelse("dbz_all" %in% colnames(data), dbz_all, DBZH), dbz_all = ifelse("DBZH" %in% colnames(data), DBZH, dbz_all)) %>% + dplyr::select(-DBZH) %>% tidyr::pivot_longer(-c(datetime, height), names_to = "variable", values_to = "value") %>% dplyr::group_by(variable) %>% dplyr::group_split() From ed4704473ba8c9f8f80ecbcf5e4ed2b653c4ea2c Mon Sep 17 00:00:00 2001 From: iskandari Date: Thu, 19 Oct 2023 14:41:35 -0400 Subject: [PATCH 2/7] remove addition of DBZH --- R/utils.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 7646f087..e1f236a1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -299,9 +299,6 @@ df_to_mat_list <- function(data, maskvars, schema) { tbls_lst <- data %>% dplyr::select(c(setdiff(colnames(data), maskvars), "datetime", "height")) %>% - dplyr::mutate(DBZH = ifelse("dbz_all" %in% colnames(data), dbz_all, DBZH), - dbz_all = ifelse("DBZH" %in% colnames(data), DBZH, dbz_all)) %>% - dplyr::select(-DBZH) %>% tidyr::pivot_longer(-c(datetime, height), names_to = "variable", values_to = "value") %>% dplyr::group_by(variable) %>% dplyr::group_split() From 8784c6088cece9b78769b174f95df9012b96b641 Mon Sep 17 00:00:00 2001 From: iskandari Date: Thu, 19 Oct 2023 18:38:05 -0400 Subject: [PATCH 3/7] added conditional logic for as.vpts on whether to name reflectivity dbz_all or DBZH --- R/as.vpts.R | 8 ++++++-- R/read_vpts.R | 2 +- R/utils.R | 17 ++++++++++++++++- cran-comments.md | 3 ++- tests/testthat/test-as.vpts.R | 17 +++++++++++++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/R/as.vpts.R b/R/as.vpts.R index 3b40b806..1cd4b9ad 100644 --- a/R/as.vpts.R +++ b/R/as.vpts.R @@ -2,6 +2,9 @@ #' Convert a dataframe into a vpts object #' #' @param data a dataframe created from a VPTS CSV file +#' @param from_csv logical. If TRUE, the function assumes the dataframe is in VPTS format +#' and keeps the reflectivity variable name as `dbz_all`. If FALSE, the function assumes conversion +#' to a vpts object where reflectivity is referred to as `DBZH`. Default is FALSE. #' @returns a bioRad vpts object #' @examples #' # locate example file in VPTS CSV format: @@ -9,7 +12,7 @@ #' # convert the data.frame to a vpts object: #' as.vpts(df) #' @export -as.vpts <- function(data) { +as.vpts <- function(data, from_csv=FALSE) { assertthat::assert_that(inherits(data,"data.frame")) height <- datetime <- source_file <- radar <- NULL @@ -81,7 +84,8 @@ as.vpts <- function(data) { # Convert dataframe maskvars <- c("radar", "rcs", "sd_vvp_threshold", "radar_latitude", "radar_longitude", "radar_height", "radar_wavelength", "source_file", "datetime", "height", "sunrise", "sunset", "day") - data <- df_to_mat_list(data, maskvars, cached_schema) + + data <- df_to_mat_list(data, maskvars, cached_schema, from_csv=from_csv) # Create vpts object output <- list( diff --git a/R/read_vpts.R b/R/read_vpts.R index a96e8383..ce683ddb 100644 --- a/R/read_vpts.R +++ b/R/read_vpts.R @@ -122,7 +122,7 @@ read_vpts_csv <- function(files, data_frame = FALSE) { # Return data as data frame if (!data_frame) { - data <- as.vpts(data) + data <- as.vpts(data, from_csv=TRUE) } return(data) diff --git a/R/utils.R b/R/utils.R index e1f236a1..cb582f9b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -289,7 +289,7 @@ tibble_to_mat <- function(tibble) { #' @returns A named list of matrices ordered according to radvars #' @keywords internal #' @noRd -df_to_mat_list <- function(data, maskvars, schema) { +df_to_mat_list <- function(data, maskvars, schema, from_csv) { datetime <- height <- variable <- fields <- dbz_all <- DBZH <- NULL radvars <- extract_names(schema$fields) #allow DBZH as alternative to dbz_all radvars <- radvars[!radvars %in% maskvars] @@ -297,8 +297,23 @@ df_to_mat_list <- function(data, maskvars, schema) { insert_index <- which(radvars == "dbz_all") + 1 radvars <- append(radvars, alt_radvar, after = insert_index) +rename_reflectivity <- function(data) { + if (!from_csv) { + if ("dbz_all" %in% colnames(data)) { + data <- data %>% + dplyr::mutate(DBZH = dbz_all) %>% + dplyr::select(-dbz_all) + } else { + data <- data %>% + dplyr::rename(dbz_all = DBZH) %>% + dplyr::select(-dbz_all) + } + } + return(data) +} tbls_lst <- data %>% dplyr::select(c(setdiff(colnames(data), maskvars), "datetime", "height")) %>% + rename_reflectivity() %>% tidyr::pivot_longer(-c(datetime, height), names_to = "variable", values_to = "value") %>% dplyr::group_by(variable) %>% dplyr::group_split() diff --git a/cran-comments.md b/cran-comments.md index 4d602020..870b8be1 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,4 +1,4 @@ -## bioRad 0.7.2 +## bioRad 0.7.3 Updates to external links and tests. Errors addressed from CRAN package check. @@ -7,3 +7,4 @@ Updates to external links and tests. Errors addressed from CRAN package check. 2. Fixes an issue where flight altitude quantiles were incorrectly shifted down by up to one height bin in integrate_profile(). 3. Tests brought to date with the latest version of the `testthat` package, which requires handling each indvidual warning and deprecates expect_equivalent() + diff --git a/tests/testthat/test-as.vpts.R b/tests/testthat/test-as.vpts.R index 588377c0..5bfd8ea0 100644 --- a/tests/testthat/test-as.vpts.R +++ b/tests/testthat/test-as.vpts.R @@ -7,3 +7,20 @@ test_that("as.vpts() returns error message for incorrect data", { expect_error(as.vpts(df),"identical") }) + + +test_that("as.vpts() returns 'DBZH' or 'dbz_all' based on from_csv argument", { + + file <- system.file("extdata", "example_vpts.csv", package = "bioRad") + + # When as.vpts() is called via read_vpts(), the reflectivity variable is named dbz_all in the resulting data.frame + vpts_df <- as.data.frame(read_vpts(file)) + expect_true(!"DBZH" %in% colnames(vpts_from_csv)) + expect_true("dbz_all" %in% colnames(vpts_from_csv)) + + # When as.vpts() is called on a dataframe, the reflectivity variable will be renamed DBZH in the resulting vpts object + vpts_obj <- as.vpts(vpts_df, from_csv = FALSE) + expect_true("DBZH" %in% names(vpts_obj$data)) + expect_true(!"dbz_all" %in% names(vpts_obj$data)) + +}) \ No newline at end of file From 8e61b760247b2c9c06776e2917f1466df4b05c03 Mon Sep 17 00:00:00 2001 From: iskandari Date: Thu, 19 Oct 2023 18:40:13 -0400 Subject: [PATCH 4/7] added conditional logic for as.vpts on whether to name reflectivity dbz_all or DBZH --- tests/testthat/test-as.vpts.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-as.vpts.R b/tests/testthat/test-as.vpts.R index 5bfd8ea0..228b61c8 100644 --- a/tests/testthat/test-as.vpts.R +++ b/tests/testthat/test-as.vpts.R @@ -9,18 +9,18 @@ test_that("as.vpts() returns error message for incorrect data", { }) -test_that("as.vpts() returns 'DBZH' or 'dbz_all' based on from_csv argument", { - +test_that("as.vpts() names reflectivity 'DBZH' or 'dbz_all' based on from_csv argument", { + file <- system.file("extdata", "example_vpts.csv", package = "bioRad") - + # When as.vpts() is called via read_vpts(), the reflectivity variable is named dbz_all in the resulting data.frame vpts_df <- as.data.frame(read_vpts(file)) - expect_true(!"DBZH" %in% colnames(vpts_from_csv)) - expect_true("dbz_all" %in% colnames(vpts_from_csv)) - + expect_true(!"DBZH" %in% colnames(vpts_df)) + expect_true("dbz_all" %in% colnames(vpts_df)) + # When as.vpts() is called on a dataframe, the reflectivity variable will be renamed DBZH in the resulting vpts object vpts_obj <- as.vpts(vpts_df, from_csv = FALSE) expect_true("DBZH" %in% names(vpts_obj$data)) expect_true(!"dbz_all" %in% names(vpts_obj$data)) -}) \ No newline at end of file +}) From 987c64d74213277e0647ca035cb461e3935deded Mon Sep 17 00:00:00 2001 From: iskandari Date: Thu, 19 Oct 2023 19:00:40 -0400 Subject: [PATCH 5/7] updated documenation --- man/as.vpts.Rd | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/man/as.vpts.Rd b/man/as.vpts.Rd index 6995a790..e230fb5f 100644 --- a/man/as.vpts.Rd +++ b/man/as.vpts.Rd @@ -4,10 +4,14 @@ \alias{as.vpts} \title{Convert a dataframe into a vpts object} \usage{ -as.vpts(data) +as.vpts(data, from_csv = FALSE) } \arguments{ \item{data}{a dataframe created from a VPTS CSV file} + +\item{from_csv}{logical. If TRUE, the function assumes the dataframe is in VPTS format +and keeps the reflectivity variable name as \code{dbz_all}. If FALSE, the function assumes conversion +to a vpts object where reflectivity is referred to as \code{DBZH}. Default is FALSE.} } \value{ a bioRad vpts object From 50e8a235a55aff88e402562f63a6426cba68ed9e Mon Sep 17 00:00:00 2001 From: Adriaan Dokter Date: Thu, 19 Oct 2023 21:48:56 -0400 Subject: [PATCH 6/7] remove from_csv argument and simplify code --- R/as.vpts.R | 15 +++++++++------ R/read_vpts.R | 2 +- R/utils.R | 20 ++------------------ tests/testthat/test-as.vpts.R | 6 +++--- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/R/as.vpts.R b/R/as.vpts.R index 1cd4b9ad..88481c30 100644 --- a/R/as.vpts.R +++ b/R/as.vpts.R @@ -2,9 +2,6 @@ #' Convert a dataframe into a vpts object #' #' @param data a dataframe created from a VPTS CSV file -#' @param from_csv logical. If TRUE, the function assumes the dataframe is in VPTS format -#' and keeps the reflectivity variable name as `dbz_all`. If FALSE, the function assumes conversion -#' to a vpts object where reflectivity is referred to as `DBZH`. Default is FALSE. #' @returns a bioRad vpts object #' @examples #' # locate example file in VPTS CSV format: @@ -12,9 +9,15 @@ #' # convert the data.frame to a vpts object: #' as.vpts(df) #' @export -as.vpts <- function(data, from_csv=FALSE) { +as.vpts <- function(data) { assertthat::assert_that(inherits(data,"data.frame")) + # if dbz_all is a column name, rename to bioRad naming DBZH + if("dbz_all" %in% names(data)){ + data <- data %>% + dplyr::rename(DBZH = dbz_all) + } + height <- datetime <- source_file <- radar <- NULL # Throw error if nrows per height are not identical @@ -84,8 +87,8 @@ as.vpts <- function(data, from_csv=FALSE) { # Convert dataframe maskvars <- c("radar", "rcs", "sd_vvp_threshold", "radar_latitude", "radar_longitude", "radar_height", "radar_wavelength", "source_file", "datetime", "height", "sunrise", "sunset", "day") - - data <- df_to_mat_list(data, maskvars, cached_schema, from_csv=from_csv) + + data <- df_to_mat_list(data, maskvars, cached_schema) # Create vpts object output <- list( diff --git a/R/read_vpts.R b/R/read_vpts.R index ce683ddb..a96e8383 100644 --- a/R/read_vpts.R +++ b/R/read_vpts.R @@ -122,7 +122,7 @@ read_vpts_csv <- function(files, data_frame = FALSE) { # Return data as data frame if (!data_frame) { - data <- as.vpts(data, from_csv=TRUE) + data <- as.vpts(data) } return(data) diff --git a/R/utils.R b/R/utils.R index cb582f9b..c5b6cd30 100644 --- a/R/utils.R +++ b/R/utils.R @@ -228,7 +228,7 @@ guess_file_type <- function(file_path, n_lines = 5) { } if(tools::file_ext(file_path) == "txt"){ - return("txt") + return("txt") } else { message("No extension detected; assuming file type .txt which maps to stdout format") return("txt") @@ -289,31 +289,15 @@ tibble_to_mat <- function(tibble) { #' @returns A named list of matrices ordered according to radvars #' @keywords internal #' @noRd -df_to_mat_list <- function(data, maskvars, schema, from_csv) { +df_to_mat_list <- function(data, maskvars, schema) { datetime <- height <- variable <- fields <- dbz_all <- DBZH <- NULL radvars <- extract_names(schema$fields) #allow DBZH as alternative to dbz_all radvars <- radvars[!radvars %in% maskvars] alt_radvar <- "DBZH" insert_index <- which(radvars == "dbz_all") + 1 radvars <- append(radvars, alt_radvar, after = insert_index) - -rename_reflectivity <- function(data) { - if (!from_csv) { - if ("dbz_all" %in% colnames(data)) { - data <- data %>% - dplyr::mutate(DBZH = dbz_all) %>% - dplyr::select(-dbz_all) - } else { - data <- data %>% - dplyr::rename(dbz_all = DBZH) %>% - dplyr::select(-dbz_all) - } - } - return(data) -} tbls_lst <- data %>% dplyr::select(c(setdiff(colnames(data), maskvars), "datetime", "height")) %>% - rename_reflectivity() %>% tidyr::pivot_longer(-c(datetime, height), names_to = "variable", values_to = "value") %>% dplyr::group_by(variable) %>% dplyr::group_split() diff --git a/tests/testthat/test-as.vpts.R b/tests/testthat/test-as.vpts.R index 228b61c8..3bca1f1d 100644 --- a/tests/testthat/test-as.vpts.R +++ b/tests/testthat/test-as.vpts.R @@ -9,17 +9,17 @@ test_that("as.vpts() returns error message for incorrect data", { }) -test_that("as.vpts() names reflectivity 'DBZH' or 'dbz_all' based on from_csv argument", { +test_that("as.vpts() converts reflectivity `dbz_all` into 'DBZH'", { file <- system.file("extdata", "example_vpts.csv", package = "bioRad") # When as.vpts() is called via read_vpts(), the reflectivity variable is named dbz_all in the resulting data.frame - vpts_df <- as.data.frame(read_vpts(file)) + vpts_df <- read_vpts(file, data_frame=TRUE) expect_true(!"DBZH" %in% colnames(vpts_df)) expect_true("dbz_all" %in% colnames(vpts_df)) # When as.vpts() is called on a dataframe, the reflectivity variable will be renamed DBZH in the resulting vpts object - vpts_obj <- as.vpts(vpts_df, from_csv = FALSE) + vpts_obj <- as.vpts(vpts_df) expect_true("DBZH" %in% names(vpts_obj$data)) expect_true(!"dbz_all" %in% names(vpts_obj$data)) From dc9f3edaed0909fb65b1f926da48da9beb9cf8f4 Mon Sep 17 00:00:00 2001 From: Adriaan Dokter Date: Thu, 19 Oct 2023 22:04:16 -0400 Subject: [PATCH 7/7] update ignores, rebuild documentation --- .Rbuildignore | 1 + .gitignore | 2 ++ R/as.vpts.R | 2 +- man/as.vpts.Rd | 6 +----- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 8bf3a6ad..b8a7fc7f 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -17,3 +17,4 @@ inst/extdata/vpts.txt.zip ^CRAN-SUBMISSION$ ^doc$ ^Meta$ +rosm.cache diff --git a/.gitignore b/.gitignore index b3e1c080..be3c0817 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,5 @@ pkgdown/* /doc/ /Meta/ +# rosm cache +rosm.cache diff --git a/R/as.vpts.R b/R/as.vpts.R index 88481c30..7af45202 100644 --- a/R/as.vpts.R +++ b/R/as.vpts.R @@ -15,7 +15,7 @@ as.vpts <- function(data) { # if dbz_all is a column name, rename to bioRad naming DBZH if("dbz_all" %in% names(data)){ data <- data %>% - dplyr::rename(DBZH = dbz_all) + dplyr::rename(DBZH = "dbz_all") } height <- datetime <- source_file <- radar <- NULL diff --git a/man/as.vpts.Rd b/man/as.vpts.Rd index e230fb5f..6995a790 100644 --- a/man/as.vpts.Rd +++ b/man/as.vpts.Rd @@ -4,14 +4,10 @@ \alias{as.vpts} \title{Convert a dataframe into a vpts object} \usage{ -as.vpts(data, from_csv = FALSE) +as.vpts(data) } \arguments{ \item{data}{a dataframe created from a VPTS CSV file} - -\item{from_csv}{logical. If TRUE, the function assumes the dataframe is in VPTS format -and keeps the reflectivity variable name as \code{dbz_all}. If FALSE, the function assumes conversion -to a vpts object where reflectivity is referred to as \code{DBZH}. Default is FALSE.} } \value{ a bioRad vpts object