-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
117 lines (94 loc) · 2.47 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
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
#- server.R -#
server <- function(input, output, session) {
OPTS$weather_date_max = yesterday()
OPTS$climate_date_min = start_of_year()
OPTS$climate_date_max = end_of_year()
# Reactive values ----
rv <- reactiveValues(
# false if new weather data needs downloading
weather_ready = length(weather_dates()) == 0,
# true after a location has been selected
loc_ready = FALSE,
# true after map has loaded
map_ready = FALSE,
)
loc_data <- reactive({
loc <- req(mapServerValues()$selected_grid)
list(
loc = loc,
weather = weather %>%
filter(lat == loc$lat, lng == loc$lng),
c10 = climate$c10 %>%
filter(lat == loc$lat, lng == loc$lng) %>%
mutate(date = start_of_year() + yday - 1, .after = yday),
c5 = climate$c5 %>%
filter(lat == loc$lat, lng == loc$lng) %>%
mutate(date = start_of_year() + yday - 1, .after = yday)
)
})
# Initialize module servers ----
mapServerValues <- mapServer()
growthServer(reactive(loc_data()))
timingServer(reactive(loc_data()))
plotServer(reactive(loc_data()))
downloadsServer(
grid_data = reactive(mapServerValues()$grid_data),
loc_data = reactive(loc_data())
)
observe({
req(mapServerValues()$selected_grid)
rv$loc_ready <- TRUE
})
observe({
req(mapServerValues()$grid_data)
rv$map_ready <- TRUE
})
# Main UI ----
output$main_ui <- renderUI({
if (!rv$weather_ready) {
load_data()
rv$weather_ready <- TRUE
}
fluidRow(
column(6, h3("Weather and climate map"), mapUI()),
column(6, uiOutput("sidebar_ui"))
)
})
# Sidebar selector ----
sidebar_pages <- list(
map = list(
h3("Map display options"),
mapSidebarUI()
),
growth = list(
h3("Alfalfa growth projection"),
growthUI()
),
timing = list(
h3("Alfalfa cut planning tool"),
timingUI()
),
charts = list(
h3("Weather and climate charts"),
plotUI()
),
downloads = list(
h3("Data downloads"),
downloadsUI()
),
about = list(
h3("About this app"),
includeMarkdown("about.Rmd")
)
)
output$sidebar_ui <- renderUI({
if (!rv$map_ready) {
sidebar_pages[["map"]]
} else {
page <- req(input$navbar)
# scroll to sidebar element on mobile view
runjs("if (window.innerWidth < 768) document.getElementById('sidebar_ui').scrollIntoView();")
sidebar_pages[[page]]
}
})
}