I'm working on data scraping a few data sources. For the sake of this question, I've created a sample example. 3 hopefully easy questions
- What is the best way to create a "master function" that calls the proper scrape function and returns the requested
df
? I have one way of doing it below - Would I need to create a separate master function if I wanted to row bind (I'm guessing using
purrr::map_dfr
)the results within the type argument? Ex. Getdf
for allstore
choices, fortype = "snack"
? - Let's say I want to use
write_csv
to write an individual.csv
file for eachstore
andtype
, is that an argument that I could add into the function to achieve question 2? How would I skip to the next "store" choice if there are no rows/an error without writing a blank.csv
file?
Example functions:
library(tidyverse)
# function 1
get_focus_brands <- function(store = c("cinnabon", "auntie annes"),
type = c("snack", "drinks"),
clean_columns = TRUE){
df <- tibble(
ITEM_NAME = c("cinnabon","cookie"),
price = c(3.99, 2.99)
)
if (clean_columns == TRUE) {
df <- df %>% janitor::clean_names()
}
df
}
# function 2
get_bk <- function(type = c("snack", "drinks"),
clean_columns = TRUE){
df <- tibble(
ITEM_NAME = c("soft serve vanilla","cookie"),
price = c(3.99, 2.99)
)
if(clean_columns == TRUE){
df <- df %>% janitor::clean_names()
}
df
}
# master function that calls get function based on store name choise
get_fast_food <- function(store, ...){
switch(store,
"cinnabon" =
get_focus_brands(...),
"auntie annes" =
get_focus_brands(...),
"burger king" =
get_bk(...))
}
df <- get_fast_food(store = "cinnabon",
type = "snack",
clean_columns = TRUE)
df