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 read.snapshot documentation, read entire snapshot if grep goes wrong #80

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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: '614443879'
ValidationKey: '614494800'
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.3127.1
date-released: '2023-10-19'
version: 0.3127.2
date-released: '2023-10-20'
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.3127.1
Date: 2023-10-19
Version: 0.3127.2
Date: 2023-10-20
Authors@R: c(
person("Michaja", "Pehl", , "[email protected]", role = c("aut", "cre")),
person("Nico", "Bauer", , "[email protected]", role = "aut"),
Expand Down
52 changes: 26 additions & 26 deletions R/read.snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,33 @@ read.snapshot <- function(file, keep = list(), filter.function = NULL) {
tmpfile <- tempfile(pattern = "data", fileext = ".csv")
if (length(setdiff(names(keep), "period")) > 0) {
# check whether system commands are supported
testcommand <- c("grep", "head", "tail", "sed")
exitcodes <- suppressWarnings(
sapply(paste(testcommand, '--version'), system,
ignore.stdout = TRUE, ignore.stderr = TRUE))
if (any(0 != exitcodes)) {
stop(paste(paste0('`', testcommand[0 != exitcodes], '`', collapse = ', '),
"are not available system commands, please use 'read.quitte'."))
testcommand <- c("grep", "head", "tail")
notavailable <- Sys.which(testcommand) == ""
if (any(notavailable)) {
message(paste(paste0('`', testcommand[notavailable], '`', collapse = ', '),
"are not available system commands, so the entire file is read."))
} else {
# always keep first lines of original file (comments, colnames), grep in the rest
alwayskeep <- 20
system(paste("head -n", alwayskeep, file, ">", tmpfile))
# the goal of the next lines is to grep one after the other through the elements of keep
# keep = list(variable = "GDP|PPP", region = c("World", "FRA")) should get you
# | grep -E '(GDP\|PPP)' | grep -E '(World|FRA)'
# 1. escape | in variable names and do not grep for period
cleanup <- function(x) {
x <- gsub("[^A-Za-z0-9\\| ]", ".", x)
x <- gsub("|", "\\|", x, fixed = TRUE)
}
keepescaped <- lapply(keep[setdiff(names(keep), "period")], cleanup)
# 2. collapse each element with a |
keepcollapsed <- unlist(lapply(keepescaped, paste0, collapse = "|"))
# generate a grep -E statement for each element of keep list
greptext <- paste0(" | grep -E '(", keepcollapsed, ")'", collapse = "")
command <- paste0("tail -n +", (alwayskeep + 1), " ", file, greptext, " >> ", tmpfile)
system(command)
}
# always keep first lines of original file (comments, colnames), grep in the rest
alwayskeep <- 20
system(paste("head -n", alwayskeep, file, ">", tmpfile))
# the goal of the next lines is to grep one after the other through the elements of keep
# keep = list(variable = "GDP|PPP", region = c("World", "FRA")) should get you
# | grep -E '(GDP\|PPP)' | grep -E '(World|FRA)'
# 1. escape | in variable names and do not grep for period
cleanup <- function(x) {
x <- gsub("[^A-Za-z0-9\\| ]", ".", x)
x <- gsub("|", "\\|", x, fixed = TRUE)
}
keepescaped <- lapply(keep[setdiff(names(keep), "period")], cleanup)
# 2. collapse each element with a |
keepcollapsed <- unlist(lapply(keepescaped, paste0, collapse = "|"))
# generate a grep -E statement for each element of keep list
greptext <- paste0(" | grep -E '(", keepcollapsed, ")'", collapse = "")
command <- paste0("tail -n +", (alwayskeep + 1), " ", file, greptext, " >> ", tmpfile)
system(command)
} else {
}
if (! file.exists(tmpfile)) { # if either system commands do not exist or something went wrong
file.copy(file, tmpfile, overwrite = TRUE)
}
joinedfilter <- function(data) {
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.3127.1**
R package **quitte**, version **0.3127.2**

[![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 (2023). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3127.1, <URL: https://github.com/pik-piam/quitte>.
Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2023). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3127.2, <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 = {2023},
note = {R package version 0.3127.1},
note = {R package version 0.3127.2},
url = {https://github.com/pik-piam/quitte},
}
```
8 changes: 6 additions & 2 deletions man/read.snapshot.Rd

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

11 changes: 4 additions & 7 deletions tests/testthat/test-read.snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ test_that("read.snapshot works", {
eol = "\n", na = "", dec = ".", row.names = FALSE,
col.names = TRUE) # mimick IIASA snapshot format
expect_equal(qe, read.snapshot(tmpfile))
fails <- tryCatch(read.snapshot(tmpfile, list(region = head(levels(qe$region), 1))),
error = function(e) { paste(e) })
if (is.character(fails) && length(fails) == 1 && grepl("not available system commands", fails)) {
skip(paste0(gsub("Error in ", "", gsub(", pleas.*", "", fails)), ", skipping tests."))
if (Sys.which("sed") != "") {
system(paste("sed -i 's/GCAM/\"GCAM\"/g;'", tmpfile))
system(paste("sed -i 's/Delayed transition/\"Delayed transition\"/g;'", tmpfile))
}
system(paste("sed -i 's/GCAM/\"GCAM\"/g;'", tmpfile))
system(paste("sed -i 's/Delayed transition/\"Delayed transition\"/g;'", tmpfile))
rtests <- list(head(levels(qe$region), 2))
rtests <- list(head(levels(qe$region), 1), head(levels(qe$region), 2))
for (r in rtests) {
expect_equal(droplevels(dplyr::filter(qe, region %in% r)),
read.snapshot(tmpfile, list(region = r)))
Expand Down