-
Notifications
You must be signed in to change notification settings - Fork 1
/
runR.R
77 lines (64 loc) · 2.57 KB
/
runR.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
# ------------------------------------------------------------------------------
# GPS statistics
# Inspired by http://cnr.lwlss.net/GarminR/
# ------------------------------------------------------------------------------
source("gpsStats.R")
source("nike.R")
defaultLocalFnm = "Runs.rdat"
# ------------------------------------------------------------------------------
# Loads data from disk if available or from Nike otherwise. Returns df of gps
# data from multiple runs.
# ------------------------------------------------------------------------------
loadGps = function(fnm, maxRuns=10)
{
accessToken = getNikeAccessToken()
# Retrieve list of run metadata from Nike+
runs = listNikeRuns(maxRuns, accessToken)
# See whether we have previously cached runs. File stores gps data in variable "gps"
if(file.exists(fnm)){
# Only dowload non-cached runs
load(fnm)
cachedRuns = unique(gps$activityId)
downloadIds = setdiff(runs$activityId, cachedRuns)
message(paste("Need to download", length(downloadIds), "runs."))
} else{
# Dowload all runs
message(paste("Need to download", nrow(runs), "runs:"))
gps = processGps(getNikeSingleRun(runs$activityId[[1]], accessToken))
gps$startTime = runs$startTime[1]
downloadIds = runs$activityId[2:nrow(runs)]
}
# Download and process all necessary runs
for(id in downloadIds){
run = processGps(getNikeSingleRun(id, accessToken))
run$startTime = runs[runs$activityId==id, "startTime"]
gps = rbind(gps, run)
}
return(gps)
}
# ------------------------------------------------------------------------------
# Update locally store runs and load into global environment
# ------------------------------------------------------------------------------
updateLocalStore = function(fnm=defaultLocalFnm, maxDownloads=2){
gps <<- loadGps(fnm, maxDownloads)
stats <<- allStats(gps)
save(gps, stats, file=fnm)
}
# ------------------------------------------------------------------------------
# Converts df to D3 compatible json
# From http://theweiluo.wordpress.com/2011/09/30/r-to-json-for-d3-js-and-protovis/
# ------------------------------------------------------------------------------
toD3JSON = function(df){
names = colnames(df)
nameValue = function(nm){
quote = '';
if(class(df[, nm]) != 'numeric'){
quote = '"';
}
paste('"', nm, '" : ', quote, df[,nm], quote, sep='')
}
objs = apply(sapply(names, nameValue), 1, function(x){paste(x, collapse=', ')})
objs = paste('{', objs, '}')
res = paste('[', paste(objs, collapse=', '), ']')
return(res)
}