forked from phillipsjs/GoSplitGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
71 lines (58 loc) · 3.15 KB
/
server.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
library(shiny)
library(ggplot2)
library(tidyr)
#devtools::install_github("corynissen/fitbitScraper")
library("fitbitScraper")
mypassword <- "Lsf87Qq6"
cookie <- login(email="[email protected]", password=mypassword)
startDate <- Sys.Date()-29
endDate <- Sys.Date()
goal <- 600
bonus <- 826
penalty <- 229
dataset <- get_premium_export(cookie, what="ACTIVITIES", start_date=as.character(startDate),
end_date=as.character(endDate))
dataset$score <- dataset$Minutes.Lightly.Active + dataset$Minutes.Fairly.Active*2 + dataset$Minutes.Very.Active*4
dataset$goal[dataset$Date==endDate] <- "Today"
dataset$goal[dataset$score>=bonus & dataset$Date<endDate] <- "Bonus"
dataset$goal[dataset$score<bonus & dataset$Date<endDate] <- "Goal"
dataset$goal[dataset$score<goal & dataset$Date<endDate] <- "Rest"
dataset$goal[dataset$score<=penalty & dataset$Date<endDate] <- "Penalty"
dataset$goal <- factor(dataset$goal, levels = c("Today","Bonus","Goal","Rest","Penalty"))
#dataset$Date <- weekdays(as.Date(dataset$Date))
#weekdays <- c(weekdays(as.Date(startDate)),weekdays(as.Date(startDate+1)),weekdays(as.Date(startDate+2)),weekdays(as.Date(startDate+3))
# ,weekdays(as.Date(startDate+4)),weekdays(as.Date(startDate+5)),weekdays(as.Date(startDate+6)))
#dataset$Date <- factor(dataset$Date, levels=weekdays)
dataset$Date <- as.Date(dataset$Date)
## this converts character numeric vectors to integers
for (i in names(dataset[,-1])) {
if (class(dataset[[i]])=="character") {
dataset[[i]] <- gsub(",", "", dataset[[i]])
dataset[[i]] <- as.integer(dataset[[i]])
}
}
dataset <- gather(dataset,show,score,2:11)
dataset$show <- factor(c("Calories Burned","Steps Taken","Distance","Floors Climbed","Minutes Sedentary",
"Low Activity Minutes","Medium Activity Minutes","High Activity Minutes",
"Active Calories Burned","GoSplitGo Score")[dataset$show])
dataset$show <- factor(dataset$show, levels=c("GoSplitGo Score", "High Activity Minutes", "Medium Activity Minutes",
"Low Activity Minutes","Minutes Sedentary","Steps Taken","Distance","Floors Climbed",
"Calories Burned","Active Calories Burned"))
library(scales) # To get date formatting for the plot
shinyServer(function(input, output) {
show <- reactive({paste(input$display)})
output$caption <- renderText({show()})
# Fill in the spot we created for a plot
output$GSGPlot <- renderPlot({
##ggplot
print(ggplot(dataset[dataset$show==input$display & dataset$Date>(Sys.Date()-input$past),], aes(x=Date, y=score, fill=goal)) +
geom_bar(stat="identity",position="dodge", color="black") +
geom_text(aes(label=score,y=score*1.1)) +
ylab("") +
xlab("Date") +
scale_x_date(breaks="2 days", minor_breaks="1 day", labels = date_format("%d\n%b")) + # Look at scales package for more alternatives
theme(axis.text.x = element_text(size=12, face="bold"),
axis.title.x = element_text(vjust=.2, face="bold", size="20"))
)
})
})