[CHNOSZ-commits] r520 - in pkg/CHNOSZ: . R tests/testthat

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Dec 10 07:25:41 CET 2019


Author: jedick
Date: 2019-12-10 07:25:40 +0100 (Tue, 10 Dec 2019)
New Revision: 520

Modified:
   pkg/CHNOSZ/DESCRIPTION
   pkg/CHNOSZ/R/add.obigt.R
   pkg/CHNOSZ/R/affinity.R
   pkg/CHNOSZ/R/basis.R
   pkg/CHNOSZ/R/subcrt.R
   pkg/CHNOSZ/R/util.affinity.R
   pkg/CHNOSZ/R/util.args.R
   pkg/CHNOSZ/R/water.R
   pkg/CHNOSZ/tests/testthat/test-util.data.R
Log:
small code cleanups (remove length(which(*)) and some try(*))


Modified: pkg/CHNOSZ/DESCRIPTION
===================================================================
--- pkg/CHNOSZ/DESCRIPTION	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/DESCRIPTION	2019-12-10 06:25:40 UTC (rev 520)
@@ -1,6 +1,6 @@
 Date: 2019-12-10
 Package: CHNOSZ
-Version: 1.3.3-16
+Version: 1.3.3-17
 Title: Thermodynamic Calculations and Diagrams for Geochemistry
 Authors at R: c(
     person("Jeffrey", "Dick", , "j3ffdick at gmail.com", role = c("aut", "cre"),

Modified: pkg/CHNOSZ/R/add.obigt.R
===================================================================
--- pkg/CHNOSZ/R/add.obigt.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/add.obigt.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -144,8 +144,8 @@
   }
   id2 <- paste(to2$name,to2$state)
   # check if the data is compatible with thermo$obigt
-  tr <- try(rbind(to1,to2),silent=TRUE)
-  if(inherits(tr, "try-error")) stop(paste(file, "is not compatible with thermo$obigt data table."))
+  tr <- tryCatch(rbind(to1, to2), error = identity)
+  if(inherits(tr, "error")) stop(paste(file, "is not compatible with thermo$obigt data table."))
   # match the new species to existing ones
   does.exist <- id2 %in% id1
   ispecies.exist <- na.omit(match(id2, id1))

Modified: pkg/CHNOSZ/R/affinity.R
===================================================================
--- pkg/CHNOSZ/R/affinity.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/affinity.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -162,7 +162,7 @@
       # to return the activities of buffered basis species
       tb <- logact.basis.new[unique(ibasis.new)]
       if(!is.null(ncol(tb[[1]]))) {
-        nd <- length(which(dim(tb[[1]]) > 1))
+        nd <- sum(dim(tb[[1]]) > 1)
         # TODO: apply names for more than two dimensions
         if(nd < 3) {
           for(i in 1:length(tb)) {

Modified: pkg/CHNOSZ/R/basis.R
===================================================================
--- pkg/CHNOSZ/R/basis.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/basis.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -118,7 +118,7 @@
     else stop("the number of basis species is less than the number of elements")
   }
   # the second test: matrix is invertible
-  if(inherits(try(solve(comp), silent=TRUE), "try-error")) 
+  if(inherits(tryCatch(solve(comp), error = identity), "error")) 
     stop("singular stoichiometric matrix")
   # store the basis definition in thermo$basis, including
   # both numeric and character data, so we need to use a data frame

Modified: pkg/CHNOSZ/R/subcrt.R
===================================================================
--- pkg/CHNOSZ/R/subcrt.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/subcrt.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -439,7 +439,7 @@
     # deal with repeated species here
     if(TRUE %in% duplicated(iphases[arephases])) {
       # only take the first, not the duplicates
-      ndups <- length(which(ispecies==ispecies[i]))
+      ndups <- sum(ispecies==ispecies[i])
       nphases <- length(arephases) / ndups
       arephases <- arephases[1:nphases]
     }
@@ -448,7 +448,7 @@
       # assemble the Gibbs energies for each species
       for(j in 1:length(arephases)) {
         G.this <- outprops[[arephases[j]]]$G
-        if(length(which(is.na(G.this))) > 0 & exceed.Ttr) warning(paste('subcrt: NAs found for G of ',
+        if(sum(is.na(G.this)) > 0 & exceed.Ttr) warning(paste('subcrt: NAs found for G of ',
           reaction$name[arephases[j]],' ',reaction$state[arephases[j]],' at T-P point(s) ',
           c2s(which(is.na(G.this)),sep=' '),sep=''),call.=FALSE)
         if(j==1) G <- as.data.frame(G.this)

Modified: pkg/CHNOSZ/R/util.affinity.R
===================================================================
--- pkg/CHNOSZ/R/util.affinity.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/util.affinity.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -62,7 +62,7 @@
   ibasisvar <- ibasisvar[!is.na(ibasisvar)]
   ## which vars are in P,T,IS
   varissubcrt <- vars %in% c("P","T","IS")
-  if(length(which(varissubcrt)) > 2) stop("only up to 2 of P,T,IS are supported")
+  if(sum(varissubcrt) > 2) stop("only up to 2 of P,T,IS are supported")
   ## categorize the basis species:
   # 0 - not in the vars; 1 - one of the vars
   ibasis <- 1:nbasis
@@ -71,7 +71,7 @@
   if(identical(what,"logact.basis")) ispecies <- ibasis
   ## what subcrt variable is used to make a 2-D grid?
   workaround.IS <- FALSE
-  if(length(which(varissubcrt)) > 1 & !transect) {
+  if(sum(varissubcrt) > 1 & !transect) {
     grid <- vars[varissubcrt][1]
     if("IS" %in% vars) {
       if(grid != "IS") workaround.IS <- TRUE

Modified: pkg/CHNOSZ/R/util.args.R
===================================================================
--- pkg/CHNOSZ/R/util.args.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/util.args.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -12,7 +12,7 @@
     # water.SUPCRT92 issues its own warnings about 
     # exceeding Psat's temperature limit
     if(get("thermo", CHNOSZ)$opt$water == "IAPWS95")
-      if(length(which(is.na(P)))>0) 
+      if(sum(is.na(P))>0) 
         warning('TP.args: NAs in Psat (likely T > Tc where Tc = 647.096 K)',call.=FALSE)
   }
   if(length(P) < length(T) & !is.null(P)) P <- rep(P, length.out=length(T))

Modified: pkg/CHNOSZ/R/water.R
===================================================================
--- pkg/CHNOSZ/R/water.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/R/water.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -134,7 +134,7 @@
   # tell the user about any problems
   if(any(err.out==1)) {
     if(length(T) > 1) plural <- "s" else plural <- ""
-    nerr <- length(which(err.out==1))
+    nerr <- sum(err.out==1)
     if(nerr > 1) plural2 <- "s" else plural2 <- ""
     if(identical(P, "Psat")) message(paste("water.SUPCRT92: error", plural2, " calculating ",
       nerr, " of ", length(T), " point", plural, "; for Psat we need 273.16 < T < 647.067 K", sep=""))

Modified: pkg/CHNOSZ/tests/testthat/test-util.data.R
===================================================================
--- pkg/CHNOSZ/tests/testthat/test-util.data.R	2019-12-10 02:01:26 UTC (rev 519)
+++ pkg/CHNOSZ/tests/testthat/test-util.data.R	2019-12-10 06:25:40 UTC (rev 520)
@@ -56,7 +56,7 @@
   expect_equal(d1, d2)
 })
 
-test_that("add.obigt() is backwards compatibile for a file that doesn't have an E_units column", {
+test_that("add.obigt() is backwards compatible for a file that doesn't have an E_units column", {
   # test added 20190529
   file <- system.file("extdata/adds/BZA10.csv", package="CHNOSZ")
   rc <- read.csv(file)
@@ -65,6 +65,12 @@
   expect_true(unique(info(inew)$E_units) == "cal")
 })
 
+test_that("add.obigt() gives an error for an incompatible file", {
+  # test added 20191210
+  file <- system.file("extdata/Berman/Ber88_1988.csv", package="CHNOSZ")
+  expect_error(add.obigt(file))
+})
+
 test_that("info() gives consistent messages for cal and J", {
   # test added 20190529
   # add data for dimethylamine and trimethylamine in different units (cal or J)



More information about the CHNOSZ-commits mailing list