diff --git a/.buildlibrary b/.buildlibrary index 8e3fd40..1547a79 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '514404' +ValidationKey: '533624' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.zenodo.json b/.zenodo.json index c458ad1..2596a89 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -1,6 +1,6 @@ { "title": "mrtutorial: Tutorial Package For Madrat Package Library", - "version": "0.2.7", + "version": "0.2.8", "description": "
Example package of mr- world libraries, in conjunction with MAgPIE MADRat tutorial. For more information please see: https://github.com/magpiemodel/tutorials/tree/master/madrat .<\/p>",
"creators": [
{
diff --git a/DESCRIPTION b/DESCRIPTION
index 7a2d91d..f362c62 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
Package: mrtutorial
Type: Package
Title: Tutorial Package For Madrat Package Library
-Date: 2022-03-01
-Version: 0.2.7
+Date: 2022-03-07
+Version: 0.2.8
Authors@R:
person("David Meng-Chuen", "Chen", email = "david.chen@pik-potsdam.de", role = c("aut","cre"))
Description: Example package of mr- world libraries, in conjunction with MAgPIE MADRat tutorial. For more information please see: https://github.com/magpiemodel/tutorials/tree/master/madrat .
@@ -20,6 +20,9 @@ Imports:
countrycode,
lucode2,
reshape2,
- magpiesets
+ magpiesets,
+ dplyr,
+ tidyr,
+ rlang
Suggests:
covr
diff --git a/NAMESPACE b/NAMESPACE
index 4bd3754..62aaccf 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -6,6 +6,9 @@ importFrom(WDI,WDI)
importFrom(WDI,WDIcache)
importFrom(WDI,WDIsearch)
importFrom(countrycode,countrycode)
+importFrom(dplyr,"%>%")
+importFrom(dplyr,filter)
+importFrom(dplyr,select)
importFrom(lucode2,path)
importFrom(madrat,calcOutput)
importFrom(madrat,getConfig)
@@ -27,5 +30,6 @@ importFrom(magclass,getRegions)
importFrom(magclass,getSets)
importFrom(magclass,getYears)
importFrom(magpiesets,findset)
-importFrom(reshape2,melt)
+importFrom(rlang,.data)
+importFrom(tidyr,pivot_longer)
importFrom(utils,read.csv)
diff --git a/R/calcAgGDP.R b/R/calcAgGDP.R
index 88a58d8..ccd4838 100644
--- a/R/calcAgGDP.R
+++ b/R/calcAgGDP.R
@@ -15,6 +15,7 @@
calcAgGDP <- function(){
gdp <- readSource("TutorialWDI", subtype="NY.GDP.MKTP.CD")
+
ag_gdp <- readSource("TutorialWDI", subtype="NV.AGR.TOTL.CD")
x <- ag_gdp/gdp
@@ -24,9 +25,12 @@ calcAgGDP <- function(){
## some division by 0 happening
x[is.na(x)] <- 0
+ #population weight
+ weight <- readSource("TutorialWDI", subtype = "SP.POP.TOTL")
+
return(list(
x=x,
- weight=NULL,
+ weight=weight,
unit="share",
description="Ag GDP as percentage of total GDP"))
}
diff --git a/R/convertTutorialWDI.R b/R/convertTutorialWDI.R
index 47106f3..e05123d 100644
--- a/R/convertTutorialWDI.R
+++ b/R/convertTutorialWDI.R
@@ -17,46 +17,28 @@
#' @importFrom madrat toolCountryFill
-convertTutorialWDI<-function(x,subtype){
- WDI_data <- WDI::WDI_data
-
- # changing scale of indicators
- if (subtype %in% c("SP.POP.TOTL",
- "NY.GDP.MKTP.PP.KD",
- "NV.AGR.TOTL.CD",
- "NY.GDP.MKTP.PP.CD",
- "NY.GDP.MKTP.CD",
- "NY.GDP.MKTP.KD",
- "NY.GDP.MKTP.KN")) {
- x <- x/1000000
-
-#Kosovo added to Serbia
- x["RS",,] <- dimSums(x[c("RS","XK"),,],dim=1,na.rm=T)
- }else if (subtype %in% WDI_data$series[,"indicator"]){
-
- vcat("Warning: Kosovo left out of conversion and has differing population values from FAO", verbosity=2)
- x <- x
- }else {
- stop("subtype does not exist in the dataset!")
- }
- y <- x
-
-## Channel Islands add to JEY
- JG <- "JEY"
- names(JG) <- "JG"
- getCells(y)<-countrycode::countrycode(getCells(y),"iso2c","iso3c", custom_match = JG)
- y<-y[!is.na(getCells(y)),,]
- y<-clean_magpie(y)
-
- y<-y["ANT",,,invert=TRUE]
-
- y<-toolCountryFill(y,fill = 0)
- y[is.na(y)]<-0
- y <- y[,sort(getYears(y)),]
-
- #remove years which only contain 0s as entries
- y <- y[,!apply(y,2,function(x) return(all(x==0))),]
-
- y<-y[,sort(getYears(y)),]
- return(y)
+convertTutorialWDI<-function(x, subtype){
+
+ # change scale of absolute indicators, MAgPIE runs in Million scale for Pop and GDP drivers
+if (subtype %in% c("NY.GDP.MKTP.CD",
+ "SP.POP.TOTL",
+ "NV.AGR.TOTL.CD")){
+
+ out <- x/1e6 } else
+
+ if (subtype == "SL.AGR.EMPL.ZS"){
+ # don't change for percentage of ag employment
+
+ out <- x } else{
+
+ stop("subtype does not exist in the dataset!")}
+
+ # toolCountryFill fills in missing countries - 249 required for aggregation
+
+ x<-toolCountryFill(out,fill = 0)
+
+ x[is.na(x)] <- 0 #remove NAs
+
+
+ return(x)
}
diff --git a/R/fullTUTORIALVALIDATION.R b/R/fullTUTORIALVALIDATION.R
new file mode 100644
index 0000000..ceee8dc
--- /dev/null
+++ b/R/fullTUTORIALVALIDATION.R
@@ -0,0 +1,33 @@
+#' fullTUTORIALVALIDATION
+#'
+#' Function that produces the complete validation data set used for evaluation of MAgPIE outputs
+#'
+#' @param rev data revision which should be used as input (positive numeric).
+#' @author Jan Philipp Dietrich, Kristine Karstens
+#' @seealso
+#' \code{\link{readSource}}, \code{\link{getCalculations}}, \code{\link{calcOutput}}
+#' @examples
+#' \dontrun{
+#' retrieveData("VALIDATION")
+#' }
+#' @importFrom madrat getConfig calcOutput
+#' @importFrom lucode2 path
+
+fullTUTORIALVALIDATION <- function(rev = 0.1) {
+
+ # all validation data regional aggregations happens here
+
+ # exitsting "validation.mif" file is loaded if all functions are set to append=TRUE
+ # Please put the validation.mif into your output folder.
+ # Use retrieveData("VALIDATION") to create a .tgz
+
+ file.copy(lucode2::path(paste0(getConfig("mainfolder"), "/output/"), "validation.mif"),
+ lucode2::path(getConfig("outputfolder"), "validation.mif"))
+
+ valfile <- "validation.mif"
+
+ # Add your validation scripts here
+ calcOutput(type = "ValidPopulation", datasource = "WDI", aggregate = "REG+GLO",
+ file = valfile, append = TRUE, na_warning = FALSE, try = TRUE)
+
+}
diff --git a/R/readTutorialWDI.R b/R/readTutorialWDI.R
index e0645e3..cb42ff2 100644
--- a/R/readTutorialWDI.R
+++ b/R/readTutorialWDI.R
@@ -5,14 +5,11 @@
#'
#'
#' @param subtype Type of WDI data that should be read. Please use the
-#' worldbank indicator abbreviation. Available types are e.g.: \itemize{ \item
-#' \code{SP.POP.TOTL}: Population, total \item \code{NY.GDP.MKTP.PP.KD}: GDP,
-#' PPP (constant 2011 international Dollar) \item \code{NY.GDP.MKTP.PP.CD}:
-#' GDP, PPP (current international Dollar) \item \code{NY.GDP.MKTP.CD}: GDP MER
-#' (current USDollar) \item \code{NY.GDP.MKTP.KD}: GDP MER (constant 2005
-#' USDollar) \item \code{NY.GDP.MKTP.KN}: GDP LCU (constant LCU)
-#' \item \code{SP.URB.TOTL.IN.ZS}: Urban population (peccentage of total)
-#' \item \code{ NV.AGR.TOTL.CD}: GDP (current USD)}
+#' worldbank indicator abbreviation. Available types are e.g.: \itemize{
+#' \item \code{SP.POP.TOTL}: Population, total
+#' \item \code{NY.GDP.MKTP.PP.CD}: GDP, PPP (current international Dollar)
+#' \item \code{SL.AGR.EMPL.ZS}: Employment in agriculture as % of total employment
+#' \item \code{NV.AGR.TOTL.CD}: Agricultural GDP (current international Dollar) }
#' @return magpie object of the WDI data
#' @author David Chen
#' @seealso \code{\link{readSource}} \code{\link{downloadTutorialWDI}}
@@ -20,17 +17,28 @@
#'
#' \dontrun{ a <- readSource(type="WDI",subtype="SP.POP.TOTL")
#' }
-#' @importFrom reshape2 melt
+#' @importFrom dplyr select filter %>%
+#' @importFrom tidyr pivot_longer
#' @importFrom magclass as.magpie
+#' @importFrom rlang .data
readTutorialWDI<-function(subtype){
+
load("WDI.rda")
- wdi$country <- NULL
- wdi <- as.magpie(melt(wdi,id.vars = c("iso2c","year")),
- spatial=1,temporal=2,tidy=TRUE,replacement =".")
+
+ #convert the iso2c to iso3c using madrat tool
+ wdi$iso2c <- toolCountry2isocode(wdi$iso2c)
+
+ wdi <- wdi %>%
+ dplyr::select(!.data$country) %>%
+ filter(!is.na(.data$iso2c)) %>%
+ pivot_longer(cols=3:6) %>%
+ as.magpie(spatial = 1, temporal = 2, replacement =".")
+
wdi <- wdi[,,subtype]
- getNames(wdi) <- gsub("\\.", "_", getNames(wdi)) ## Replace . with _
+ getNames(wdi) <- gsub("\\.", "_", getNames(wdi))
+ # Replacement of . with _ in dimension is also done automatically in as.magpie()
return(wdi)
}
diff --git a/README.md b/README.md
index ca053c7..3b5dd42 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Tutorial Package For Madrat Package Library
-R package **mrtutorial**, version **0.2.7**
+R package **mrtutorial**, version **0.2.8**
[![CRAN status](https://www.r-pkg.org/badges/version/mrtutorial)](https://cran.r-project.org/package=mrtutorial) [![R build status](https://github.com/pik-piam/mrtutorial/workflows/check/badge.svg)](https://github.com/pik-piam/mrtutorial/actions) [![codecov](https://codecov.io/gh/pik-piam/mrtutorial/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrtutorial) [![r-universe](https://pik-piam.r-universe.dev/badges/mrtutorial)](https://pik-piam.r-universe.dev/ui#builds)
@@ -38,7 +38,7 @@ In case of questions / problems please contact David Meng-Chuen Chen