hrbrmstr / omdbapi

R package to access the OMDB API (http://www.omdbapi.com/)
Other
40 stars 13 forks source link

Error: Column `Ratings` must be length 1, not 0 #6

Open robinpellegrims opened 6 years ago

robinpellegrims commented 6 years ago

For some reason, omdbapi sometimes returns an empty value for the Ratings field (probably a bug on their side). But when this happens, this library's functions throw the following error:

Error: Column 'Ratings' must be length 1, not 0

Can be handled more gracefully? It is still interesting to get all the other fields as a result.

Example call: find_by_id('tt6911608', type='movie', include_tomatoes = TRUE)

Omdb api response: {"Title":"Mamma Mia! Here We Go Again","Year":"2018","Rated":"PG-13","Released":"20 Jul 2018","Runtime":"114 min","Genre":"Comedy, Musical","Director":"Ol Parker","Writer":"Ol Parker (screenplay by), Richard Curtis (story by), Ol Parker (story by), Catherine Johnson (story by), Judy Craymer (originally conceived by), Catherine Johnson (based on the original musical by)","Actors":"Lily James, Amanda Seyfried, Meryl Streep, Dominic Cooper","Plot":"In this sequel to Mamma Mia!, Sophie learns about her mother's past while pregnant herself.","Language":"English","Country":"UK, USA","Awards":"N/A","Poster":"https://m.media-amazon.com/images/M/MV5BMjEwMTM3OTI1NV5BMl5BanBnXkFtZTgwNDk5NTY0NTM@._V1_SX300.jpg","Ratings":[],"Metascore":"N/A","imdbRating":"N/A","imdbVotes":"N/A","imdbID":"tt6911608","Type":"movie","DVD":"N/A","BoxOffice":"N/A","Production":"Universal Pictures","Website":"N/A","Response":"True"}

I'm guessing the issue is in using dplyr::as_data_frame on the following line: https://github.com/hrbrmstr/omdbapi/blob/593775319bc8ce1ad004e3a20a73369fd98247b2/R/title_or_id.R#L89

jemus42 commented 5 years ago

I often run into this issue with a different API wrapper, and I have yet to find a perfect solution.
For now, I usually pre-process the response with something like

ret <- purrr::map_if(ret, is.null, ~return(NA_character))

Example:

ret <- list(a = 4, b = NULL)
map_if(ret, is.null, ~return(NA))

$a
[1] 4

$b
[1] NA

This at least makes the missing value explicit, doesn't mess with data.frame (or the stricter tibble) conversion, and it doesn't drop the variable entirely, which is helpful in cases where you want to rbind multiple results, but may run into variable mismatch.

I'm hoping to make a PR soon to hopefully fix this.

jemus42 commented 5 years ago

Wait a second… I just ran your provided example (find_by_id('tt6911608', type='movie', include_tomatoes = TRUE)) and it works just fine. Soooo yeah. Might need a way to reproduce before I can try to fix it, or figure out if it even needs to be fixed.

robinpellegrims commented 5 years ago

Yeah I think it happens with movies that were recently released or something. 'tt6911608' also works fine here in the meantime, but the same error is now encountered with (currently) recently released movies like 'tt4154664' (Captain Marvel).