jmsigner / amt

37 stars 13 forks source link

dplyr issue: Error: `.data` is a corrupt grouped_df, the `"groups"` attribute must be a data frame #7

Closed kjklauder closed 4 years ago

kjklauder commented 4 years ago

I have code drawn from the amt vignette to regularize some GPS data stored as an sf object. It was working a few months ago but when I came back to it, an update to dplyr seems to be causing problems (or so I assume). Original code:

amttestA<-make_track(pointsf,
                    lat_albers,long_albers,fixtime,id=AnimalId,
                    Gender=Gender, in.dena=in.dena,disttobound=disttobound,
                    bear.bout=bear.bout,f.cat=f.cat,elevation=elevation)
amttestB<-amttestA%>%
  group_by(id)%>%
  nest()

amttestC<-amttestB%>%
  mutate(burst=map(data,function(x)
    x%>%
      track_resample(rate=hours(2),toleranace=minutes(20),
                     Gender=Gender, in.dena=in.dena,disttobound=disttobound,
                     bear.bout=bear.bout,f.cat=f.cat,elevation=elevation)))

The first issue I encountered was this clash between sf and tidyr when using nest(); consequently I had to make my sf object a dataframe so that nest() would work:

amttestA<-make_track(as.data.frame(pointsf),
                    lat_albers,long_albers,fixtime,id=AnimalId,
                    Gender=Gender, in.dena=in.dena,disttobound=disttobound,
                    bear.bout=bear.bout,f.cat=f.cat,elevation=elevation)
amttestB<-amttestA%>%
  group_by(id)%>%
  nest()

amttestC<-amttestB%>%
  mutate(burst=map(data,function(x)
    x%>%
      track_resample(rate=hours(2),toleranace=minutes(20),
                     Gender=Gender, in.dena=in.dena,disttobound=disttobound,
                     bear.bout=bear.bout,f.cat=f.cat,elevation=elevation)))

The final call, to create the bursts, produces the error:

"Error: `.data` is a corrupt grouped_df, the `"groups"` attribute must be a data frame 

Running with debug pulls up:

function (df, quo) 
{
  .Call(`_dplyr_filter_impl`, df, quo)
}

This appears to be very similar to this issue. I've tried various combinations of grouping and ungrouping the objects before the call to burst, and/or making my data object a vanilla dataframe in a couple different ways, and still get the same error. I'm sorry this isn't a reprex; the data are sensitive. If you're having trouble reproducing it I can email some. Thanks for your work on this package!

Traceback:

Error: `.data` is a corrupt grouped_df, the `"groups"` attribute must be a data frame 
37.
stop(structure(list(message = "`.data` is a corrupt grouped_df, the `\"groups\"` attribute must be a data frame", 
    call = NULL, cppstack = NULL), class = c("Rcpp::exception", 
"C++Error", "error", "condition"))) 
36.
filter_impl(.data, quo) 
35.
filter.tbl_df(x, !!quo(burst_ > 0)) 
34.
NextMethod() 
33.
as.vector(y) 
32.
setdiff(from$class, class(to)) 
31.
track_transfer_attr(.data, NextMethod()) 
30.
filter.track_xy(x, !!quo(burst_ > 0)) 
29.
NextMethod() 
28.
as.vector(y) 
27.
setdiff(from$class, class(to)) 
26.
track_transfer_attr(.data, NextMethod()) 
25.
filter.track_xyt(x, !!quo(burst_ > 0)) 
24.
filter(x, !!quo(burst_ > 0)) 
23.
track_resample.track_xyt(., rate = hours(2), toleranace = minutes(20), 
    Gender = Gender, in.dena = in.dena, disttobound = disttobound, 
    bear.bout = bear.bout, f.cat = f.cat, elevation = elevation) 
22.
track_resample(., rate = hours(2), toleranace = minutes(20), 
    Gender = Gender, in.dena = in.dena, disttobound = disttobound, 
    bear.bout = bear.bout, f.cat = f.cat, elevation = elevation) 
21.
function_list[[k]](value) 
20.
withVisible(function_list[[k]](value)) 
19.
freduce(value, `_function_list`) 
18.
`_fseq`(`_lhs`) 
17.
eval(quote(`_fseq`(`_lhs`)), env, env) 
16.
eval(quote(`_fseq`(`_lhs`)), env, env) 
15.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
14.
x %>% track_resample(rate = hours(2), toleranace = minutes(20), 
    Gender = Gender, in.dena = in.dena, disttobound = disttobound, 
    bear.bout = bear.bout, f.cat = f.cat, elevation = elevation) 
13.
.f(.x[[i]], ...) 
12.
map(data, function(x) x %>% track_resample(rate = hours(2), toleranace = minutes(20), 
    Gender = Gender, in.dena = in.dena, disttobound = disttobound, 
    bear.bout = bear.bout, f.cat = f.cat, elevation = elevation)) 
11.
mutate_impl(.data, dots, caller_env()) 
10.
mutate.tbl_df(., burst = map(data, function(x) x %>% track_resample(rate = hours(2), 
    toleranace = minutes(20), Gender = Gender, in.dena = in.dena, 
    disttobound = disttobound, bear.bout = bear.bout, f.cat = f.cat, 
    elevation = elevation))) 
9.
mutate(., burst = map(data, function(x) x %>% track_resample(rate = hours(2), 
    toleranace = minutes(20), Gender = Gender, in.dena = in.dena, 
    disttobound = disttobound, bear.bout = bear.bout, f.cat = f.cat, 
    elevation = elevation))) 
8.
function_list[[k]](value) 
7.
withVisible(function_list[[k]](value)) 
6.
freduce(value, `_function_list`) 
5.
`_fseq`(`_lhs`) 
4.
eval(quote(`_fseq`(`_lhs`)), env, env) 
3.
eval(quote(`_fseq`(`_lhs`)), env, env) 
2.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
1.
amttestB %>% mutate(burst = map(data, function(x) x %>% track_resample(rate = hours(2), 
    toleranace = minutes(20), Gender = Gender, in.dena = in.dena, 
    disttobound = disttobound, bear.bout = bear.bout, f.cat = f.cat, 
    elevation = elevation))) 

Session info:

R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.4.0       stringr_1.4.0       dplyr_0.8.3         purrr_0.3.3        
 [5] tidyr_1.0.0         tibble_2.1.3        tidyverse_1.3.0     adehabitatLT_0.3.24
 [9] CircStats_0.2-6     boot_1.3-22         MASS_7.3-51.3       adehabitatMA_0.3.13
[13] ade4_1.7-13         effects_4.1-0       carData_3.0-2       amt_0.0.6          
[17] geosphere_1.5-7     rgeos_0.4-3         rgdal_1.4-3         sf_0.7-4           
[21] tmap_2.2            readxl_1.3.1        data.table_1.12.2   readr_1.3.1        
[25] ggplot2_3.2.1       plyr_1.8.4          lubridate_1.7.4     sp_1.3-1           

loaded via a namespace (and not attached):
 [1] nlme_3.1-137       fs_1.2.7           satellite_1.0.1    httr_1.4.1        
 [5] webshot_0.5.1      RColorBrewer_1.1-2 mapview_2.6.3      tools_3.5.3       
 [9] backports_1.1.3    R6_2.4.0           KernSmooth_2.23-15 DBI_1.0.0         
[13] lazyeval_0.2.2     colorspace_1.4-1   nnet_7.3-12        raster_2.8-19     
[17] withr_2.1.2        tidyselect_0.2.5   leaflet_2.0.2      compiler_3.5.3    
[21] cli_1.1.0          rvest_0.3.5        xml2_1.2.2         scales_1.0.0      
[25] classInt_0.3-3     digest_0.6.23      minqa_1.2.4        base64enc_0.1-3   
[29] dichromat_2.0-0    pkgconfig_2.0.2    htmltools_0.3.6    lme4_1.1-21       
[33] dbplyr_1.4.2       htmlwidgets_1.3    rlang_0.4.2        rstudioapi_0.10   
[37] shiny_1.3.2        generics_0.0.2     jsonlite_1.6       crosstalk_1.0.0   
[41] magrittr_1.5       Matrix_1.2-17      Rcpp_1.0.1         munsell_0.5.0     
[45] lifecycle_0.1.0    stringi_1.4.3      tmaptools_2.0-1    grid_3.5.3        
[49] promises_1.0.1     crayon_1.3.4       lattice_0.20-38    haven_2.2.0       
[53] splines_3.5.3      hms_0.5.2          zeallot_0.1.0      pillar_1.4.2      
[57] codetools_0.2-16   stats4_3.5.3       reprex_0.3.0       XML_3.98-1.19     
[61] glue_1.3.1         modelr_0.1.5       png_0.1-7          vctrs_0.2.0       
[65] nloptr_1.2.1       httpuv_1.5.1       cellranger_1.1.0   gtable_0.3.0      
[69] assertthat_0.2.1   mime_0.6           lwgeom_0.1-6       xtable_1.8-3      
[73] broom_0.5.2        survey_3.35-1      e1071_1.7-1        later_0.8.0       
[77] class_7.3-15       survival_2.43-3    viridisLite_0.3.0  units_0.6-2 
kjklauder commented 4 years ago

UPDATE: Was able to resolve this issue by mirroring the code in the latest version of the vignette (2019-09-19), which contains new syntax for tidyr::nest() that I was unaware of. Apologies for not observing it more closely.

amttestB<-amttestA%>% nest(data=-"id")

The call to map the burst function now works fine.