-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
139 lines (107 loc) · 3.62 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinythemes)
source('auxs.R') # We store the functions here to avoid clutter
# Config
regions <- regions_generator()
population_threshold <- 1e5
# Create the dataset
cities_db <- get_towns(regions, pop_threshold = population_threshold)
timezones <- get_timezones(cities_db)
offsets <- do.call(rbind, lapply(timezones, get_utc_offset))
cities_db <- cbind(cities_db, timezones, offsets)
text <- get_text() # Translate the site to the available languages (default = EN)
# Define UI
ui <- fluidPage(
theme = shinytheme("cerulean"),
# Application title
titlePanel(text$Title),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput('town_name', text$ChooseCity, cities_db$name),
helpText(text$ChooseTime),
radioButtons('time_scheme', text$DisplayTime,
choiceValues = c('Daylight saving time in summer', 'Winter time all year', 'Summer time all year') ,
choiceNames = c(text$WithDST, text$WithWinter, text$WithSummer))
),
# Show output
mainPanel(
plotOutput('lightPlot'),
h6(text$EarlierSunrise),
textOutput('earlier_sunrise'),
h6(text$EarlierSunset),
textOutput('earlier_sunset'),
h6(text$LatestSunrise),
textOutput('later_sunrise'),
h6(text$LatestSunset),
textOutput('later_sunset'),
h4('Pablo Rodríguez-Sánchez'),
tags$a(href="https://pabrod.github.io", "pabrod.github.io")
)
)
)
# Define server logic
server <- function(input, output) {
# Parameters
city_selected <- reactive({
city_selected <- filter(cities_db, name %in% input$town_name)
})
daylight_saving <- reactive({
identical(input$time_scheme, 'Daylight saving time in summer')
})
summer_time <- reactive({
identical(input$time_scheme, 'Summer time all year')
})
times <- reactive({
## Set position
city <- city_selected()
## Set conditions
daylight_saving <- daylight_saving()
summer_time <- summer_time()
case <- get_case(daylight_saving, summer_time, city)
## Get the sunrise and sunset times
times <- get_sunlight_times(city$lat, city$lon, case)
return(times)
})
output$earlier_sunrise <- reactive({
times <- times()
date <- filter(times, sunrise_decimal == min(sunrise_decimal))
earlier_sunrise <- format(date$sunrise[1], format = '%d-%B %H:%M:%S')
})
output$earlier_sunset <- reactive({
times <- times()
date <- filter(times, sunset_decimal == min(sunset_decimal))
earlier_sunset <- format(date$sunset[1], format = '%d-%B %H:%M:%S')
})
output$later_sunrise <- reactive({
times <- times()
date <- filter(times, sunrise_decimal == max(sunrise_decimal))
later_sunrise <- format(date$sunrise[1], format = '%d-%B %H:%M:%S')
})
output$later_sunset <- reactive({
times <- times()
date <- filter(times, sunset_decimal == max(sunset_decimal))
later_sunset <- format(date$sunset[1], format = '%d-%B %H:%M:%S')
})
output$lightPlot <- renderPlot({
times <- times()
## Set position
city <- city_selected()
## Set conditions
daylight_saving <- daylight_saving()
summer_time <- summer_time()
case <- get_case(daylight_saving, summer_time, city)
## Plot info
plot_result(times, text, case)
})
}
# Run the application
shinyApp(ui = ui, server = server)