0

I have seen many questions on SO with this problem , but I can't understand , and relate to those with my case. I need to test for a case and then take appropriate action. Clearly the following code is not what I would eventually use ... but it illustrates the problem.

Code

Init Code

threshold <- 10
slotDuration <- 10 # data arriving in slotDuration sec will get into a single slot
totalSlots <- 200 # number of slots to keep 
prevSlot <- 0

window <- slotDuration * totalSlots # window [time in sec] of data to cover
data <- array (0, dim=c(1,totalSlots))
handler <- function(transactionTime, dataValue){
    # This line perhaps does not matter as the test uses the destSlot variable below.
    dput(transactionTime)
    n <- as.numeric(transactionTime, units = "secs")

    # This is the var that causes problem
   destSlot <- ceiling((n %% window)/slotDuration)

   # this line should ensure we dont proceed on null values .. right ?
   if (is.null(destSlot)) {
      print("This is NULLL")
      return (0)
   }

   if (!is.null(destSlot)) {
       print(destSlot)       ## prints numeric(0)
       print("This is NOT NULLL")
   }

   if (destSlot != 0){ ## This line causes argument length zero error
       prevSlot <<- destSlot
       data[destSlot] <<- 0 
   }
   data[destSlot] <<- data[destSlot] + dataValue

}

Output

structure(1475591155, class = c("POSIXct", "POSIXt"))
[1] 116
[1] "This is NOT NULLL"
Error in eval(substitute(expr), envir, enclos) : 
argument is of length zero
Community
  • 1
  • 1
Scalable
  • 1,550
  • 4
  • 16
  • 29
  • 1
    perhaps try is.na() or is.finite() – AidanGawronski Oct 04 '16 at 14:13
  • I replaced the last condition with if (is.na(destSlot)) Still same result. – Scalable Oct 04 '16 at 14:15
  • 3
    It would be best if you could provide a small reproducible example. You can use `dput` to outpu the `x$data$TIME_STRING`. – Roman Luštrik Oct 04 '16 at 14:19
  • @Roman I have update the code and output to give better context. The handler is being called by an api with a time value as printed by dput – Scalable Oct 04 '16 at 14:38
  • 1
    I tried setting `test <- structure(1475591155, class = c("POSIXct", "POSIXt"))` and running `handler(test, 123)`. The function prints the values as in your output, but does not return an error; `prevSlot` and `data` have the expected values. – Weihuang Wong Oct 04 '16 at 14:47
  • I found the issue. The issue was was a combination of not checking null on transactionTime and later dataValue itslef. Thanks for steering me in the right direction. – Scalable Oct 04 '16 at 14:57
  • I think its unfair that you guys did'nt get a reward of your contribution. I will find a question you answered and reward that. – Scalable Oct 04 '16 at 15:32
  • 1
    We'll live. :) Answer your question with the correct solution and hopefully someone will find it useful in the future. Perhaps even you. – Roman Luštrik Oct 06 '16 at 10:49

0 Answers0