Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AgMIP reporting #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '5824112'
ValidationKey: '5863988'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
92 changes: 21 additions & 71 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Run CI for R using https://eddelbuettel.github.io/r-ci/

name: check

on:
Expand All @@ -8,92 +6,44 @@ on:
pull_request:
branches: [main, master]

env:
USE_BSPM: "true"
_R_CHECK_FORCE_SUGGESTS_: "false"
NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen", "GDPuc", "roxygen2")'

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Bootstrap
run: |
sudo chown runner -R .
sudo locale-gen en_US.UTF-8
sudo add-apt-repository -y ppa:ubuntugis/ppa
curl -OLs https://eddelbuettel.github.io/r-ci/run.sh
chmod 0755 run.sh
./run.sh bootstrap
rm -f bspm_*.tar.gz

- name: Enable r-universe repo, modify bspm integration
run: |
# install packages from https://pik-piam.r-universe.dev and CRAN
echo '
options(repos = c(universe = "https://pik-piam.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
' >> .Rprofile
cat .Rprofile
# modify bspm integration to never install binary builds of PIK CRAN packages
sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site
# need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above)
printf '
local({
expr <- quote({
if (!is.null(repos)) {
noBinaryInstallRPackages <- %s
pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]),
pkgs[pkgs %%in%% noBinaryInstallRPackages])
}
type <- "source"
})
trace(utils::install.packages, expr, print = FALSE)
})
' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null
cat /etc/R/Rprofile.site

- name: Set up Pandoc
uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-pandoc@v2

- name: Set up Python 3.9
uses: actions/setup-python@v4
- uses: r-lib/actions/setup-r@v2
with:
python-version: 3.9
use-public-rspm: true
extra-repositories: "https://rse.pik-potsdam.de/r/packages"

- name: Cache R libraries
if: ${{ !env.ACT }} # skip when running locally via nektos/act
uses: pat-s/always-upload-cache@v3
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: /usr/local/lib/R/
key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }}
restore-keys: |
3-${{ runner.os }}-usr-local-lib-R-

- name: Restore R library permissions
run: |
sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library

- name: Install dependencies
run: |
./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev
./run.sh install_all
./run.sh install_r_binary covr rstudioapi
./run.sh install_r lucode2
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
# piam packages also available on CRAN (madrat, magclass, citation,
# gms, goxygen, GDPuc) will usually have an outdated binary version
# available; by using extra-packages we get the newest version

- uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install python dependencies if applicable
run: |
[ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true
[ -f requirements.txt ] && pip install -r requirements.txt || true

- name: Remove bspm integration # to get rid of error when running install.packages
run: |
sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site
cat /etc/R/Rprofile.site

- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9013
rev: v0.3.2.9019
hooks:
- id: parsable-R
- id: deps-in-desc
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'iamc: IAMC Tools'
version: 0.29.8
date-released: '2023-07-06'
version: 0.29.9
date-released: '2023-09-12'
abstract: A collection of R tools provided by the Integrated Assessment Modeling Consortium
(IAMC) for data analysis and diagnostics.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: iamc
Type: Package
Title: IAMC Tools
Version: 0.29.8
Date: 2023-07-06
Version: 0.29.9
Date: 2023-09-12
Authors@R: c(person("Jan Philipp", "Dietrich", email = "[email protected]", role = c("aut","cre")),
person("Cornelia", "Auer", role = "aut"),
person("Anastasis", "Giannousakis", role = "aut"),
Expand Down
64 changes: 50 additions & 14 deletions R/write.reportProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@

write.reportProject <- function(mif, mapping,
file=NULL, max_file_size=NULL,
format="default", append=FALSE, missing_log=NULL, ...){
if(is.character(mif)){
format="default", append=FALSE, missing_log=NULL, ...) {

if (is.character(mif)) {
data <- read.report(mif,as.list=TRUE)
} else if (is.list(mif)){
data <- mif
Expand All @@ -75,10 +76,11 @@ write.reportProject <- function(mif, mapping,
}

### Here the actual magic takes place
new_data = RenameAndAggregate(data=data,mapping=mapping,missing_log=missing_log)
new_data <- RenameAndAggregate(data = data, mapping = mapping, missing_log = missing_log)

### write the outputs
if(!is.null(file)){

# save project reporting
if(!file.exists(file)) append <- FALSE
if(format == "default") {
Expand All @@ -99,16 +101,33 @@ write.reportProject <- function(mif, mapping,

} else
write.report(new_data,file=file,append=append,...)

} else if (format == "IAMC") {

a <- write.report(new_data,file=NULL,...)
a <- do.call(rbind,do.call(rbind,a))
write.table(a,file,quote=FALSE,sep=",",row.names=FALSE,col.names=!append,append=append,eol="\n")

} else if (format == "AgMIP") {
a <- write.report(new_data,file=NULL,extracols = "Item",...)
a <- do.call(rbind,do.call(rbind,a))
b<-melt(a,id.vars = c("Model","Scenario","Region","Item","Variable","Unit"),variable.name = "Year",value.name="Value")
b<-b[c("Model","Scenario","Region","Item","Variable","Year","Unit","Value")]
write.table(b,file,quote=FALSE,sep=",",row.names=FALSE,col.names=!append,append=append,eol="\n")

if (is.list(new_data)) {
a <- NULL
for (i in seq_along(new_data)) {
model <- names(new_data[[i]])
scenario <- names(new_data)[i]
tmp <- write.report(new_data[[i]][[model]], file = NULL,
model = model, scenario = scenario,
extracols = "Item")
a <- rbind(a, tmp)
}
} else {
a <- write.report(new_data, file = NULL, extracols = "Item",...)
}
b <- reshape2::melt(a, id.vars = c("Model", "Scenario", "Region", "Item", "Variable", "Unit"),
variable.name = "Year", value.name = "Value")
b <- b[c("Model", "Scenario", "Region", "Item", "Variable", "Year", "Unit", "Value")]
write.table(b, file, quote = FALSE, sep = ",",
row.names = FALSE, col.names = !append, append = append, eol = "\n")
}

if (!is.null(max_file_size)) {
Expand Down Expand Up @@ -143,14 +162,31 @@ write.reportProject <- function(mif, mapping,
a <- write.report(tmp,file=NULL,...)
a <- do.call(rbind,do.call(rbind,a))
write.csv(a,file=file,row.names = FALSE,quote = FALSE)

} else if (format == "AgMIP") {
a <- write.report(tmp,file=NULL,extracols = "Item",...)
a <- do.call(rbind,do.call(rbind,a))
b<-melt(a,id.vars = c("Model","Scenario","Region","Item","Variable","Unit"),variable.name = "Year",value.name="Value")
b<-b[c("Model","Scenario","Region","Item","Variable","Year","Unit","Value")]
write.csv(b,file=paste0(file_name[1:last-1],"_part",f,".",file_name[last]),row.names = FALSE,quote = FALSE)

if (is.list(tmp)) {
a <- NULL
for (i in seq_along(tmp)) {
model <- names(tmp[[i]])
scenario <- names(tmp)[i]
b <- write.report(tmp[[i]][[model]], file = NULL,
model = model, scenario = scenario,
extracols = "Item")
a <- rbind(a, b)
}
} else {
a <- write.report(tmp, file = NULL, extracols = "Item",...)
}

a <- do.call(rbind, do.call(rbind, a))
b <- reshape2::melt(a, id.vars = c("Model", "Scenario", "Region", "Item", "Variable", "Unit"),
variable.name = "Year", value.name = "Value")
b <- b[c("Model", "Scenario", "Region", "Item", "Variable", "Year", "Unit", "Value")]
write.csv(b, file = paste0(file_name[1:last-1], "_part", f, ".", file_name[last]),
row.names = FALSE, quote = FALSE)
}
#set counter for next loop
# set counter for next loop
first_scen <- first_scen+scen_per_file
}
}
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# IAMC Tools

R package **iamc**, version **0.29.8**
R package **iamc**, version **0.29.9**

[![CRAN status](https://www.r-pkg.org/badges/version/iamc)](https://cran.r-project.org/package=iamc) [![R build status](https://github.com/giannou/iamc/workflows/check/badge.svg)](https://github.com/giannou/iamc/actions) [![codecov](https://codecov.io/gh/giannou/iamc/branch/master/graph/badge.svg)](https://app.codecov.io/gh/giannou/iamc) [![r-universe](https://pik-piam.r-universe.dev/badges/iamc)](https://pik-piam.r-universe.dev/builds)
[![CRAN status](https://www.r-pkg.org/badges/version/iamc)](https://cran.r-project.org/package=iamc) [![R build status](https://github.com/FelicitasBeier/iamc/workflows/check/badge.svg)](https://github.com/FelicitasBeier/iamc/actions) [![codecov](https://codecov.io/gh/FelicitasBeier/iamc/branch/master/graph/badge.svg)](https://app.codecov.io/gh/FelicitasBeier/iamc) [![r-universe](https://pik-piam.r-universe.dev/badges/iamc)](https://pik-piam.r-universe.dev/builds)

## Purpose and Functionality

Expand Down Expand Up @@ -46,7 +46,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **iamc** in publications use:

Dietrich J, Auer C, Giannousakis A, Bertram C, Benke F, Humpenoeder F, Baumstark L (2023). _iamc: IAMC Tools_. R package version 0.29.8.
Dietrich J, Auer C, Giannousakis A, Bertram C, Benke F, Humpenoeder F, Baumstark L (2023). _iamc: IAMC Tools_. R package version 0.29.9.

A BibTeX entry for LaTeX users is

Expand All @@ -55,6 +55,6 @@ A BibTeX entry for LaTeX users is
title = {iamc: IAMC Tools},
author = {Jan Philipp Dietrich and Cornelia Auer and Anastasis Giannousakis and Christoph Bertram and Falk Benke and Florian Humpenoeder and Lavinia Baumstark},
year = {2023},
note = {R package version 0.29.8},
note = {R package version 0.29.9},
}
```