-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
106 lines (78 loc) · 2.73 KB
/
app.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
library(shiny)
library(fit)
library(dplyr)
library(ggplot2)
library(leaflet)
library(sp)
library(mapview)
ui <- fluidPage(
titlePanel("Visualize FIT file"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose .fit File",
multiple = TRUE,
accept = c(".fit"))
),
mainPanel(
fluidRow(tableOutput("summary"), align="center"), ## header
leafletOutput("map"), ## map with line
plotOutput("figures"), ## speed vs distance
hr(),
tags$footer("Brian M. Lang 2020", align = "center")
)
)
)
# Define server logic to read selected file ----
server <- function(input, output) {
## user input file will be NULL to begin with, so use the example to start with, this will be shipped with the shiny app when uploaded to rstudio.io
fit_file <- reactive({
if (is.null(input$file1)) {
fit::read.fit("./data/example2.fit")
} else {
fit::read.fit(input$file1$datapath)
}
})
output$summary <- renderTable({
return(fit_file() %>%
.$session %>%
transmute(kilometers = total_distance/1000,
minutes = total_elapsed_time/60,
average_pace = (minutes)/kilometers,
total_calories))
})
#
# output$record <- renderTable({
# # input$file1 will be NULL initially. After the user selects
# # and uploads a file, head of that data file by default,
# # or all rows if selected, will be shown.
# req(input$file1)
#
# return(fit_file() %>% .$record %>% head())
#
# })
output$map <- renderLeaflet({
fitdata <- fit_file()
mp <- sp::SpatialPoints(coords = fitdata$record %>%
transmute(lng = position_long,
lat = position_lat) %>%
filter(!is.na(lat)))
mp1 <- mapview::coords2Lines(coords = sp::coordinates(mp), ID = "A")
return(leaflet::leaflet(mp1) %>%
leaflet::addTiles() %>%
leaflet::addPolylines(weight = 4, color = "black"))
})
output$figures <- renderPlot({
fitdata <- fit_file()
pdata <- fitdata$record %>%
filter(!is.na(position_long)) %>%
mutate(mpkm = 1000 / (speed * 60) )
pace_dist <- pdata %>%
ggplot(aes(distance/1000, mpkm)) +
geom_line() +
lims(y = c(0, 12)) +
theme_minimal() +
labs(y = "min/km", x = "distance (km)")
pace_dist
})
}
shinyApp(ui, server)