Error in `purrr::map()` Caused by error in `rename()`:

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():
:information_source: In index: 7.
Caused by error in rename():
! Can't rename columns that don't exist.
:heavy_multiplication_x: Column key doesn't exist.
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/purrr_error_indexed>
Error in purrr::map():
:information_source: In index: 7.
Caused by error in rename():
! Can't rename columns that don't exist.
:heavy_multiplication_x: Column key doesn't exist.


Backtrace:

  1. purrr::map(event_ids, ~my_func(sport = "basketball_nba", eventId = .x))
  2. 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():
:information_source: 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 in map():
:information_source: In index: 1.
Caused by error in as_mapper():
! Can't convert .f, a <tbl_df/tbl/data.frame> object, to a function.


Backtrace:

  1. purrr::map(eventids, ~safely(nba_alt_lines(sport = "basketball_nba", eventId = .x)))
  2. purrr:::map_("list", .x, .f, ..., .progress = .progress)
  3. global .f(.x[[i]], ...)
  4. purrr::safely(nba_alt_lines(sport = "basketball_nba", eventId = .x))
  5. purrr:::as_mapper.default(.f)
  6. 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.

Can you share the details of my_func()?

Here is my function. It queries an API to retrieve a list of alternate spreads for upcoming basketball games.

my_func <- function(sport,
                eventId = eventids,
                apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx",
                regions = "us",
                markets = "alternate_spreads",
                odds_format = "american",
                date_format = "iso"){
    
    base_url = glue::glue("https://xxx.xxx-xxx- 
xxx.xxx/xx/xxxxxx/{sport}/xxxxxx/{eventId}/odds")
    
    query_params <- list(apiKey = apiKey,
                         sport = sport,
                         eventId = eventId,
                         regions = regions, 
                         markets = markets,
                         oddsFormat = odds_format,
                         dateFormat = date_format)
    
    endpoint <- httr::modify_url(base_url, query = query_params)
    
    resp <- fromJSON(endpoint) %>% 
            as_tibble(validate = F) %>% 
            unnest(cols = bookmakers) %>% 
            rename("bookmaker_key" = "key") %>% 
            unnest(cols = markets) %>% 
            unnest(cols = outcomes)
    
    return(resp)
}

I am passing the following list of eventids as an argument. Each eventid is a single basketball game.

eventids <- nba_h2h %>% 
    distinct(id) %>% 
    pull()
             
 [1] "314cdd9cecb9b66219c944996f0249b2" "ab26545fc28c693c52329db2a68a06a9" 
"818b7fece8a6f82cecf0b4ef38f903d3"
 [4] "9427475e5b454a44a4d9cc365d72e416" "3ee7569ac3f38bd5d62ca03b13ee1344" 
"35c8c545c966fd2ad738dcccd02a6d8f"
 [7] "f2aa3876acabca48a66ea6eae3d9f1b9" "63308e00869e2009bc6597edbdaf0c99" 
"96e9ac4414cf1fb0ef4164d710c420c8"
[10] "e6b22267452d3dd4d99fa62ff71f7fcc" "8d78600b0a7ab6a7f15c5f935cbace68" 
"80d27fa88ccd30a960caacce5dfac049"
[13] "c95982844161e998ab02c1eab506902d"

This is the function that I am using to iterate over the list of eventids

purrr::map(eventids, ~my_func(sport = "basketball_nba", eventId = .x))

Again, the issue arises if one of the eventids doesn't have any data to return.  It can exist as an event but not have any data yet.

Thanks for sharing. When an event id is passed that doesn't have any data to return, what do you get for the following?

resp <- fromJSON(endpoint) %>% 
            as_tibble(validate = F)

A friendly request to follow the policy on cross-posting:

My apologies and duly noted.

I"ll close this out here for now as I posted in StackOverflow as well. I'll see if I make any headway there before pursuing here. Again, my apologies.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.