-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
56 lines (46 loc) · 1.62 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
library(shiny)
library(jsonlite)
library(lubridate)
library(dplyr)
# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {
getWeightData <- function(){
#get weight data from phant:
phantUrl <- 'http://data.sparkfun.com/output/mKEvnrmWD0iKXbbN76r3.json'
weightTable <- fromJSON(phantUrl)
weightTable <- weightTable %>%
arrange(desc(timestamp)) %>%
mutate(timestamp = suppressMessages(ymd_hms(weightTable$timestamp, tz = "Pacific/Auckland")))
weightTable
}
lastWeightData <- getWeightData
getWeightDataSafely <- function(){
#wraps 'getWeightData', returns previous value if an error is caught
tryCatch(
{
lastWeightData <- getWeightData()
lastWeightData
},
error = function(e) {
cat("caught an error")
return(lastWeightData)}
)
}
currentWeightData <- reactivePoll(10000, session, checkFunc = getWeightDataSafely, valueFunc = getWeightDataSafely)
output$weightPlot <- renderPlotly({
plot_ly(data = currentWeightData() , x = timestamp, y = weight, line = list(shape = "linear"), mode = "lines+markers"
) %>% layout(
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
})
output$currentWeightSummary <- renderUI({
mostRecentWeightReading <-
currentWeightData() %>%
filter(row_number() == n())
currentWeight <- mostRecentWeightReading$weight
summaryHtml <- paste("WEIGHT", strong(currentWeight), " kg (0% fat) BMI ", strong("23.56"))
cat(summaryHtml)
summaryHtml
})
})