Skip to content

Commit

Permalink
Merge pull request #85 from orichters/smallfix
Browse files Browse the repository at this point in the history
allow to read empty mif files, but warn; read IIASA xlsx snapshots
  • Loading branch information
orichters authored Apr 2, 2024
2 parents e8b4d76 + 6e7f30a commit b36443f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
ValidationKey: '618891372'
ValidationKey: '619755850'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: Imports includes .* non-default packages.
AcceptedNotes:
- Imports includes .* non-default packages.
- checking installed package size
AutocreateReadme: yes
allowLinterWarnings: yes
enforceVersionUpdate: no
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: 'quitte: Bits and pieces of code to use with quitte-style data frames'
version: 0.3128.4
date-released: '2024-03-01'
version: 0.3128.5
date-released: '2024-03-28'
abstract: A collection of functions for easily dealing with quitte-style data frames,
doing multi-model comparisons and plots.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: quitte
Title: Bits and pieces of code to use with quitte-style data frames
Version: 0.3128.4
Date: 2024-03-01
Version: 0.3128.5
Date: 2024-03-28
Authors@R: c(
person("Michaja", "Pehl", , "[email protected]", role = c("aut", "cre")),
person("Nico", "Bauer", , "[email protected]", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(as.quitte,"NULL")
S3method(as.quitte,character)
S3method(as.quitte,data.frame)
S3method(as.quitte,list)
Expand Down
9 changes: 8 additions & 1 deletion R/as.quitte.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#' @param na.rm if set to TRUE entries with value NA will be removed
#' @author Jan Philipp Dietrich
#' @keywords classes
#' @importFrom dplyr bind_rows relocate
#' @importFrom dplyr bind_rows filter relocate
#' @importFrom forcats fct_na_value_to_level
#' @importFrom magclass clean_magpie getNames getNames<- getSets getSets<-
#' @importFrom reshape2 melt
#' @importFrom rlang .data
#' @importFrom stats setNames
#' @importFrom tibble as_tibble
#'
Expand Down Expand Up @@ -238,6 +239,12 @@ as.quitte.list <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FA
return(bind_rows(lapply(x, as.quitte, periodClass = periodClass, addNA = addNA, na.rm = na.rm)))
}

#' @method as.quitte NULL
#' @export
as.quitte.NULL <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
return(filter(as_tibble(as.quitte(data.frame(value = 0), periodClass = periodClass)), .data$value > 1))
}

qaddNA <- function(x) {
for (col in colnames(x)) {
if (is.factor(x[[col]]))
Expand Down
8 changes: 7 additions & 1 deletion R/read.quitte.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ read.quitte <- function(file,

if (grepl("\\.xlsx?$", f)) {
data <- read_excel(path = f, sheet = if ('data' %in% excel_sheets(f)) 'data' else 1, guess_max = 21474836)
if (! any(grepl("^[0-9]{4}$", colnames(data)))) {
warning("File ", f, " contains no data, returning empty data.frame.")
return(as.quitte(NULL))
}
data <- pivot_longer(data, matches("^[0-9]{4}$"), names_to = 'period', values_drop_na = drop.na)
missing.default.columns <- default.columns[! default.columns %in% tolower(colnames(data))]
if (length(missing.default.columns) > 0) {
Expand Down Expand Up @@ -130,7 +134,9 @@ read.quitte <- function(file,
paste(setdiff(default.columns, header[1:5]), collapse = ", "))

if (length(period.columns) == 0) {
stop("No column name found that could be understood as a 4-digit year in file ", f, ".")
warning("No column name found that could be understood as a 4-digit year in file ", f,
". Returning empty data.frame.")
return(as.quitte(NULL))
}

if (last(period.columns) != length(header))
Expand Down
6 changes: 4 additions & 2 deletions R/read.snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ read.snapshot <- function(file, keep = list(), filter.function = NULL) {
joinelements <- function(v, list) return(setNames(list(unique(unname(unlist(list[names(list) == v])))), v))
keep <- do.call(c, lapply(unique(names(keep)), joinelements, list = keep))

if (! file.exists(file)) stop("file '", file, "' not found.")

# temporary file
tmpfile <- tempfile(pattern = "data", fileext = ".csv")
if (length(setdiff(names(keep), "period")) > 0) {
tmpfile <- tempfile(pattern = "data", fileext = gsub("^.*\\.", ".", basename(file)))
if (length(setdiff(names(keep), "period")) > 0 && ! grepl("\\.xlsx?$", file)) {
# check whether system commands are supported
testcommand <- c("grep", "head", "tail")
notavailable <- Sys.which(testcommand) == ""
Expand Down
4 changes: 2 additions & 2 deletions R/write.mif.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Write .mif file
#'
#' A wrapper around [`readr::write_lines] for writing files conforming to the
#' [`.mif` standard](https://redmine.pik-potsdam.de/projects/mo/wiki/Model_Intercomparison_File_Format_(mif)).
#' A wrapper around [`readr::write_lines`] for writing files conforming to the
#' [`.mif` standard](https://gitlab.pik-potsdam.de/rse/rsewiki/-/wikis/Model-Intercomparison-File-Format-(mif)).
#'
#' @md
#' @param x A [`quitte`] data frame.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bits and pieces of code to use with quitte-style data frames

R package **quitte**, version **0.3128.4**
R package **quitte**, version **0.3128.5**

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

Expand Down Expand Up @@ -47,7 +47,7 @@ In case of questions / problems please contact Michaja Pehl <michaja.pehl@pik-po

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

Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2024). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3128.4, <URL: https://github.com/pik-piam/quitte>.
Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2024). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3128.5, <URL: https://github.com/pik-piam/quitte>.

A BibTeX entry for LaTeX users is

Expand All @@ -56,7 +56,7 @@ A BibTeX entry for LaTeX users is
title = {quitte: Bits and pieces of code to use with quitte-style data frames},
author = {Michaja Pehl and Nico Bauer and Jérôme Hilaire and Antoine Levesque and Gunnar Luderer and Anselm Schultes and Jan Philipp Dietrich and Oliver Richters},
year = {2024},
note = {R package version 0.3128.4},
note = {R package version 0.3128.5},
url = {https://github.com/pik-piam/quitte},
}
```
3 changes: 2 additions & 1 deletion man/write.mif.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b36443f

Please sign in to comment.