Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns the number of tags for a given token on StackOverflow.com
# Get StackOverflow data
get.stack<-function(tok) {
# Must check for XML install, thanks onertipaday!
if (!require(XML)) install.packages('XML')
library(XML)
# Enter a SO tag as character string, and number of tags are returned
tok<-gsub("(/| )","-",tok)
tok<-gsub("#","%23",tok,fixed=TRUE)
base.stack<-"http://stackoverflow.com/questions/tagged/"
stack.tree<-htmlTreeParse(paste(base.stack,tok,sep=""),useInternalNodes=TRUE)
tag.count<-getNodeSet(stack.tree,"//div[@class='module']/div[@class='summarycount al']")
tag.num<-suppressWarnings(as.numeric(gsub(",","",xmlValue(tag.count[[1]]),fixed=TRUE)))
if(is.na(tag.num)) {
warning(paste("Something went wrong trying to parse '",tok,"'.\nNA returned",sep=""))
}
return(tag.num)
}
@onertipaday

This comment has been minimized.

Copy link

@onertipaday onertipaday commented Dec 10, 2010

Pedantic note:

get.stack<-function(tok) {
    if (!require(XML)) install.packages('XML')
    library(XML)
    # Enter a SO tag as character string, and number of tags are returned
    tok<-gsub("(/| )","-",tok)
    tok<-gsub("#","%23",tok,fixed=TRUE)
    base.stack<-"http://stackoverflow.com/questions/tagged/"
    stack.tree<-htmlTreeParse(paste(base.stack,tok,sep=""),useInternalNodes=TRUE)
    tag.count<-getNodeSet(stack.tree,"//div[@class='module']/div[@class='summarycount al']")
    tag.num<-suppressWarnings(as.numeric(gsub(",","",xmlValue(tag.count[[1]]),fixed=TRUE)))
    if(is.na(tag.num)) {
        warning(paste("Something went wrong trying to parse '",tok,"'.\nNA returned",sep=""))
    }
    return(tag.num)
}
@drewconway

This comment has been minimized.

Copy link
Owner Author

@drewconway drewconway commented Dec 10, 2010

Great catch, thank you! I updated the above code.

@onertipaday

This comment has been minimized.

Copy link

@onertipaday onertipaday commented Dec 10, 2010

My pleasure! Thanks for the code! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment