I am trying to work out how to define a custom for
loop in R, or if that's even possible.
Examples
A couple of things that would be nice to have are
- something equivalent to ruby's
each_with_index
, as well as - A
for
loop that completely ignores exceptions (without having to manually code exception handling into the loop).
Is it possible to define a new kind of for loop in R (and if so, how), or is this an inherent limitation of the language and hence not something that can be done?
Use cases
Here's a random example of how for_each_with_index
could simplify finicky arithmetic
Suppose we want to scrape the 36th to the 55th article from a website and assign the output to a position in a list. This works well
library(rvest)
library(dplyr)
articles <- vector(mode = "list", length = 20)
for(i in 36:55) {
paste0("Scraping article ", i) %>% print
articles[[i - 35]] <- read_html(paste0("http://afr.herokuapp.com/articles/", i)) %>%
html_nodes("p") %>% html_text %>% paste0(collapse="/n")
}
But we see some finicky arithmetic (36:55
, i - 35
etc) that could theoretically be abstracted away through for_each_with_index
enumerating over each element of the articles
object, like so:
# NOT ACTUAL R CODE
library(rvest)
library(dplyr)
articles <- vector(mode = "list", length = 20)
for_each_with_index(articles, i) {
paste0("Scraping article ", i) %>% print
articles[[i]] <- read_html(paste0("http://afr.herokuapp.com/articles/", i + 35)) %>%
html_nodes("p") %>% html_text %>% paste0(collapse="/n")
}
By using for_each_with_index
, we avoided the fiddly arithmetic . This example is very simple, but when the complexity turns up some notches i.e when we have various conditionals, nested loops etc, things get much more complex and these seemingly small improvements in clarity become more profound