diff --git a/DESCRIPTION b/DESCRIPTION index bd6d17e..a950581 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: common Type: Package Title: chepec common -Version: 0.0.0.9010 +Version: 0.0.0.9011 Description: Commonly used functions and scripts. Authors@R: person("Taha", "Ahmed", email = "taha@chepec.se", role = c("aut", "cre")) License: GPL-3 diff --git a/R/unit-converters-electrochemical.R b/R/unit-converters-electrochemical.R index 4a0a2b6..9405d95 100644 --- a/R/unit-converters-electrochemical.R +++ b/R/unit-converters-electrochemical.R @@ -302,7 +302,14 @@ as.SHE <- function(potential, if (length(potential) == 0 | length(scale) == 0) { stop("Arguments potential or scale cannot be empty!") } else if (length(potential) != length(scale)) { - stop("Arguments potential and scale must have equal number of elements") + # stop, unless length(scale) == 1 where we will assume it should be recycled + if (length(scale) == 1) { + message("Argument has unit length. Recycling it to match length of . ") + scale <- rep(scale, length(potential)) + } else { + stop(paste0("Correspondence between the supplied potentials and scales could not be worked out.\n", + "Please make sure the number of elements in each match, or make unit length.")) + } } arglength <- length(potential) @@ -428,9 +435,11 @@ as.SHE <- function(potential, # just check that it matches whatever the user supplied, if not, # issue a warning (but don't abort, typically the user did not set it # because they don't care and want whatever is in the data) - if (unique(subset.SHE.data$electrolyte) != electrolyte) { + if (any(subset.SHE.data$electrolyte != electrolyte)) { warning(paste0("The requested electrolyte: ", - ifelse(electrolyte == "", "", electrolyte), + ifelse(any(electrolyte == ""), + "", + electrolyte), " was not found for E = ", df$potential[p], " V vs ", df$scale[p], ".\n", "My data only lists one electrolyte for that scale - return value calculated on that basis.")) subset.SHE.data <- @@ -505,7 +514,14 @@ from.SHE <- function(potential, if (length(potential) == 0 | length(scale) == 0) { stop("Arguments potential or scale cannot be empty!") } else if (length(potential) != length(scale)) { - stop("Arguments potential and scale must have the same number of elements") + # stop, unless length(scale) == 1 where we will assume it should be recycled + if (length(scale) == 1) { + message("Argument has unit length. Recycling it to match length of . ") + scale <- rep(scale, length(potential)) + } else { + stop(paste0("Correspondence between the supplied potentials and scales could not be worked out.\n", + "Please make sure the number of elements in each match, or make unit length.")) + } } arglength <- length(potential) @@ -513,7 +529,7 @@ from.SHE <- function(potential, # unless the user supplied them (only necessary for > 1) if (arglength > 1) { # handle two cases: - # 1. user did not touch concentration, temperature and electrolyte args. + # 1. user did not touch concentration, temperature or electrolyte args. # Assume they forgot and reset their length and print a message # 2. user did change concentration or temperature or electrolyte, but still failed to # ensure length equal to arglength. In this case, abort. @@ -618,9 +634,11 @@ from.SHE <- function(potential, # just check that it matches whatever the user supplied, if not, # issue a warning (but don't abort, typically the user did not set it # because they don't care and want whatever is in the data) - if (unique(subset.SHE.data$electrolyte) != electrolyte) { + if (any(subset.SHE.data$electrolyte != electrolyte)) { warning(paste0("The requested electrolyte: ", - ifelse(electrolyte == "", "", electrolyte), + ifelse(any(electrolyte == ""), + "", + electrolyte), " was not found for E = ", df$potential[p], " V vs ", df$scale[p], ".\n", "My data only lists one electrolyte for that scale - return value calculated on that basis.")) subset.SHE.data <- @@ -645,7 +663,7 @@ from.SHE <- function(potential, # (more accurate than simple linear interpolation with approx()) pot.interp <- lm.subset$coefficients[2] * df$temperature[p] + lm.subset$coefficients[1] - message("Calc potential using interp temperature") + # message("Calc potential using interp temperature") ### CALC POTENTIAL vs requested scale if (df$scale[p] == common::RefCanonicalName("AVS")) { # message("Target scale is AVS") @@ -660,7 +678,7 @@ from.SHE <- function(potential, } else { # requested temperature does exist in dataset ### CALC POTENTIAL vs requested scale - message("Calc potential using exact temperature match") + # message("Calc potential using exact temperature match") if (df$scale[p] == common::RefCanonicalName("AVS")) { # message("Target scale is AVS") df$potentialvsscale[p] <- diff --git a/README b/README index 49bad9c..737ebdd 100644 --- a/README +++ b/README @@ -1,4 +1,10 @@ -A collection of general functions and data +## A collection of general functions and data Includes common numerical functions, unit converters, some LaTeX-specific functions, as well as reference data. + + +## Known bugs + +`as.SHE()` and `from.SHE()` will fail to return any results if a vector of potentials is supplied along with a vector containing more than one reference scale (e.g., `as.SHE(potentials = c(0.24, 0.46, -0.15), scale = c("AgCl", "SCE", "AVS"))`). +This bug was first confirmed for vesion `0.0.0.9011`.