Skip to content

Instantly share code, notes, and snippets.

@mharris717
Created October 16, 2014 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharris717/6a4ef2f3dd364cde0c88 to your computer and use it in GitHub Desktop.
Save mharris717/6a4ef2f3dd364cde0c88 to your computer and use it in GitHub Desktop.
We must kill the Loop .... Man.
# pretending to fetch HTML
getBody <- function(url) {
if (url == "http://garbage.broken") return(0)
else return(paste("this is the html for ",url))
}
sites.names <- c("Google","ESPN","Garbage","ROK")
sites.urls <- c("http://google.com","http://espn.com","http://garbage.broken","http://returnofkings.com")
sites.df <- data.frame(Name=sites.names, Url=sites.urls, stringsAsFactors=FALSE)
# I want to get the HTML for the working sites
# No loop you guys
goodBodies <- Reduce(function(list,url) {
body <- getBody(url)
if (body != 0) c(list,body) else list
},sites.df["Url"][[1]],vector())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment