diff --git a/.Rbuildignore b/.Rbuildignore index 8155388..051ebab 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,7 +3,6 @@ ^README\.Rmd$ ^\.Rproj\.user$ ^\.github$ -^\.pre-commit-config\.yaml$ ^_pkgdown\.yml$ ^docs$ ^pkgdown$ diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 3b3f86e..b5a2980 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -29,5 +29,3 @@ Fixes # - [ ] *Lorem ipsum dolor sit amet, consectetur adipiscing.* - [ ] Everything looks ok? - -Thank you! diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..e87d167 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,33 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + +name: lint.yaml + +permissions: read-all + +jobs: + lint: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::lintr, local::. + needs: lint + + - name: Lint + run: lintr::lint_package() + shell: Rscript {0} + env: + LINTR_ERROR_ON_LINT: true diff --git a/.github/workflows/pr-commands.yaml b/.github/workflows/pr-commands.yaml new file mode 100644 index 0000000..2edd93f --- /dev/null +++ b/.github/workflows/pr-commands.yaml @@ -0,0 +1,85 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + issue_comment: + types: [created] + +name: pr-commands.yaml + +permissions: read-all + +jobs: + document: + if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }} + name: document + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/pr-fetch@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::roxygen2 + needs: pr-document + + - name: Document + run: roxygen2::roxygenise() + shell: Rscript {0} + + - name: commit + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git add man/\* NAMESPACE + git commit -m 'Document' + + - uses: r-lib/actions/pr-push@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + style: + if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }} + name: style + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/pr-fetch@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: r-lib/actions/setup-r@v2 + + - name: Install dependencies + run: install.packages("styler") + shell: Rscript {0} + + - name: Style + run: styler::style_pkg() + shell: Rscript {0} + + - name: commit + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git add \*.R + git commit -m 'Style' + + - uses: r-lib/actions/pr-push@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 1e1beaf..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,76 +0,0 @@ -# All available hooks: https://pre-commit.com/hooks.html -# R specific hooks: https://github.com/lorenzwalthert/precommit -repos: -- repo: https://github.com/lorenzwalthert/precommit - rev: v0.4.3.9003 - hooks: - - id: style-files - args: [--style_pkg=styler, --style_fun=tidyverse_style] - - id: roxygenize - # codemeta must be above use-tidy-description when both are used - # - id: codemeta-description-updated - - id: use-tidy-description - - id: spell-check - exclude: > - (?x)^( - .*\.[rR]| - .*\.feather| - .*\.jpeg| - .*\.pdf| - .*\.png| - .*\.py| - .*\.RData| - .*\.rds| - .*\.Rds| - .*\.Rproj| - .*\.sh| - (.*/|)\.gitignore| - (.*/|)\.gitlab-ci\.yml| - (.*/|)\.lintr| - (.*/|)\.pre-commit-.*| - (.*/|)\.Rbuildignore| - (.*/|)\.Renviron| - (.*/|)\.Rprofile| - (.*/|)\.travis\.yml| - (.*/|)appveyor\.yml| - (.*/|)NAMESPACE| - (.*/|)renv/settings\.dcf| - (.*/|)renv\.lock| - (.*/|)WORDLIST| - \.github/workflows/.*| - data/.*| - )$ - - id: lintr - - id: readme-rmd-rendered - - id: parsable-R - - id: no-browser-statement - - id: no-print-statement - - id: no-debug-statement - - id: deps-in-desc - - id: pkgdown -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 - hooks: - - id: check-added-large-files - args: ['--maxkb=200'] - - id: file-contents-sorter - files: '^\.Rbuildignore$' - - id: end-of-file-fixer - exclude: '\.Rd' -- repo: https://github.com/pre-commit-ci/pre-commit-ci-config - rev: v1.6.1 - hooks: - # Only required when https://pre-commit.ci is used for config validation - - id: check-pre-commit-ci-config -- repo: local - hooks: - - id: forbid-to-commit - name: Don't commit common R artifacts - entry: Cannot commit .Rhistory, .RData, .Rds or .rds. - language: fail - files: '\.(Rhistory|RData|Rds|rds)$' - # `exclude: ` to allow committing specific files - -ci: - autoupdate_schedule: monthly - skip: [pkgdown] diff --git a/R/data-process.R b/R/data-process.R index 63f9a47..c8c0826 100644 --- a/R/data-process.R +++ b/R/data-process.R @@ -179,9 +179,9 @@ gen_data_weighted_ds_diff <- function(data_weighted) { ) data_weighted_ds_wide <- tidyr::pivot_wider(data_weighted_ds, - names_from = config$names_from, - id_cols = config$id_cols, - values_from = config$weighted_ds + names_from = config$names_from, + id_cols = config$id_cols, + values_from = config$weighted_ds ) data_weighted_ds_wide_diff <- data_weighted_ds_wide |> @@ -359,9 +359,11 @@ gen_data_weighted_burden_spline <- function(data_weighted_burden_wide_collapse) config <- load_config("default") print("Processing the data...") config_file_path <- system.file("config", "config.yml", package = "healthgpsrvis") - burden_spline <- config::get(value = "burden_spline", - file = config_file_path, - use_parent = FALSE) + burden_spline <- config::get( + value = "burden_spline", + file = config_file_path, + use_parent = FALSE + ) ## This function is data smoothing ## It is applied manually now in India project due to abnormal positive values in diff_daly or cumdiff_daly @@ -391,25 +393,31 @@ gen_data_weighted_burden_spline <- function(data_weighted_burden_wide_collapse) ## Fit spline and predict spline_fit_mean <- splines::interpSpline( as.numeric(unlist(data_weighted_burden_mean[config$group])), - as.numeric(unlist(data_weighted_burden_mean[burden_spline[[1]]$burden_mean]))) + as.numeric(unlist(data_weighted_burden_mean[burden_spline[[1]]$burden_mean])) + ) data_weighted_burden_spline[burden_spline[[1]]$burden_mean] <- stats::predict( spline_fit_mean, - as.numeric(unlist(data_weighted_burden_spline[config$group])))$y + as.numeric(unlist(data_weighted_burden_spline[config$group])) + )$y spline_fit_min <- splines::interpSpline( as.numeric(unlist(data_weighted_burden_min[config$group])), - as.numeric(unlist(data_weighted_burden_min[burden_spline[[2]]$burden_min]))) + as.numeric(unlist(data_weighted_burden_min[burden_spline[[2]]$burden_min])) + ) data_weighted_burden_spline[burden_spline[[2]]$burden_min] <- stats::predict( spline_fit_min, - as.numeric(unlist(data_weighted_burden_spline[config$group])))$y + as.numeric(unlist(data_weighted_burden_spline[config$group])) + )$y ## Use smooth.spline for ps4-low spline_fit_max <- splines::interpSpline( as.numeric(unlist(data_weighted_burden_max[config$group])), - as.numeric(unlist(data_weighted_burden_max[burden_spline[[3]]$burden_max]))) + as.numeric(unlist(data_weighted_burden_max[burden_spline[[3]]$burden_max])) + ) data_weighted_burden_spline[burden_spline[[3]]$burden_max] <- stats::predict( spline_fit_max, - as.numeric(unlist(data_weighted_burden_spline[config$group])))$y + as.numeric(unlist(data_weighted_burden_spline[config$group])) + )$y ## Keep 0 values in the first two years, before policy implementation group <- config$group @@ -419,7 +427,8 @@ gen_data_weighted_burden_spline <- function(data_weighted_burden_wide_collapse) data_weighted_burden_spline[[burden_sp]] <- ifelse( data_weighted_burden_spline[[group]] < 2024, 0, - data_weighted_burden_spline[[burden_sp]]) + data_weighted_burden_spline[[burden_sp]] + ) } print("Data processing complete.") return(data_weighted_burden_spline) @@ -433,6 +442,7 @@ gen_data_weighted_burden_spline <- function(data_weighted_burden_wide_collapse) #' @return A data frame with life expectancy. #' @export gen_data_le <- function(data_mean) { + print("Processing the data...") data_le <- data_mean[, c( data_mean$source, data_mean$time, @@ -476,7 +486,7 @@ gen_data_le <- function(data_mean) { values_from = c(data_ple$ple) ) data_ple_wide$diff <- data_ple_wide$intervention - data_ple_wide$baseline - + print("Data processing complete.") return(data_ple_wide) } @@ -485,4 +495,5 @@ utils::globalVariables(c( "incidence_intracerebralhemorrhage", "incidence_ischemicstroke", "incidence_subarachnoidhemorrhage", "prevalence_intracerebralhemorrhage", "prevalence_ischemicstroke", "prevalence_subarachnoidhemorrhage", - "diff_energyintake", "diff_inc_diabetes")) + "diff_energyintake", "diff_inc_diabetes" +)) diff --git a/tests/testthat/test-data-process.R b/tests/testthat/test-data-process.R index f76273c..fab3199 100644 --- a/tests/testthat/test-data-process.R +++ b/tests/testthat/test-data-process.R @@ -193,7 +193,8 @@ test_that("Columns in the generated dataframe", { # Generate a data frame with spline smoothing applied for burden of disease data_weighted_burden_spline <- gen_data_weighted_burden_spline( - data_weighted_bd_wide_collapse) + data_weighted_bd_wide_collapse + ) # Check if the data has the expected number of columns expect_equal(ncol(data_weighted_burden_spline), 4) @@ -204,3 +205,5 @@ test_that("Columns in the generated dataframe", { c("time", "cumdiff_daly_mean", "cumdiff_daly_min", "cumdiff_daly_max") ) }) + +# Testing gen_data_le() function diff --git a/tests/testthat/test-health-plots.R b/tests/testthat/test-health-plots.R index 082e8cd..5a88687 100644 --- a/tests/testthat/test-health-plots.R +++ b/tests/testthat/test-health-plots.R @@ -265,6 +265,20 @@ test_that("life_exp function works correctly", { expect_equal(plot_le$labels$title, "Increase in life expectancy under intervention") }) +# Testing life_exp() function +test_that("life_exp function works correctly", { + # Create sample data + data_ple_wide <- data.frame( + timediff = seq(-9, 21, by = 1), + diff = runif(31, -3, 0) + ) + + # Test for valid input + plot_le <- life_exp(data_ple_wide$diff, data_ple_wide) + expect_s3_class(plot_le, "ggplot") + expect_equal(plot_le$labels$title, "Increase in life expectancy under intervention") +}) + # Testing combine_plots() function test_that("combine_plots function works correctly", { # Create sample metrics