Hi. I have a function that returns the following list of characters.
eventids <-
[1] "314cdd9cecb9b66219c944996f0249b2" "ab26545fc28c693c52329db2a68a06a9"
"818b7fece8a6f82cecf0b4ef38f903d3"
[4] "9427475e5b454a44a4d9cc365d72e416" "3ee7569ac3f38bd5d62ca03b13ee1344"
"35c8c545c966fd2ad738dcccd02a6d8f"
[7] "f2aa3876acabca48a66ea6eae3d9f1b9" "63308e00869e2009bc6597edbdaf0c99"
"96e9ac4414cf1fb0ef4164d710c420c8"
[10] "e6b22267452d3dd4d99fa62ff71f7fcc" "8d78600b0a7ab6a7f15c5f935cbace68"
"80d27fa88ccd30a960caacce5dfac049"
[13] "c95982844161e998ab02c1eab506902d"
I then have the following function that executes against the above list
purrr::map(event_ids, ~my_func(sport = "basketball_nba", eventId = .x))
%>% bind_rows()
Under normal circumstances, when all eventids have valid data to return, something similar to the following will be returned.
my_func('basketball_nba', '314cdd9cecb9b66219c944996f0249b2')
A tibble: 475 × 13
id sport_key sport…¹ comme…² home_…³ away_…⁴ bookm…⁵ title key
last_…⁶ name price point
1 314cdd9cecb9b66219c944996f0249b2 basketbal… NBA 2023-0… Charlo… Miami … draftk… Draf…
alte… 2023-0… Char… -475 15.5
However, if one of the eventids doesn't have any data to return, I get the following error
nba_alt_lines <- purrr::map(event_ids, ~my_func(sport = "basketball_nba", eventId = .x))
Error in purrr::map()
:
In index: 7.
Caused by error in rename()
:
! Can't rename columns that don't exist.
Column key
doesn't exist.
Run rlang::last_error()
to see where the error occurred.
rlang::last_error()
<error/purrr_error_indexed>
Error inpurrr::map()
:
In index: 7.
Caused by error inrename()
:
! Can't rename columns that don't exist.
Columnkey
doesn't exist.
Backtrace:
- purrr::map(event_ids, ~my_func(sport = "basketball_nba", eventId = .x))
- dplyr:::rename.data.frame(., bookmaker_key = "key")
Is there a way to just skip the eventid that doesn't contain any data and process the others?
I have tried using purrr::safely but I am unsure if I used it correctly or if it will even accomplish what I need. The following command yields the following error
map(eventids, ~ safely(nba_alt_lines(sport = "basketball_nba", eventId = .x)))
Error in map()
:
In index: 1.
Caused by error in as_mapper()
:
! Can't convert .f
, a <tbl_df/tbl/data.frame> object, to a function.
Run rlang::last_error()
to see where the error occurred.
rlang::last_error()
<error/purrr_error_indexed>
Error inmap()
:
In index: 1.
Caused by error inas_mapper()
:
! Can't convert.f
, a <tbl_df/tbl/data.frame> object, to a function.
Backtrace:
- purrr::map(eventids, ~safely(nba_alt_lines(sport = "basketball_nba", eventId = .x)))
- purrr:::map_("list", .x, .f, ..., .progress = .progress)
- global .f(.x[[i]], ...)
- purrr::safely(nba_alt_lines(sport = "basketball_nba", eventId = .x))
- purrr:::as_mapper.default(.f)
- rlang::as_function(.f)
My end goal is to iterate through the eventid list and skipping any that don't contain data and combining those that do have data into a single tibble. I'm hoping somebody can help me.