[codeface] Re: [PATCH 26/26] Prevent warnings in 'scale.data' if data is empty

  • From: Wolfgang Mauerer <wolfgang.mauerer@xxxxxxxxxxxxxxxxx>
  • To: <codeface@xxxxxxxxxxxxx>
  • Date: Thu, 24 Nov 2016 13:09:45 +0100



Am 13/10/2016 um 17:29 schrieb Claus Hunsen:

If the given data is empty, the function 'max' throws warnings about
that. To prevent this, the function now checks for empty data and
returns early.

I agree that it does not make sense to scale an empty data set.
Do you know where this happens in the actual code? It might be
better to respond with an error in scale.data and fix any callers
instead. An empty data set would likely cause problems later on
of the caller is not aware that this case can happen.

Thanks, Wolfgang

Signed-off-by: Claus Hunsen <hunsen@xxxxxxxxxxxxxxxxx>
---
 codeface/R/utils.r | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/codeface/R/utils.r b/codeface/R/utils.r
index 9911497..28aae41 100644
--- a/codeface/R/utils.r
+++ b/codeface/R/utils.r
@@ -30,16 +30,24 @@ gen.df.from.ts <- function(ts, type) {
 
 ## Scale a given data set to the range [min,max]
 scale.data <- function(dat, .min=0, .max=1) {
+  ## catch empty data
+  if (is.null(dat)) {
+    return(as.numeric(c())) # an empty numeric vector
+  }
+
   ## calculate the scale factor with which to divide the data
   maxDat <- max(dat)
   scale.factor <- (maxDat - min(dat))/(.max - .min)
+
   ## If the data is not all the same, do apply the scale factor
   if (scale.factor > 0) {
     dat <- dat / scale.factor
     maxDat <- maxDat / scale.factor
   }
+
   ## Set the maximum to .max
   dat <- dat + (- maxDat + .max)
+
   return(dat)
 }
 


Other related posts: