For loop in a package function

Viewed 20

I have this for loop, meant to search through a list of URLs and calls a function to scrape off the web and return a list of recipes where a match occurs. It works great on its own.

for (recipe_url in recipe_urls) {
  if ("allrecipes.*" %rin% recipe_urls){
    scrape_allrecipes(recipe_url)
  }
  if("foodnetwork.*" %rin% recipe_urls){
    scrape_foodnetwork(recipe_url)
  }
}

Note: %rin% is a custom function to search for a partial string of text.

`%rin%` = function (pattern, list) {
  vapply(pattern, function (p) any(grepl(p, list)), logical(1L), USE.NAMES = FALSE)
}

However, the problem is that when I tried to put it inside of a function in a package, it gives me this error:

Error in for (recipe_url in recipe_urls) { : invalid for() loop sequence

Version I converted to a function in a package:

scrape <- function(scrape) {
for (recipe_url in recipe_urls) {
  if ("allrecipes.*" %rin% recipe_urls){
    scrape_allrecipes(recipe_url)
  }
  if("foodnetwork.*" %rin% recipe_urls){
    scrape_foodnetwork(recipe_url)
  }
}
}

I sense that it's either to do with it now being in a function, or something to do with how a package works (never made a package before so I am unfamiliar).

Any advice is appreciated!

0 Answers
Related