Trackage / trip

trip package development
http://trackage.github.io/trip/
12 stars 2 forks source link

easy trip constructors #21

Closed mdsumner closed 5 years ago

mdsumner commented 7 years ago

This is completely hopeless as a way of working:

chunk <- 500
tab <- tbl(db, "Tracking") %>% 
   arrange/distinct/mutate/rename etc. 

## 3 points don't make it
tab <- tab %>% group_by(ID) %>% filter(n() > 3) %>% ungroup() %>% as.data.frame()
## shouldn't have to make a copy
  tr <- tab
## needs to be elevated unjoin(tr, c("x", "y")) or aes(x, y)
  coordinates(tr) <- c("x", "y")
## again, shouldn't be so awkward
  proj4string(tr) <- "+init=epsg:4326"
## I constantly forget which way so  aes(x, y, time = gmt, id = ID) or unjoin(x, y, time, id)
  tr <- trip(tr, c("gmt", "ID"))
## bespoke filter idiom e.g. filter(speedfilter(x, y, gmt))  ?
  tr <- tr[speedfilter(tr, max.speed = max_speed), ]
## at least allow trip to work with tibble
  tab <- as.data.frame(tr) %>% as_tibble()

Make some easy improvements to help using trip with the current functionality.

mdsumner commented 7 years ago

Early stab here:

https://github.com/Trackage/trip/blob/aes/R/gg-aesthetics.R

Still need:

mdsumner commented 5 years ago

I think this is waaay better, also toying with rlang approach to catpure trip(data, x, y, t, g, ...) as the column names.

trip.grouped_df <- function(obj, ..., crs = NULL) {
   tor <- c(names(obj)[3], dplyr::group_vars(obj))
   if (!inherits(obj[[tor[1]]], "POSIXct")) stop(sprintf("3rd column [%s] must be date-time", tor[1]))
   obj <- dplyr::ungroup(obj)
   sp::coordinates(obj) <- names(obj)[1:2]
   if (!is.null(crs)) sp::proj4string(obj)
   trip(obj, tor)
}

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(trip)
#> Loading required package: sp

d <- tibble::as_tibble(walrus818)
d <- d %>% group_by(Deployment)
d <- d %>% select(X_AED170_70,Y_AED170_70, DataDT, everything())
tr <- trip.grouped_df(d)

## the really neat part is that this grouped data frame is directly useable with
## ggplot2 as-is
library(ggplot2)
ggplot(d, aes(X_AED170_70, Y_AED170_70, col = DataDT)) + geom_path()

Created on 2019-04-05 by the reprex package (v0.2.1)

mdsumner commented 5 years ago

Pay heed to feedback here: https://gist.github.com/mdsumner/e2fc9d282d82bb3d5a2ea55dd2426874#gistcomment-2881887

mdsumner commented 5 years ago

This is done!

Also added mousetrap, amt and trackeR support see https://github.com/Trackage/trip/issues/29

mdsumner commented 5 years ago

thanks @ianjonsen ;)