-
Notifications
You must be signed in to change notification settings - Fork 2
/
clinicalmap.R
89 lines (72 loc) · 2.8 KB
/
clinicalmap.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Load libraries
library(XML)
library(plyr)
library(RCurl)
library(utils)
library(rMaps)
library(geomapdata)
library(rgdal)
library(RgoogleMaps)
library(httr)
library(rjson)
# Define search term
keyword <- "tuberculosis"
start_date <- "1/1/2009"
stop_date <- "12/31/2014"
trial_status <- "Closed"
# Construct search term and link
keyword <- as.character(gsub(" ", "+", as.character(keyword)))
download.file(paste(
"http://clinicaltrials.gov/search?term=", keyword,"&recr=", trial_status ,"&rcv_s=", start_date, "&rcv_e=", stop_date, "&studyxml=true", sep=''), destfile="search_result.zip", mode="wb")
# Unzip & remove zip file
unzip("search_result.zip")
file.remove("search_result.zip")
file.list <- list.files(pattern = ".xml")
location_tab <- NULL
for (i in 1:length(file.list))
{
adat <- xmlTreeParse(file.list[i], useInternalNodes = TRUE)
location_tab <- rbind(location_tab,
if(ncol(xmlToDataFrame(getNodeSet(adat, "//address//country"))) > 0)
{
cbind(
as.character(xmlToDataFrame(getNodeSet(adat, "//nct_id"))[1,]),
if(ncol(xmlToDataFrame(getNodeSet(adat, "//city"))) > 0)
{
as.character(xmlToDataFrame(getNodeSet(adat, "//city"))[,1])
} else {
NULL},
as.character(xmlToDataFrame(getNodeSet(adat, "//address//country"))[,1]))
} else {
NULL
})
}
location_tab <- as.data.frame(location_tab)
colnames(location_tab) <- c("trialId", "city", "country")
# Tried this API service looks like it cannot access the other countries locations
# Get longitude and latitude coordinates
# location_tab$address<-paste(location_tab$city,",",location_tab$country)
# data <- paste0("[",paste(paste0("\"",location_tab$address,"\""),collapse=","),"]")
# url <- "http://www.datasciencetoolkit.org/street2coordinates"
# response <- POST(url,body=data)
# json <- fromJSON(content(response,type="text"))
# geocode <- do.call(rbind,sapply(json,function(x) c(long=x$longitude,lat=x$latitude)))
get_geocode <- geocode(as.character(location_tab[,2]))
location_tab$long <- as.numeric(get_geocode[,1])
location_tab$lat <- as.numeric(get_geocode[,2])
# Using rMaps()
map <- Leaflet$new()
map$setView(c(33.320731,-14.033990), zoom = 2)
map$tileLayer(provider = 'Stamen.Toner')
map$set(width = 1600, height =800)
for (i in 1:nrow(location_tab))
{
if(is.na(location_tab$lat[i]) | is.na(location_tab$lon[i])) {next}
map$marker(
c(location_tab$lat[i], location_tab$lon[i]),
bindPopup = location_tab$trialId[i])
}
map
sink("outfile.txt")
map$show('iframesrc',cdn=TRUE)
sink()