I two questions regarding loops in R
.
1) I'm using XML
package to scrap some tables from the website and combine them using rbind
. I'm using following command and it is working without issues if price data and tables are present in the given websites.
url.list <- c("www1", "www2", "www3")
for(url_var in url.list)
{
url <- url_var
url.parsed <- htmlParse(getURL(url), asText = TRUE)
tableNodes <- getNodeSet(url.parsed, '//*[@id="table"]/table')
newdata <- readHTMLTable(tableNodes[[1]], header=F, stringsAsFactors=F)
big.data <- rbind(newdata, big.data)
Sys.sleep(30)
}
But sometimes web page does not have corresponding table (in this case I'm left with one variable table with the message: No current prices reported.
) and my loop stops with following error message (since number of table columns do not match):
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
I want R
to ignore the error and go ahead with the next web page (skipping the one that has different number of columns).
2) In the end of the loop I have Sys.sleep(30)
. Does it force R
to wait 30 seconds before it tries next web page.
Thank you