Closed geodevm closed 2 months ago
Just a quick comment to say that I ran into the same error with random_steps(), and tweaking from sapply() to lapply() worked! Thanks so much, saved me a ton of time!!
I also faced same issue and the change from sapply() to lapply() solved it. Commented here maybe to increase weight to this issue and to thank @geodevm for figuring out a quick solution. I also want to add that I didn't face this issue on a previous version of the package, and got this problem when I updated it in the past couple of weeks or so. I used the same data set for both cases.
I also had a similar issue issue. Adding simplify = FALSE
fixed it for me.
start_ids <- c(1, head(cumsum(rle(unlist(sapply(bursts, "[[", "burst_", simplify = FALSE)))$lengths), -1) + 1)
I ran into an issue preparing data for a SSF analysis, which I debugged on my own but thought deserved attention. Since this is unpublished data I can't share the data that caused the error, but I can share what caused the error.
This function that I wrote to prep data for analysis is what caused the error:
Most of this function runs fine and can be disregarded, but it can provide some context to what is being done here. There are multiple individuals of multiple species in one GPS collar dataset, and this is a nest--unnest iteration over those animals to fit tracks. The
random_steps()
line of this function is what causes the error, and it returns it in both this, a simplified example of the prep I'm doing for the analysis, and a more complicated version using a population-level gamma and von Mises distribution.When run on my dataset (you'll see it referenced as
gps
), this code returns the following error:Running
rlang::last_trace()
returns:As you can see, the function gets through multiple iterations of the nested tibble before running into this issue on index 12. Like I said, I cannot share the data that caused the issue since it is not yet published, but there were no evident differences between this index of the nested tibble and indices 1 through 11.
I dug into the code of the function
random_steps()
to find which line is throwing the error, which is line 130 in therandom_steps.R
file:start_ids <- c(1, head(cumsum(rle(unlist(sapply(bursts, "[[", "burst_")))$lengths), -1) + 1)
For some reason
sapply()
's "user-friendly" implementation, which allows coercion to lists OR arrays, decides to produce an array in this particular index. This breaks the function, throwing an error as soon asunlist()
is called within this line. Again, I don't know why it coerces into an array rather than a list on a dataframe with the same structure as the others being iterated through, but the fix was to coerce directly into a list by changing thesapply()
in this line of code into alapply()
, as follows:start_ids <- c(1, head(cumsum(rle(unlist(lapply(bursts, "[[", "burst_")))$lengths), -1) + 1)
That produced exactly what I was hoping for in the function output. Not sure why this is an issue for my dataset, but coercing directly to lists through
lapply()
rather thansapply()
in this function might save someone with a similarly structured dataset the debugging headache I just went through in the future.