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

better errors in readDefaultConfig, make chooseFromList more space-tolerant #54

Merged
merged 6 commits into from
Mar 13, 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: '4700124'
ValidationKey: '4740676'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "gms: 'GAMS' Modularization Support Package",
"version": "0.24.2",
"version": "0.24.4",
"description": "<p>A collection of tools to create, use and maintain modularized model code written in the modeling \n language 'GAMS' (<https://www.gams.com/>). Out-of-the-box 'GAMS' does not come with support for modularized\n model code. This package provides the tools necessary to convert a standard 'GAMS' model to a modularized one\n by introducing a modularized code structure together with a naming convention which emulates local\n environments. In addition, this package provides tools to monitor the compliance of the model code with\n modular coding guidelines.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: gms
Type: Package
Title: 'GAMS' Modularization Support Package
Version: 0.24.2
Date: 2023-03-06
Version: 0.24.4
Date: 2023-03-13
Authors@R: c(person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = c("aut","cre")),
person("David", "Klein", role = "aut"),
person("Anastasis", "Giannousakis", role = "aut"),
Expand Down
16 changes: 11 additions & 5 deletions R/chooseFromList.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#' @param type string in plural shown to user to understand what they have to choose
#' @param userinfo string printed to the user before choosing
#' @param addAllPattern boolean whether 'all' and 'Search by pattern' options are added
#' @param returnBoolean TRUE: returns array with dimension of theList with FALSE and TRUE
#' FALSE: returns selected entries of theList
#' @param returnBoolean TRUE: returns array with dimension of theList with FALSE and TRUE,
#' which erases the order in which entries were selected
#' FALSE: returns selected entries of theList, conserving the order
#' in which entries were selected
#' @param multiple TRUE: allows to select multiple entries. FALSE: no
#' @param userinput string provided by the user. If not supplied, user is asked (mainly for testing)
#'
Expand Down Expand Up @@ -62,7 +64,7 @@ chooseFromList <- function(theList, type = "items", userinfo = NULL, addAllPatte
}
# interpret userinput and perform basic checks
identifier <- try(eval(parse(text = paste("c(", userinput, ")"))))
if (! all(grepl("^[0-9,:]*$", userinput)) || inherits(identifier, "try-error")) {
if (! all(grepl("^[0-9,: ]*$", userinput)) || inherits(identifier, "try-error")) {
message("Try again, you have to choose some numbers.")
return(chooseFromList(originalList, type, userinfo, addAllPattern, returnBoolean, multiple))
}
Expand Down Expand Up @@ -105,8 +107,12 @@ choosePatternFromList <- function(theList, type = "items", pattern = FALSE) {
}
id <- grep(pattern = pattern, theList)
# lists all chosen and ask for the confirmation of the made choice
message("\n\nThe search pattern matches the following ", type, ":")
if (length(id) > 0) message(paste(paste(seq_along(id), theList[id], sep = ": "), collapse = "\n"))
if (length(id) > 0) {
message("\n\nThe search pattern matches the following ", type, ":")
message(paste(paste(seq_along(id), theList[id], sep = ": "), collapse = "\n"))
} else {
message("Oops. You didn't select anything.")
}
if (confirmchoice) {
message("\nAre you sure these are the right ", type, "? (y/n): ")
if (! getLine() %in% c("y", "Y")) {
Expand Down
14 changes: 12 additions & 2 deletions R/readDefaultConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@
readDefaultConfig <- function(path) {
# read in settings from main.gms first
cfg <- list()
cfg$gms <- as.list(readSettings(paste0(path, "/", "main.gms")))
fileMain <- file.path(path, "main.gms")
if (file.exists(fileMain)) {
cfg$gms <- as.list(readSettings(fileMain))
} else {
stop("readDefaultConfig cannot find main.gms in ", normalizePath(path))
}
# overwrite with settings from default.cfg
env <- new.env()
source(paste0(path, "/", "config/default.cfg"), local = env) # nolint: undesirable_function_linter
fileDefault <- file.path(path, "config", "default.cfg")
if (file.exists(fileDefault)) {
source(fileDefault, local = env) # nolint: undesirable_function_linter
} else {
stop("readDefaultConfig cannot find config/default.cfg in ", normalizePath(path))
}
return(modifyList(cfg, env$cfg, keep.null = TRUE))
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 'GAMS' Modularization Support Package

R package **gms**, version **0.24.2**
R package **gms**, version **0.24.4**

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

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

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

Dietrich J, Klein D, Giannousakis A, Beier F, Koch J, Baumstark L, Pflüger M, Richters O (2023). _gms: 'GAMS' Modularization Support Package_. doi: 10.5281/zenodo.4390032 (URL: https://doi.org/10.5281/zenodo.4390032), R package version 0.24.2, <URL: https://github.com/pik-piam/gms>.
Dietrich J, Klein D, Giannousakis A, Beier F, Koch J, Baumstark L, Pflüger M, Richters O (2023). _gms: 'GAMS' Modularization Support Package_. doi: 10.5281/zenodo.4390032 (URL: https://doi.org/10.5281/zenodo.4390032), R package version 0.24.4, <URL: https://github.com/pik-piam/gms>.

A BibTeX entry for LaTeX users is

Expand All @@ -52,7 +52,7 @@ A BibTeX entry for LaTeX users is
title = {gms: 'GAMS' Modularization Support Package},
author = {Jan Philipp Dietrich and David Klein and Anastasis Giannousakis and Felicitas Beier and Johannes Koch and Lavinia Baumstark and Mika Pflüger and Oliver Richters},
year = {2023},
note = {R package version 0.24.2},
note = {R package version 0.24.4},
doi = {10.5281/zenodo.4390032},
url = {https://github.com/pik-piam/gms},
}
Expand Down
6 changes: 4 additions & 2 deletions man/chooseFromList.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/test-chooseFromList.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ test_that("check various chooseFromList settings", {
c("B", "1"))
expect_identical(unname(chooseFromList(theList, returnBoolean = TRUE, userinput = "3,5")),
c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE))
expect_identical(unname(chooseFromList(theList, returnBoolean = TRUE, userinput = " 3, 5 ")),
c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE))
expect_identical(chooseFromList(theList, userinput = "1"),
theList)
expect_identical(chooseFromList(theList, addAllPattern = FALSE, userinput = "1"),
theList[1])
expect_identical(chooseFromList(theList, addAllPattern = FALSE, userinput = " 1 : 1 "),
theList[1])
expect_identical(unname(chooseFromList(theList, userinput = "1", returnBoolean = TRUE)),
rep(TRUE, length(theList)))
expect_identical(chooseFromList(theList, userinput = toString(length(theList) + 2)),
Expand Down