A few weeks ago, I saw an example of someone using purrr::map()
within their function to map over that same function. This way, the function can automatically handle vectors of whatever size without the user having to work some map()
solution out by themselves.
Do you think this is a good practice to keep? I think it makes sense, and I do it in this function below. See the bottom of the function where I use map_dfr()
.
library(rvest)
library(tidyverse)
get_player_urls <- function(...) {
.get_player_urls <- function(player_letter, ...) {
if (tolower(player_letter) == "x") {
warning("Uh, no X-Men in the NHL. Sorry :/")
return(tibble())
}
else {
page <- str_c("https://www.hockey-reference.com/players/", player_letter, "/") %>% read_html()
player_url <- page %>%
html_nodes("#div_players a") %>%
html_attr("href") %>%
str_c("https://www.hockey-reference.com", .) %>%
as_tibble() %>%
set_names("url")
player_name <- page %>%
html_nodes("#div_players a") %>%
html_text() %>%
as_tibble() %>%
set_names("name")
player_data <- bind_cols(player_name, player_url)
return(player_data)
}
}
all_player_data <- map_dfr(..., .get_player_urls)
return(all_player_data)
}