-
-
Save epijim/8937299 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(gdata) | |
require("RCurl") | |
require("XML") | |
AtcSearch <- function(AtcCodes){ | |
#Function inside a function is bad practice... | |
#DummySearch searches for one atc code | |
DummySearch <- function(Code){ | |
#A link to WHO search | |
htmlLink <- paste("http://www.whocc.no/atc_ddd_index/?code=", Code, sep="") | |
doc <- htmlParse(getURL(htmlLink)) | |
Drug <- xpathSApply(doc, "//td", xmlValue) | |
#No idea why these three are needed | |
Drug <- gsub("Â", "", Drug) | |
Drug <- gsub("\\s", " ", Drug) | |
Drug <- trim(Drug) #gdata just for this... | |
#if no drug is found, returning NULL | |
if(sum(Drug == Code) == 0 | length(Drug) == 0) return() | |
atcLevels <- xpathSApply(doc, "//b", xmlValue) | |
out <- matrix(Drug[which(Drug == Code):length(Drug)], ncol= which(Drug == Code)-1, byrow=TRUE) | |
#Basic handling of multiple DDDs and Units | |
if(nrow(out) > 1) out <- t(apply(out, 2, function(x) paste(x[x != ""], collapse="/"))) | |
out <- data.frame(out, t(atcLevels)) | |
colnames(out) <- c(Drug[1:6], "Anatomical", "Therapeutic", "Subgroup", "ChemicalSubgroup") | |
out | |
} | |
#lapply to get all the codes. | |
ret <- lapply(AtcCodes, DummySearch) | |
do.call("rbind", ret) | |
} | |
#Some examples | |
AtcSearch("N01AX15") | |
AtcSearch(c("B01AA01", "N01AX15")) | |
#Not perfect for example, the who database isn't complete. | |
AtcSearch("QN01AX99") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment