camtrapR icon indicating copy to clipboard operation
camtrapR copied to clipboard

cameraOperation does not work with one camera

Open LisaNicvert opened this issue 2 months ago • 0 comments

Hello, I am using the cameraOperation function with a CTable argument having one row in some instances. The function works perfectly fine with 2 or more cameras, but throws an error when there is only one camera.

library(camtrapR)

df2 <- data.frame(setup = as.POSIXct(c("2020-04-01 12:00:00", "2020-05-01 12:00:00")),
                  retrieval = as.POSIXct(c("2020-04-10 12:00:00", "2020-05-10 12:00:00")),
                  camera = c("A01", "A02"))
# This works
camtrapR::cameraOperation(df2,
                          stationCol = "camera",
                          setupCol = "setup",
                          retrievalCol = "retrieval",
                          dateFormat = "Ymd HMS",
                          hasProblems = FALSE) 

df1 <- data.frame(setup = as.POSIXct("2020-04-01 12:00:00"),
                  retrieval = as.POSIXct("2020-04-10 12:00:00"),
                  camera = "A01")
# This throws an error
camtrapR::cameraOperation(df1,
                          stationCol = "camera",
                          setupCol = "setup",
                          retrievalCol = "retrieval",
                          dateFormat = "Ymd HMS",
                          hasProblems = FALSE) 

The error I get is the following:

Error in `rownames<-`(`*tmp*`, value = "A01Cam1"): 
  attempt to set 'rownames' on an object with no dimensions

I believe this error appeared in the newest version of camtrapR (2.3.0) and is caused by the renaming operation rownames(camop_binary) <- rownames(camOp_empty) (in the function code), which is intended to be performed on rows of a matrix but happens on a vector when there is only one camera. I guess it should be easily fixable by setting a special case when there is only one camera!

Thank you for your time!

LisaNicvert avatar Apr 20 '24 08:04 LisaNicvert