diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..bc695e2 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,47 @@ +name: build + +on: + push: + branches: + - main + +jobs: + R-CMD: + runs-on: ${{ matrix.config.os }} + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - { os: ubuntu-latest, r: 'devel', cran: "https://demo.rstudiopm.com/all/__linux__/xenial/latest"} + + env: + R_REMOTES_NO_ERRORS_FROM_WARNINGS: true + GITHUB_PAT: ${{ secrets.GH_RFASSTUI_TOKEN}} + + steps: + - uses: actions/checkout@v1 + - uses: r-lib/actions/setup-r@v2-branch + with: + r-version: '3.6' + - uses: r-lib/actions/setup-pandoc@v2-branch + - uses: r-lib/actions/setup-tinytex@v2-branch + + - name: Install libraries + if: startsWith(matrix.config.os, 'ubuntu') + run: | + sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable + sudo apt-get update + sudo apt-get install -y libcurl4-openssl-dev libssl-dev libxml2-dev libudunits2-dev libgdal-dev libgeos-dev libproj-dev libmagick++-dev libavfilter-dev libharfbuzz-dev libfribidi-dev + + - name: Install dependencies + run: | + install.packages(c("remotes","rcmdcheck"), repos = "https://cloud.r-project.org") + remotes::install_deps(dependencies = TRUE) + remotes::install_github("JGCRI/rpackageutils") + shell: Rscript {0} + + - name: Check + run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--ignore-vignettes", "--no-build-vignettes"), build_args = c("--no-manual", "--ignore-vignettes", "--no-build-vignettes"), error_on = "error") + shell: Rscript {0} diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 0000000..e130965 --- /dev/null +++ b/.github/workflows/codecov.yml @@ -0,0 +1,48 @@ +name: test-coverage + +on: + push: + branches: + - main +jobs: + test: + runs-on: ubuntu-latest + + env: + R_REMOTES_NO_ERRORS_FROM_WARNINGS: true + GITHUB_PAT: ${{secrets.GH_GCAMREPORT_TOKEN}} + + steps: + - uses: actions/checkout@v2 + - uses: r-lib/actions/setup-r@v2-branch + with: + r-version: '4.1.0' + - uses: r-lib/actions/setup-pandoc@v2-branch + - uses: r-lib/actions/setup-tinytex@v2-branch + - uses: actions/download-artifact@v2 + + - name: Cache R packages + uses: actions/cache@v1 + with: + path: ${{ env.R_LIBS_USER }} + key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} + restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- + + - name: Ubuntu config + run: | + sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable + sudo apt-get update + sudo apt-get install -y libcurl4-openssl-dev libharfbuzz-dev libfribidi-dev libssl-dev libxml2-dev libudunits2-dev libgdal-dev libgeos-dev libproj-dev libmagick++-dev libavfilter-dev + + - name: Install dependencies + run: | + install.packages(c("remotes","rcmdcheck","covr"), repos = "https://cloud.r-project.org") + remotes::install_github("JGCRI/rpackageutils") + remotes::install_deps(dependencies = TRUE) + shell: Rscript {0} + + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v4.0.1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + slug: bc3LC/rfasstui diff --git a/NAMESPACE b/NAMESPACE index 2ddf5b9..e2e5bc2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,5 @@ # Generated by roxygen2: do not edit by hand -export(data_query) -export(fill_queries) export(get_example_prj) export(get_globalCapabilities) export(get_globalCustomColorScales) diff --git a/R/set_globals.R b/R/set_globals.R index 6ec893d..1d4157b 100644 --- a/R/set_globals.R +++ b/R/set_globals.R @@ -103,10 +103,6 @@ get_globalCapabilities <- function() { globalCapabilities[['agriculture_rev_loss']] <- 'agriculture_rev_loss' attr(globalCapabilities[['agriculture_rev_loss']], 'longName') <- "Agriculture revenue loss" - # # "Economic impacts" TODO - # globalCapabilities[['economy']] <- c('VSL') - # attr(globalCapabilities[['economy']], 'longName') <- "Economic imipacts" - return(globalCapabilities) } diff --git a/R/user_prj_creation.R b/R/user_prj_creation.R deleted file mode 100644 index 83dc7b6..0000000 --- a/R/user_prj_creation.R +++ /dev/null @@ -1,93 +0,0 @@ -#' data_query -#' -#' Add nonCO2 large queries -#' @param db_path: path of the database -#' @param db_name: name of the database -#' @param prj_name: name of the project -#' @param scenarios: name of the scenarios to be considered -#' @param query_path: path to the queries file -#' @param query_file: queries file name -#' @return dataframe with the data from the query -#' @export -data_query = function(db_path, db_name, prj_name, scenarios, query_path, - query_file = 'queries_rfasst_nonCO2.xml') { - dt <- data.frame() - xml <- xml2::read_xml(file.path(query_path,query_file)) - qq <- xml2::xml_find_first(xml, paste0("//*[@title='", type, "']")) - - type = 'nonCO2 emissions by sector (excluding resource production)' - - # retrive nonCO2 pollutants - emiss_list <- c("BC","BC_AWB","C2F6","CF4","CH4","CH4_AGR","CH4_AWB","CO", - "CO_AWB","HFC125","HFC134a","HFC143a","HFC152a","HFC227ea", - "HFC23","HFC236fa","HFC245fa","HFC32","HFC365mfc","N2O", - "N2O_AGR","N2O_AWB","NH3","NH3_AGR","NH3_AWB","NMVOC", - "NMVOC_AWB","NOx","NOx_AGR","NOx_AWB","OC","OC_AWB","SF6", - "SO2_1","SO2_1_AWB","SO2_2","SO2_2_AWB","SO2_3","SO2_3_AWB", - "SO2_4","SO2_4_AWB") - for (sc in scenarios) { - while (length(emiss_list) > 0) { - current_emis = emiss_list[1:min(21,length(emiss_list))] - qq_sec = gsub("current_emis", paste0("(@name = '", paste(current_emis, collapse = "' or @name = '"), "')"), qq) - - prj_tmp = rgcam::addSingleQuery( - conn = rgcam::localDBConn(db_path, - db_name,migabble = FALSE), - proj = prj_name, - qn = type, - query = qq_sec, - scenario = sc, - regions = NULL, - clobber = TRUE, - transformations = NULL, - saveProj = FALSE, - warn.empty = FALSE - ) - - tmp = data.frame(prj_tmp[[sc]][type]) - if (nrow(tmp) > 0) { - dt = dplyr::bind_rows(dt,tmp) - } - rm(prj_tmp) - - if (length(emiss_list) > 21) { - emiss_list <- emiss_list[(21 + 1):length(emiss_list)] - } else { - emiss_list = c() - } - } - } - # Rename columns - new_colnames <- sub(".*\\.(.*)", "\\1", names(dt)) - names(dt) <- new_colnames - - return(dt) -} - - -#' fill_queries -#' -#' Create a folder to save the datasets and file, in case it does not exist -#' @param prj: current project -#' @param db_path: path of the database -#' @param db_name: name of the database -#' @param prj_name: name of the project -#' @param scenarios: name of the scenarios to be considered -#' @param query_path: path to the queries file -#' @param query_file: queries file name -#' @return prj containing the nonCO2 emissions by sector -#' @export -fill_queries = function(prj, db_path, db_name, prj_name, scenarios, query_path, - query_file = 'queries_rfasst_nonCO2.xml') { - - # add nonCO2 query manually (it is too big to use the usual method) - if (!'nonCO2 emissions by sector (excluding resource production)' %in% rgcam::listQueries(prj)) { - dt_sec <- data_query(db_path, db_name, prj_name, scenarios, query_path, query_file) - prj_tmp <- rgcam::addQueryTable(project = prj_name, qdata = dt_sec, - queryname = 'nonCO2 emissions by sector (excluding resource production)', clobber = FALSE) - prj <- rgcam::mergeProjects(prj_name, list(prj,prj_tmp), clobber = TRUE, saveProj = FALSE) - } - - return(prj) -} - diff --git a/inst/rfasstui/server.R b/inst/rfasstui/server.R index 9d990f5..af73077 100644 --- a/inst/rfasstui/server.R +++ b/inst/rfasstui/server.R @@ -183,11 +183,11 @@ server <- function(input, output, session) # Add nonCO2 query manually (it is too big to use the usual method) - prj <- rfasstui::fill_queries(prj, db_path, db_name, prj_name, scen_name, - query_path, query_file) + prj <- rfasst::fill_queries(prj, db_path, db_name, prj_name, scenarios, + query_path, queries = 'queries_rfasst_nonCO2.xml') # Save the project - rgcam::saveProject(prj, file = file.path('output',prj_name))") + rgcam::saveProject(prj, file = prj_name)") }) diff --git a/man/data_query.Rd b/man/data_query.Rd deleted file mode 100644 index af8441a..0000000 --- a/man/data_query.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/user_prj_creation.R -\name{data_query} -\alias{data_query} -\title{data_query} -\usage{ -data_query( - db_path, - db_name, - prj_name, - scenarios, - query_path, - query_file = "queries_rfasst_nonCO2.xml" -) -} -\arguments{ -\item{db_path:}{path of the database} - -\item{db_name:}{name of the database} - -\item{prj_name:}{name of the project} - -\item{scenarios:}{name of the scenarios to be considered} - -\item{query_path:}{path to the queries file} - -\item{query_file:}{queries file name} -} -\value{ -dataframe with the data from the query -} -\description{ -Add nonCO2 large queries -} diff --git a/man/fill_queries.Rd b/man/fill_queries.Rd deleted file mode 100644 index c89aea2..0000000 --- a/man/fill_queries.Rd +++ /dev/null @@ -1,37 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/user_prj_creation.R -\name{fill_queries} -\alias{fill_queries} -\title{fill_queries} -\usage{ -fill_queries( - prj, - db_path, - db_name, - prj_name, - scenarios, - query_path, - query_file = "queries_rfasst_nonCO2.xml" -) -} -\arguments{ -\item{prj:}{current project} - -\item{db_path:}{path of the database} - -\item{db_name:}{name of the database} - -\item{prj_name:}{name of the project} - -\item{scenarios:}{name of the scenarios to be considered} - -\item{query_path:}{path to the queries file} - -\item{query_file:}{queries file name} -} -\value{ -prj containing the nonCO2 emissions by sector -} -\description{ -Create a folder to save the datasets and file, in case it does not exist -} diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..d30f9cc --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(rfasstui) + +test_check("rfasstui") diff --git a/tests/testthat/test-globals.R b/tests/testthat/test-globals.R new file mode 100644 index 0000000..67415da --- /dev/null +++ b/tests/testthat/test-globals.R @@ -0,0 +1,91 @@ +library(rfasst) + + +context("globals") + + +test_that("globals: color SSP scale", { + + globalSSPColorScales <- get_globalSSPColorScales() + + testthat::expect_equal(globalSSPColorScales, + c("GCAM_SSP1" = "#5DBFDE", "GCAM_SSP2" = "#5CB95C", "GCAM_SSP3" = "#FBAB33", + "GCAM_SSP4" ="#D7534E", "GCAM_SSP5" ="#9E49D1")) + +}) + +test_that("globals: color custom scale", { + + globalCustomColorScales <- get_globalCustomColorScales() + + testthat::expect_equal(globalCustomColorScales, + c("#5DBFDE","#5CB95C","#FBAB33","#D7534E","#9E49D1")) + +}) + + +test_that("globals: get example prj", { + + globalExamplePrj <- get_example_prj() + + testthat::expect_equal(globalExamplePrj, + get(load(file.path(rprojroot::find_root(rprojroot::is_testthat),"test_results","globalExamplePrj.RData")))) + +}) + +test_that("globals: get queries", { + + globalQueries <- get_queries() + + testthat::expect_equal(globalQueries, + get(load(file.path(rprojroot::find_root(rprojroot::is_testthat),"test_results","globalQueries.RData")))) + +}) + + +test_that("globals: global capabilities", { + + globalCapabilities <- get_globalCapabilities() + + expect_equal(globalCapabilities[['emissions_bc']][1], 'emissions_bc') + expect_equal(attributes(globalCapabilities[['emissions_bc']])$longName, 'BC emissions') + + expect_equal(globalCapabilities[['emissions_nh3']][1], 'emissions_nh3') + expect_equal(attributes(globalCapabilities[['emissions_nh3']])$longName, 'NH3 emissions') + + expect_equal(globalCapabilities[['emissions_nmvoc']][1], 'emissions_nmvoc') + expect_equal(attributes(globalCapabilities[['emissions_nmvoc']])$longName, 'NMVOC emissions') + + expect_equal(globalCapabilities[['emissions_nox']][1], 'emissions_nox') + expect_equal(attributes(globalCapabilities[['emissions_nox']])$longName, 'NOx emissions') + + expect_equal(globalCapabilities[['emissions_pom']][1], 'emissions_pom') + expect_equal(attributes(globalCapabilities[['emissions_pom']])$longName, 'POM emissions') + + expect_equal(globalCapabilities[['emissions_so2']][1], 'emissions_so2') + expect_equal(attributes(globalCapabilities[['emissions_so2']])$longName, 'SO2 emissions') + + expect_equal(globalCapabilities[['concentration_pm25']][1], 'concentration_pm25') + expect_equal(attributes(globalCapabilities[['concentration_pm25']])$longName, 'PM25 concentration') + + expect_equal(globalCapabilities[['concentration_o3']][1], 'concentration_o3') + expect_equal(attributes(globalCapabilities[['concentration_o3']])$longName, 'O3 concentration') + + expect_equal(globalCapabilities[['health_deaths_pm25']][1], 'health_deaths_pm25') + expect_equal(attributes(globalCapabilities[['health_deaths_pm25']])$longName, 'Premature deaths due to PM25') + + expect_equal(globalCapabilities[['health_deaths_o3']][1], 'health_deaths_o3') + expect_equal(attributes(globalCapabilities[['health_deaths_o3']])$longName, 'Premature deaths due to O3') + + expect_equal(globalCapabilities[['health_deaths_total']][1], 'health_deaths_total') + expect_equal(attributes(globalCapabilities[['health_deaths_total']])$longName, 'Total premature deaths') + + expect_equal(globalCapabilities[['agriculture_ryl_mi']][1], 'agriculture_ryl_mi') + expect_equal(attributes(globalCapabilities[['agriculture_ryl_mi']])$longName, 'Agriculture relative yield loss') + + expect_equal(globalCapabilities[['agriculture_prod_loss']][1], 'agriculture_prod_loss') + expect_equal(attributes(globalCapabilities[['agriculture_prod_loss']])$longName, 'Agriculture production loss') + + expect_equal(globalCapabilities[['agriculture_rev_loss']][1], 'agriculture_rev_loss') + expect_equal(attributes(globalCapabilities[['agriculture_rev_loss']])$longName, 'Agriculture revenue loss') +}) diff --git a/tests/testthat/test-user-prj-creation.R b/tests/testthat/test-user-prj-creation.R new file mode 100644 index 0000000..8af4805 --- /dev/null +++ b/tests/testthat/test-user-prj-creation.R @@ -0,0 +1,36 @@ +# library(rfasst) +# +# +# context("prj creation") +# +# test_that("prj creation: data query", { +# +# # download prj +# db_path <- file.path(rprojroot::find_root(rprojroot::is_testthat), "www") +# rpackageutils::download_unpack_zip( +# data_directory = db_path, +# url = "https://zenodo.org/record/10258919/files/database_basexdb_ref.zip?download=1" +# ) +# testthat::expect_equal(1, 1) +# +# # create the prj +# db_name <- "database_basexdb_ref" +# prj_name <- "rfasstui_test.dat" +# scenarios <- "Reference" +# # query_path <- file.path(rprojroot::find_root(rprojroot::is_testthat), "www", "input", "queries") +# +# +# print(db_path) +# print(db_name) +# conn <- rgcam::localDBConn(db_path,'database_basexdb_ref') +# conn <- rgcam::localDBConn(file.path(rprojroot::find_root(rprojroot::is_testthat)),'database_basexdb_ref') +# print(conn) +# +# # dt <- data_query(db_path, db_name, prj_name, scenarios, query_path, +# # query_file = 'queries_rfasst_nonCO2.xml') +# +# # testthat::expect_equal(dt, +# # get(load(file.path(rprojroot::find_root(rprojroot::is_testthat),"test_results","dt.RData")))) +# +# }) +# diff --git a/tests/testthat/test_results/dt.RData b/tests/testthat/test_results/dt.RData new file mode 100644 index 0000000..7e6a844 Binary files /dev/null and b/tests/testthat/test_results/dt.RData differ diff --git a/tests/testthat/test_results/globalExamplePrj.RData b/tests/testthat/test_results/globalExamplePrj.RData new file mode 100644 index 0000000..ae647c0 Binary files /dev/null and b/tests/testthat/test_results/globalExamplePrj.RData differ diff --git a/tests/testthat/test_results/globalQueries.RData b/tests/testthat/test_results/globalQueries.RData new file mode 100644 index 0000000..bb06eee Binary files /dev/null and b/tests/testthat/test_results/globalQueries.RData differ diff --git a/tests/testthat/www/input/example_prj/example_ssp.dat b/tests/testthat/www/input/example_prj/example_ssp.dat new file mode 100644 index 0000000..bdf4214 Binary files /dev/null and b/tests/testthat/www/input/example_prj/example_ssp.dat differ diff --git a/tests/testthat/www/input/queries/queries_rfasst.xml b/tests/testthat/www/input/queries/queries_rfasst.xml new file mode 100644 index 0000000..28ef384 --- /dev/null +++ b/tests/testthat/www/input/queries/queries_rfasst.xml @@ -0,0 +1,85 @@ + + + + + + + + sector[@name] + physical-output[@vintage] + *[@type='sector' and (local-name()='AgSupplySector')]// + output-primary/physical-output/node() + primary output only (no residue biomass) + + + + + + + technology[@name] + physical-output[@vintage] + *[@type='sector' and (local-name()='AgSupplySector')]/ + *[@type='subsector']//output-primary/physical-output/node() + primary output only (no residue biomass) + + + + + + + + market + market + Marketplace/market[true()]/price/node() + + + + + + + + sector + cost + *[@type='sector' and + (exists(child::keyword/@supplysector) or local-name() = + 'AgSupplySector')]/cost/text() + + + + + + + + + +subsectoremissions +*[@type = 'sector' ]/*[@type = 'subsector' and (@name='International Aviation')]//*[@type = 'GHG']/emissions/node() +This query will not include resource emissions + + + + + + +subsectoremissions +*[@type = 'sector' ]/*[@type = 'subsector' and (@name='International Ship')]//*[@type = 'GHG']/emissions/node() +This query will not include resource emissions + + + + + + + GHG + emissions + *[@type = 'resource']//*[@type = 'GHG' and @name != 'CO2' and @name != 'CO2_FUG']/emissions/node() + + + + + + + + + + \ No newline at end of file diff --git a/tests/testthat/www/input/queries/queries_rfasst_nonCO2.xml b/tests/testthat/www/input/queries/queries_rfasst_nonCO2.xml new file mode 100644 index 0000000..8c8e874 --- /dev/null +++ b/tests/testthat/www/input/queries/queries_rfasst_nonCO2.xml @@ -0,0 +1,14 @@ + + + + + + + GHG + emissions + *[@type = 'sector']//*[@type = 'GHG' and current_emis]/emissions/node() + + + + + \ No newline at end of file