Skip to content

Commit

Permalink
Merge pull request pik-piam#188 from robinhasse/devMappings
Browse files Browse the repository at this point in the history
extend toolGetMapping() to also fetch mappings from packages during development, i.e. loaded via devtools::load_all()
  • Loading branch information
0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q authored Nov 3, 2023
2 parents 4f0688d + 2d7aeee commit b65b40c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '70925670'
ValidationKey: '7275680'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
- name: Test coverage
shell: Rscript {0}
run: covr::codecov(quiet = FALSE)
run: |
nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps"))
if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE)
env:
NOT_CRAN: "true"
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: 'madrat: May All Data be Reproducible and Transparent (MADRaT) *'
version: 3.6.10
date-released: '2023-10-17'
version: 3.7.0
date-released: '2023-11-03'
abstract: Provides a framework which should improve reproducibility and transparency
in data processing. It provides functionality such as automatic meta data creation
and management, rudimentary quality management, data caching, work-flow management
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: madrat
Title: May All Data be Reproducible and Transparent (MADRaT) *
Version: 3.6.10
Date: 2023-10-17
Version: 3.7.0
Date: 2023-11-03
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", , "[email protected]", role = "aut"),
Expand Down
38 changes: 24 additions & 14 deletions R/toolGetMapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,44 @@ toolGetMapping <- function(name, type = NULL, where = NULL,
}

.searchNamePackage <- function(name, type, packageName) {
tmpfname <- .typedName(name, type)
fname <- system.file("extdata", tmpfname, package = packageName)
if (fname == "" && !is_dev_package(packageName)) fname <- system.file("inst/extdata", tmpfname, package = packageName)
if (fname == "") fname <- system.file("extdata", strsplit(tmpfname, split = "/")[[1]][2], package = packageName)
if (fname == "" && !is_dev_package(packageName))
fname <- system.file("inst/extdata", strsplit(tmpfname, split = "/")[[1]][2], package = packageName)
return(fname)
packageLocation <- if (is_dev_package(packageName)) {
# If <packageName> was attached using devtools::load_all(), system.file()
# might or might not be shimmed with pkgload:::shim_system.file()
# (depending on whether madrat was loaded normally or
# through devtools::load_all()), and will yield different results.
# base::system.file() will return the directory from where <packageName> was
# loaded, and we append inst/ manually.
file.path(base::system.file(package = packageName), "inst")
} else {
# If <packageName> was attached normally, system.file() (shimmed or not)
# will return correct results.
system.file(package = packageName)
}

# From packageLocation/extdata, check both the subdirectory indicated by
# <type> and the extdata/ directory, picking the first over the second should
# both exist. If nothing is found, return an empty string.
fname <- file.path(packageLocation, "extdata",
c(.typedName(name, type), name))
return(c(fname[file.exists(fname)], "")[[1]])
}

.typedName <- function(name, type) {
if (is.null(type)) {
return(name)
}
return(paste0(type, "/", name))
return(file.path(type, name))
}

.readMapping <- function(fname) {
filetype <- tolower(file_ext(fname))
if (filetype == "csv") {
if (grepl(pattern = ";", x = readLines(fname, 1))) {
return(read.csv(fname, sep = ";", stringsAsFactors = FALSE, comment.char = "*"))
} else {
return(read.csv(fname, sep = ",", stringsAsFactors = FALSE, comment.char = "*"))
}
sep <- if (grepl(pattern = ";", x = readLines(fname, 1))) ";" else ","
return(read.csv(fname, sep = sep, stringsAsFactors = FALSE, comment.char = "*"))
} else if (filetype == "rda") {
data <- NULL
load(fname)
if (is.null(data)) stop(fname, " did not contain a object named \"data\"!")
if (is.null(data)) stop(fname, " did not contain an object named \"data\"!")
return(data)
} else if (filetype == "rds") {
return(readRDS(fname))
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# May All Data be Reproducible and Transparent (MADRaT) *

R package **madrat**, version **3.6.10**
R package **madrat**, version **3.7.0**

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

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

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

Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Sauer P (2023). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi:10.5281/zenodo.1115490 <https://doi.org/10.5281/zenodo.1115490>, R package version 3.6.10, <https://github.com/pik-piam/madrat>.
Dietrich J, Baumstark L, Wirth S, Giannousakis A, Rodrigues R, Bodirsky B, Leip D, Kreidenweis U, Klein D, Sauer P (2023). _madrat: May All Data be Reproducible and Transparent (MADRaT)_. doi:10.5281/zenodo.1115490 <https://doi.org/10.5281/zenodo.1115490>, R package version 3.7.0, <https://github.com/pik-piam/madrat>.

A BibTeX entry for LaTeX users is

Expand All @@ -64,7 +64,7 @@ A BibTeX entry for LaTeX users is
title = {madrat: May All Data be Reproducible and Transparent (MADRaT)},
author = {Jan Philipp Dietrich and Lavinia Baumstark and Stephen Wirth and Anastasis Giannousakis and Renato Rodrigues and Benjamin Leon Bodirsky and Debbora Leip and Ulrich Kreidenweis and David Klein and Pascal Sauer},
year = {2023},
note = {R package version 3.6.10},
note = {R package version 3.7.0},
doi = {10.5281/zenodo.1115490},
url = {https://github.com/pik-piam/madrat},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-toolGetMapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_that("toolGetMapping works", {
a <- 1
save(a, file = paste0(getConfig("mappingfolder"), "/test.rda"))
writeLines("abc", paste0(getConfig("mappingfolder"), "/test.xyz"))
expect_error(toolGetMapping("test.rda", where = "mappingfolder"), "did not contain a object")
expect_error(toolGetMapping("test.rda", where = "mappingfolder"), "did not contain an object")
expect_error(toolGetMapping("test.xyz"), "Unsupported filetype")

readTest <- function() {
Expand Down

0 comments on commit b65b40c

Please sign in to comment.