Skip to content

Instantly share code, notes, and snippets.

@drewconway
Created December 9, 2010 22:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewconway/735430 to your computer and use it in GitHub Desktop.
Save drewconway/735430 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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

@onertipaday
Copy link

My pleasure! Thanks for the code! :-)

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