diff --git a/R/total_temperature_summaries.R b/R/total_temperature_summaries.R index 96923ca..377efa5 100644 --- a/R/total_temperature_summaries.R +++ b/R/total_temperature_summaries.R @@ -52,12 +52,15 @@ total_temperature_summaries <- function(country, } } } - if (length(summary_data) == 2) summary_data <- dplyr::full_join(summary_data[[1]], summary_data[[2]]) + if (length(summary_data) == 2){ + summary_data <- dplyr::full_join(summary_data[[1]], summary_data[[2]]) + } else { + summary_data <- summary_data[[1]] + } summary_data$year <- as.integer(summary_data$year) if ("month" %in% names(summary_data)){ summary_data$month <- as.integer(forcats::as_factor(summary_data$month)) } - list_return <- list(definitions, summary_data) return(list_return) # return a list with the metadata and the data itself } diff --git a/README.Rmd b/README.Rmd index 36da6be..8b06b15 100644 --- a/README.Rmd +++ b/README.Rmd @@ -13,19 +13,51 @@ knitr::opts_chunk$set( ) ``` -# epicsawrap +# epicsawrap: R Package for Integrating rpicsa and epicsadata Functions for API Apps +[![R-CMD-check](https://github.com/IDEMSInternational/epicsawrap/workflows/R-CMD-check/badge.svg)](https://github.com/IDEMSInternational/epicsawrap/actions) +[![Codecov test +coverage](https://codecov.io/gh/IDEMSInternational/epicsawrap/branch/main/graph/badge.svg)](https://app.codecov.io/gh/IDEMSInternational/epicsawrap?branch=main) [![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) -The goal of epicsawrap is to ... +## Overview + +**epicsawrap** is an R package that brings together the functionality of **rpicsa** and **epicsadata** packages to be used in creating API applications for crop and climate analysis. It provides a convenient interface for accessing and utilizing the functions from both packages, enabling the development of web-based tools and services for agricultural decision support. + +The package combines the capabilities of **rpicsa** for PICSA and climatic analysis for crops with **epicsadata** for retrieving climate and weather data, creating a powerful tool for addressing the needs of farmers, agronomists, and researchers. ## Installation -You can install the development version of epicsawrap from [GitHub](https://github.com/) with: +You can install **epicsawrap** from CRAN using the following command: ``` r -# install.packages("devtools") +# Install the 'devtools' package if you haven't already +if (!require(devtools)) { + install.packages("devtools") +} + +# Install 'epicsawrap' from GitHub devtools::install_github("IDEMSInternational/epicsawrap") -``` \ No newline at end of file +``` + + +## Documentation +For detailed information on each function, including arguments, usage, and examples, please refer to the package documentation. You can access it using the following command: + +```{r, message = FALSE, warning = FALSE, eval=FALSE, include=TRUE} +?epicsawrap +``` + +The GitHub page for the `rpicsa` package can be found [here](https://github.com/IDEMSInternational/rpicsa). +The GitHub page for the `epicsadata` package can be found [here](https://github.com/IDEMSInternational/epicsadata). + +## Contributing +We welcome contributions from the community. If you have any bug reports, feature requests, or would like to contribute code to rpicsa, please visit our GitHub repository. + +## License +This package is open-source and distributed under the GNU Lesser General Public License v3.0 License. Feel free to use, modify, and distribute it as needed. + +## Contact +If you have any questions, suggestions, or feedback, please feel free to reach out to us. \ No newline at end of file diff --git a/README.md b/README.md index 9dc6f25..38f3d98 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,73 @@ -# epicsawrap +# epicsawrap: R Package for Integrating rpicsa and epicsadata Functions for API Apps +[![R-CMD-check](https://github.com/IDEMSInternational/epicsawrap/workflows/R-CMD-check/badge.svg)](https://github.com/IDEMSInternational/epicsawrap/actions) +[![Codecov test +coverage](https://codecov.io/gh/IDEMSInternational/epicsawrap/branch/main/graph/badge.svg)](https://app.codecov.io/gh/IDEMSInternational/epicsawrap?branch=main) [![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) -The goal of epicsawrap is to … +## Overview + +**epicsawrap** is an R package that brings together the functionality of +**rpicsa** and **epicsadata** packages to be used in creating API +applications for crop and climate analysis. It provides a convenient +interface for accessing and utilizing the functions from both packages, +enabling the development of web-based tools and services for +agricultural decision support. + +The package combines the capabilities of **rpicsa** for PICSA and +climatic analysis for crops with **epicsadata** for retrieving climate +and weather data, creating a powerful tool for addressing the needs of +farmers, agronomists, and researchers. ## Installation -You can install the development version of epicsawrap from -[GitHub](https://github.com/) with: +You can install **epicsawrap** from CRAN using the following command: ``` r -# install.packages("devtools") +# Install the 'devtools' package if you haven't already +if (!require(devtools)) { + install.packages("devtools") +} + +# Install 'epicsawrap' from GitHub devtools::install_github("IDEMSInternational/epicsawrap") ``` + +## Documentation + +For detailed information on each function, including arguments, usage, +and examples, please refer to the package documentation. You can access +it using the following command: + +``` r +?epicsawrap +``` + +The GitHub page for the `rpicsa` package can be found +[here](https://github.com/IDEMSInternational/rpicsa). The GitHub page +for the `epicsadata` package can be found +[here](https://github.com/IDEMSInternational/epicsadata). + +## Contributing + +We welcome contributions from the community. If you have any bug +reports, feature requests, or would like to contribute code to rpicsa, +please visit our GitHub repository. + +## License + +This package is open-source and distributed under the GNU Lesser General +Public License v3.0 License. Feel free to use, modify, and distribute it +as needed. + +## Contact + +If you have any questions, suggestions, or feedback, please feel free to +reach out to us. diff --git a/tests/test.R b/tests/test.R deleted file mode 100644 index 616f5ec..0000000 --- a/tests/test.R +++ /dev/null @@ -1 +0,0 @@ -"hi" diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..638fd86 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(epicsawrap) + +test_check("epicsawrap") diff --git a/tests/testthat/test-annual_rainfall_summaries.R b/tests/testthat/test-annual_rainfall_summaries.R index 3402efb..8340405 100644 --- a/tests/testthat/test-annual_rainfall_summaries.R +++ b/tests/testthat/test-annual_rainfall_summaries.R @@ -1,21 +1,17 @@ -# library(testthat) -# library(rpicsa) -# -# # Test case 1 -# country <- "zm" -# station_id <- "01122" -# summaries <- c("annual_rain") -# result <- epicsawrap::annual_rainfall_summaries(country, station_id, summaries) -# -# definitions <- definitions(country = "zm", station_id = "16", -# summaries = summaries) -# epicsadata::get_daily_data(country = country, station_id = station_id) -# -# country <- "zm" -# station_id_2 <- "16" -# summaries_2 <- c("start_rains", "end_rains", "annual_rain", "seasonal_rain") -# result_2 <- epicsawrap::annual_rainfall_summaries(country, station_id_2, summaries_2) -# +library(testthat) +library(epicsawrap) + +# Test case 1 +epicsadata::gcs_auth_file(file = "testdata/e-picsa-e630400792e7.json") +test_1_results <- readRDS("testdata/test_1_annual_summaries.rds") +country <- "zm" +station_id <- "test_1" + +test_that("Correct summaries are calculated", { + result <- annual_rainfall_summaries(country, station_id) + expect_true(identical(result[[2]], test_1_results)) +}) + # test_that("Correct summaries are calculated", { # # Test case 2 # expect_no_error(result) @@ -28,7 +24,7 @@ # expect_error(epicsawrap::annual_rainfall_summaries(country, station_id, "end_season")) # # # Test case 2: Error is thrown if start_ is not calculated but needed -# expect_error(epicsawrap::annual_rainfall_summaries(country, station_id_2, c("end_rains", "seasonal_rain"))) +# expect_error(epicsawrap::annual_rainfall_summaries(country, station_id, c("seasonal_rain"))) # # # Test case 3: Warning if both end are given and seasonal wanted # expect_warning(epicsawrap::annual_rainfall_summaries(country, station_id_2, c("start_rains", "end_rains", "end_season", "seasonal_rain"))) diff --git a/tests/testthat/test-crop_success_probabilities.R b/tests/testthat/test-crop_success_probabilities.R index d8bfe7d..9cd24a9 100644 --- a/tests/testthat/test-crop_success_probabilities.R +++ b/tests/testthat/test-crop_success_probabilities.R @@ -1,40 +1,18 @@ -# library(testthat) -# -# # External functions call -# # Test case 1: Test with all parameter -# result <- epicsawrap::crop_success_probabilities( -# country = "zm", -# station_id = "16", -# planting_dates = c(92, 122, 153), -# water_requirements = c(300, 500, 700), -# planting_length = c(120, 180), -# start_before_season = TRUE -# ) -# -# # Test case 2: Test with missing planting_length parameter -# result_2 <- epicsawrap::crop_success_probabilities( -# country = "zm", -# station_id = "16", -# planting_dates = c(1, 2, 3), -# water_requirements = c(100, 200, 300), -# start_before_season = TRUE -# ) -# -# # Test case 2: Test with all missing parameters -# result_3 <- epicsawrap::crop_success_probabilities( -# country = "zm", -# station_id = "16" -# ) -# -# # Define test cases -# test_that("crop_success_probabilities returns correct results", { -# expect_no_error(result) -# expect_no_error(result_2) -# expect_no_error(result_3) -# }) -# -# # Define test cases -# test_that("crop_success_probabilities returns same results whether parameters are defined or not", { -# expect_identical(result[[2]], result_3[[2]]) -# }) -# +library(testthat) +library(epicsawrap) + +# Test case 1 +epicsadata::gcs_auth_file(file = "testdata/e-picsa-e630400792e7.json") +test_1_results <- readRDS("testdata/test_1_crop_success_probabilities.rds") +country <- "zm" +station_id <- "test_1" +test_that("Correct summaries are calculated", { + result <- crop_success_probabilities(country, station_id, water_requirements = c(0, 100, 300)) + expect_true(identical(result[[2]], test_1_results)) +}) + + +crop_success_probabilities(country, "01122", water_requirements = c(0, 100, 300)) + + +daily <- epicsadata::get_daily_data(country = country, station_id = station_id) diff --git a/tests/testthat/test-season_start_probabilities.R b/tests/testthat/test-season_start_probabilities.R index 5f22718..68a2a58 100644 --- a/tests/testthat/test-season_start_probabilities.R +++ b/tests/testthat/test-season_start_probabilities.R @@ -1,20 +1,15 @@ -# library(testthat) -# -# # External functions call -# # Test case 1: Test with all parameters -# result <- -# season_start_probabilities( -# country = "zm", -# station_id = "16", -# start_dates = c(10, 20, 100) -# ) -# -# # Test case 2: Test with missing parameters -# result_2 <- -# season_start_probabilities(country = "zm", station_id = "16") -# -# # Define test cases -# test_that("season_start_probabilities returns correct results", { -# expect_no_error(result) -# expect_no_error(result_2) -# }) \ No newline at end of file +library(testthat) +library(epicsawrap) + +# Test case 1 +epicsadata::gcs_auth_file(file = "testdata/e-picsa-e630400792e7.json") +test_1_results <- readRDS("testdata/test_1_season_start_probabilities.rds") +country <- "zm" +station_id <- "test_1" +test_that("Correct summaries are calculated", { + result <- season_start_probabilities(country, station_id, c(0, 150, 300)) + expect_true(identical(result[[2]], test_1_results)) +}) + + + diff --git a/tests/testthat/test-total_temperature_summaries.R b/tests/testthat/test-total_temperature_summaries.R index 7a8fcc1..7aa537b 100644 --- a/tests/testthat/test-total_temperature_summaries.R +++ b/tests/testthat/test-total_temperature_summaries.R @@ -1,27 +1,17 @@ -# library(testthat) -# -# # Test case 1: Test with to = "annual" -# result <- epicsawrap:::total_temperature_summaries( -# country = "zm", -# station_id = "23", -# summaries = c("mean_tmin", "mean_tmax"), -# to = "annual" -# ) -# -# # Test case 2: Test with to = "monthly" -# result_2 <- epicsawrap:::total_temperature_summaries( -# country = "zm", -# station_id = "1", -# summaries = c("mean_tmin", "mean_tmax"), -# to = "monthly" -# ) -# -# # Define test cases -# test_that("total_temperature_summaries returns correct results", { -# # Mock the necessary external functions or datasets if needed -# expect_length(result, 2) -# expect_length(result_2, 2) -# -# expect_no_error(result) -# expect_no_error(result_2) -# }) \ No newline at end of file +library(testthat) +library(epicsawrap) + +# Test case 1 +epicsadata::gcs_auth_file(file = "testdata/e-picsa-e630400792e7.json") +test_1_annual <- readRDS("testdata/test_1_annual.rds") +test_1_monthly <- readRDS("testdata/test_1_monthly.rds") +country <- "zm" +station_id <- "test_1" + +test_that("Correct summaries are calculated", { + result_annual <- annual_temperature_summaries(country, station_id) + result_monthly <- monthly_temperature_summaries(country, station_id) + + expect_true(identical(result_annual[[2]], test_1_annual)) + expect_true(identical(result_monthly[[2]], test_1_monthly)) +}) \ No newline at end of file diff --git a/tests/.gitignore b/tests/testthat/testdata/.gitignore similarity index 52% rename from tests/.gitignore rename to tests/testthat/testdata/.gitignore index 3300cab..4d63baa 100644 --- a/tests/.gitignore +++ b/tests/testthat/testdata/.gitignore @@ -1,2 +1,2 @@ e-picsa-e630400792e7.json -test.R +epicsae630400792e7.json diff --git a/tests/testthat/testdata/test_1.json b/tests/testthat/testdata/test_1.json new file mode 100644 index 0000000..c510220 --- /dev/null +++ b/tests/testthat/testdata/test_1.json @@ -0,0 +1,81 @@ +{ +"start_rains": { + "threshold": 1, + "start_day": 1, + "end_day": 366, + "total_rainfall": "TRUE", + "amount_rain": 25, + "over_days": 3, + "proportion": "FALSE", + "number_rain_days": "FALSE", + "dry_spell": "TRUE", + "spell_max_dry_days": 10, + "spell_interval": 21, + "dry_period": "FALSE", + "_last_updated": "2022-11-25" + }, +"end_rains": { + "start_day": 121, + "end_day": 366, + "interval_length": 1, + "min_rainfall": 10 + }, +"end_season": { + "start_day": 121, + "end_day": 366, + "capacity": 100, + "water_balance_max": 60, + "evaporation": "value", + "evaporation_value": 5 + }, +"seasonal_length": { + "end_type": "rains" + }, +"seasonal_total_rainfall": { + "na_prop": 0.05 + }, +"mean_tmax": { + "to": "c('annual', 'monthly')", + "na_rm": "TRUE" + }, +"mean_tmin": { + "to": "c('annual', 'monthly')", + "na_rm": "TRUE" + }, +"annual_rain": { + "annual_rain": "TRUE", + "n_rain": "TRUE", + "na_rm": "FALSE" + }, +"season_start_probabilities": { + "specified_day": { + "val1": 50, + "val2": 100, + "val3": 150 + } + }, +"seasonal_rain": { + "seasonal_rain": "TRUE", + "end_type": "rains", + "n_rain": "TRUE", + "na_rm": "FALSE", + "rain_day": 0.85 + }, +"crops_success": { + "water_requirements": { + "val1": 300, + "val2": 500, + "val3": 700 + }, + "planting_dates": { + "val1": 92, + "val2": 122, + "val3": 153 + }, + "planting_length": { + "val1": 120, + "val2": 180 + }, + "start_check": "TRUE" + } +} \ No newline at end of file diff --git a/tests/testthat/testdata/test_1.rds b/tests/testthat/testdata/test_1.rds new file mode 100644 index 0000000..b0fd1ac Binary files /dev/null and b/tests/testthat/testdata/test_1.rds differ diff --git a/tests/testthat/testdata/test_1_annual.rds b/tests/testthat/testdata/test_1_annual.rds new file mode 100644 index 0000000..4254340 Binary files /dev/null and b/tests/testthat/testdata/test_1_annual.rds differ diff --git a/tests/testthat/testdata/test_1_annual_summaries.rds b/tests/testthat/testdata/test_1_annual_summaries.rds new file mode 100644 index 0000000..42c8111 Binary files /dev/null and b/tests/testthat/testdata/test_1_annual_summaries.rds differ diff --git a/tests/testthat/testdata/test_1_crop_success_probabilities.rds b/tests/testthat/testdata/test_1_crop_success_probabilities.rds new file mode 100644 index 0000000..05f01b3 Binary files /dev/null and b/tests/testthat/testdata/test_1_crop_success_probabilities.rds differ diff --git a/tests/testthat/testdata/test_1_monthly.rds b/tests/testthat/testdata/test_1_monthly.rds new file mode 100644 index 0000000..8c7cda9 Binary files /dev/null and b/tests/testthat/testdata/test_1_monthly.rds differ diff --git a/tests/testthat/testdata/test_1_season_start_probabilities.rds b/tests/testthat/testdata/test_1_season_start_probabilities.rds new file mode 100644 index 0000000..5333e0e Binary files /dev/null and b/tests/testthat/testdata/test_1_season_start_probabilities.rds differ