jniedballa / camtrapR

R package for camera trap data management
https://jniedballa.github.io/camtrapR/
31 stars 5 forks source link

cameraOperation does not work with one camera #30

Closed LisaNicvert closed 3 months ago

LisaNicvert commented 4 months ago

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!

jniedballa commented 3 months ago

Hello, thank you very much for catching this. It is fixed on the dev branch: f28211e Please let me know if it doesn't work as expected. Best regards

LisaNicvert commented 3 months ago

Great, this seems to fix the bug! Thank you!