Skip to content

Commit

Permalink
Merge pull request #95 from orichters/smallfix
Browse files Browse the repository at this point in the history
allow as.quitte + read.quitte + read.snapshot to read rds files
  • Loading branch information
orichters authored Jun 24, 2024
2 parents de66833 + ced9e76 commit 0b66d3f
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '623091040'
ValidationKey: '623603320'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
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: 'quitte: Bits and pieces of code to use with quitte-style data frames'
version: 0.3133.0
date-released: '2024-06-14'
version: 0.3134.0
date-released: '2024-06-24'
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.3133.0
Date: 2024-06-14
Version: 0.3134.0
Date: 2024-06-24
Authors@R: c(
person("Michaja", "Pehl", , "[email protected]", role = c("aut", "cre")),
person("Nico", "Bauer", , "[email protected]", role = "aut"),
Expand Down
2 changes: 1 addition & 1 deletion R/as.quitte.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ as.quitte <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE)

#' @export
as.quitte.character <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
if (all(file.exists(x) & grepl("\\.(mif|csv|xlsx?)$", x)))
if (all(file.exists(x) & grepl("\\.(mif|csv|rds|xlsx?)$", x)))
return(as.quitte(read.quitte(x, sep = NULL),
periodClass = periodClass, addNA = addNA, na.rm = na.rm))
stop(
Expand Down
10 changes: 7 additions & 3 deletions R/read.quitte.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Read IAMC-style .csv or .xlsx files
#' Read IAMC-style .csv or .xlsx files, or object from rds file.
#'
#' Reads IAMC-style .csv or .xlsx files into a quitte data frame.
#' Reads IAMC-style .csv or .xlsx files or object from rds file into a quitte data frame.
#'
#' @md
#' @param file Path of IAMC-style .csv or xlsx. file or vector of paths to read.
#' @param file Path of IAMC-style .csv, .xlsx, or rds file or vector of paths to read.
#' @param sep Column separator, defaults to ";" in read_mif_header().
#' @param quote Quote characters, empty by default.
#' @param na.strings Entries to interpret as NA; defaults to
Expand Down Expand Up @@ -113,6 +113,10 @@ read.quitte <- function(file,
}
return(filter.function(as.quitte(data)))
}
if (grepl("\\.rds$", f)) {
return(filter.function(as.quitte(readRDS(f))))
}

# Check the header for correct names, periods all in one block and no
# additional columns after the periods

Expand Down
10 changes: 5 additions & 5 deletions R/read.snapshot.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Reads IAMC-style .csv or .xlsx files obtained as a IIASA snapshot into a quitte data frame,
#' allowing to filter the loaded data. If head, tail and grep are on your system, a pre-filtering
#' improves performance.
#' or data from rds file, allowing to filter the loaded data.
#' If head, tail and grep are on your system, a pre-filtering improves performance for csv files.
#'
#' @md
#' @param file Path of single IAMC-style .csv/.mif file
#' @param keep list with quitte columns as names and data points that should be kept.
#' If head, tail and grep are available, this list is used to extract the data before reading it
#' into R. The more you restrict the data here, the faster the data is read.
#' If head, tail and grep are available and a csv/mif file is read, this list is used to extract the
#' data before reading it into R. The more you restrict the data here, the faster the data is read.
#' @param filter.function A function used to filter data during read, see read.quitte description.
#' This allows for more complex filtering, but no performance-enhancing pre-filtering using grep is used.
#' The 'keep' list and the 'filter.function' can be combined.
Expand Down Expand Up @@ -38,7 +38,7 @@ read.snapshot <- function(file, keep = list(), filter.function = identity) {

# temporary file
tmpfile <- tempfile(pattern = "data", fileext = gsub("^.*\\.", ".", basename(file)))
if (length(setdiff(names(keep), "period")) > 0 && !grepl("\\.xlsx?$", file)) {
if (length(setdiff(names(keep), "period")) > 0 && !grepl("\\.(rds|xlsx?)$", file)) {
# check whether system commands are supported
testcommand <- c("grep", "head", "tail")
notavailable <- Sys.which(testcommand) == ""
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.3133.0**
R package **quitte**, version **0.3134.0**

[![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.3133.0, <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.3134.0, <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.3133.0},
note = {R package version 0.3134.0},
url = {https://github.com/pik-piam/quitte},
}
```
63 changes: 42 additions & 21 deletions man/calc_addVariable.Rd

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

35 changes: 29 additions & 6 deletions man/cartesian.Rd

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

6 changes: 3 additions & 3 deletions man/read.quitte.Rd

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

12 changes: 6 additions & 6 deletions man/read.snapshot.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-as.quitte.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ test_that(
expect_equal(nrow(as.quitte(NULL)), 0)
}
)

test_that(
'as.quitte works on rds files',
{
rdsfile <- tempfile(pattern = "report", fileext = ".rds")
saveRDS(quitte_example_data, rdsfile)
expect_identical(as.quitte(rdsfile), quitte_example_data)
}
)

0 comments on commit 0b66d3f

Please sign in to comment.