0

I have the following code and for some reason I can not get it to go to the next record. I put the print(innallURL) before the if(!file.exists) command and I get all the urls, I put it after where it is no, and nothing shows up, so that makes me think the files are failing the file.exists part. I've test 5 of the URLs and they work/exist in the web directory. I've used the next in other loops and have not had any problems. I also tried adding url= in fron of the string. I've searched here, but I can only find the formula I'm using in the examples. Please point out what idiotic thing I'm overlooking. ha! Thanks!

gidURLs <- as.list(as.data.frame(t(allGids)))

#get pitches

#set filename

for (thisgidURL in gidURLs) {

innallURL = paste(thisgidURL, "inning/inning_all.xml", sep="")

  if (!file.exists(innallURL)) next

print(innallURL)

pitches <- read_xml(innallURL)

# get all at-bats ---------------------------------------------------------

atbat <- xml_find_all(pitches, "//atbat")

# make a giant data frame -------------------------------------------------

bind_rows(lapply(atbat, function(x) {

  pitches <- xml_find_all(x, "./pitch")

bind_rows(lapply(pitches, function(y) {
  data.frame(t(xml_attrs(y)), stringsAsFactors=FALSE)
})) -> pitch_dat

pitch_dat$atbat_num <- xml_attr(x, "num")

pitch_dat

})) -> bats

game_id = substr(url, 66, 95)
bats$game_id <- game_id

allbatsdf <- data.frame(bats)
allbats <- rbind(bats, allbats)

}

example innallURLs from the list when its printed before the file.exists command.

"http://gd2.mlb.com/components/game/win/year_2015/month_01/day_30/gid_2015_01_30_magwin_oriwin_1/inning/inning_all.xml"
"http://gd2.mlb.com/components/game/win/year_2015/month_01/day_31/gid_2015_01_31_sydwin_prtwin_1/inning/inning_all.xml"
0

1 Answer 1

0

We can use httr package to use GET method.

library(httr)
if(GET(innallURL)$status_code!=200) next
1
  • Thanks! Frustrating being a noob to r that something that simple didn't show up anywhere else. Guess I wasn't using the right keywords.
    – Don Hessey
    Aug 31, 2015 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.