I will just paste a few chunks, full code is available by email or GitHub if needed. Here is where I assume the bottleneck is:
tags.detect <- as.data.table(
tbl(sqldb, "alltags") %>%
filter(tagProjID == 213, motusTagID %in% c(tags.meta$tagID, tags.ambigs$tagID),
speciesID %in% sp.list) %>%
select(runID, ts, sig, port, noise, freqsd, motusTagID,
ambigID, runLen, tagDeployID, tagDeployStart,
tagDeployEnd, tagDepLat, tagDepLon, deviceID, recvDeployID, recv,
speciesID, mfgID, motusFilter, antBearing, antType,
antHeight) %>%
collect()
)
tags.detect <- tags.detect[
, `:=`(ts = as_datetime(ts),
tagDeployStart = as_datetime(tagDeployStart),
tagDeployEnd = as_datetime(tagDeployEnd))
]
Or it could be here:
recvs <- tags.detect[
!is.na(recvDeployID),
.(recv, recvDeployID)
][unique(recv.meta), on = "recvDeployID"]
tags.detect <- tags.detect[
recvs,
on = .(recv, recvDeployID),
`:=`(recvLat = i.latitude, recvLon = i.longitude, recvType = i.recvType)
][ts >= tagDeployStart & ts <= tagDeployEnd & !is.na(recvLat)]