Closed MalditoBarbudo closed 2 years ago
slide()
isn't exactly the same as map()
. In particular, slide subsets with [
rather than the [[
that map uses. It does this because slide generally selects multiple elements at a time, while map always selects a single element. This means that you are calling log()
on things like list(1)
rather than 1
(the [
vs [[
difference really only affects lists). You can see this if you use a simpler function:
library(slider)
library(purrr)
test_list <- list(1, 2)
map(test_list, identity)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
slide(test_list, identity)
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] 2
So for this particular example you'd have to access the element of the list with [[
to make slide equivalent to map.
library(slider)
library(purrr)
test_list <- list(1,2,-1,'a')
safe_log <- safely(log)
map(test_list, safe_log)
#> Warning in .Primitive("log")(x, base): NaNs produced
#> [[1]]
#> [[1]]$result
#> [1] 0
#>
#> [[1]]$error
#> NULL
#>
#>
#> [[2]]
#> [[2]]$result
#> [1] 0.6931472
#>
#> [[2]]$error
#> NULL
#>
#>
#> [[3]]
#> [[3]]$result
#> [1] NaN
#>
#> [[3]]$error
#> NULL
#>
#>
#> [[4]]
#> [[4]]$result
#> NULL
#>
#> [[4]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
slide(test_list, ~safe_log(.x[[1]]))
#> Warning in .Primitive("log")(x, base): NaNs produced
#> [[1]]
#> [[1]]$result
#> [1] 0
#>
#> [[1]]$error
#> NULL
#>
#>
#> [[2]]
#> [[2]]$result
#> [1] 0.6931472
#>
#> [[2]]$error
#> NULL
#>
#>
#> [[3]]
#> [[3]]$result
#> [1] NaN
#>
#> [[3]]$error
#> NULL
#>
#>
#> [[4]]
#> [[4]]$result
#> NULL
#>
#> [[4]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
Thanks!
My real example is more complex and i will have to see how to adapt it to [[
notation but now I have the starter point.
purrr
allows to create safe versions of the functions used in themap*
family functions withpurrr::safely
and others. It would be nice to allowslide
to also accept these safe versions, specially when iterating the rows of a dataframe to carry on a side effect (writing to a database, creating objects with the rows, transforming other object with the data in the rows...). These side effects can fail due to external reasons, and a failsafe is needed to avoid stopping the rows loop.I tried to use a function created with
purrr::safely
but if any row fails, all results fail:Created on 2022-01-15 by the reprex package (v2.0.1)