Skip to content

Commit a56716e

Browse files
author
Nick Heyek
committed
Add comments
1 parent 5cc901a commit a56716e

3 files changed

Lines changed: 58 additions & 34 deletions

File tree

experiment-trait-data-visualizer/app.R

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ library(shinythemes)
99

1010
source('render-site-map.R')
1111

12-
# set up scheduled execution of cache update
13-
#cache_update_cmd <- cron_rscript('cache-refresh.R')
14-
#try(cron_add(command = cache_update_cmd, frequency = 'daily',
15-
# id = 'cache-update', description = 'daily update of BETYdb cache'))
12+
# schedule daily execution of cache refresh
13+
cache_update_cmd <- cron_rscript('cache-refresh.R')
14+
try(cron_add(command = cache_update_cmd, frequency = 'daily',
15+
id = 'cache-update', description = 'daily update of BETYdb cache'))
1616

1717
# set page UI
18-
ui <- fluidPage( theme = shinytheme('flatly'),
18+
ui <- fluidPage(theme = shinytheme('flatly'),
1919

2020
tags$link(rel = 'stylesheet', type = 'text/css', href = 'style.css'),
2121
title = 'TERRA-REF Experiment Data',
2222

2323
tags$img(src = 'logo.png', class = 'push-out'),
2424

25+
# destination for all dynamic UI elements
2526
uiOutput('page_content')
2627
)
2728

29+
# render UI for a given season
2830
render_season_ui <- function(season_name) {
2931

3032
tabPanel(season_name,
31-
33+
3234
sidebarPanel(class = 'push-down',
3335
uiOutput(paste0('variable_select_', season_name)),
3436
uiOutput(paste0('cultivar_select_', season_name))
@@ -58,6 +60,7 @@ render_season_ui <- function(season_name) {
5860
)
5961
}
6062

63+
# render selection menu from available variables in a given season
6164
render_variable_menu <- function(season_name, output, full_cache_data) {
6265

6366
variable_names <- names(full_cache_data[[ season_name ]][[ 'trait_data' ]])
@@ -67,6 +70,7 @@ render_variable_menu <- function(season_name, output, full_cache_data) {
6770
})
6871
}
6972

73+
# render selection menu from available cultivars in a given season, for the selected variable
7074
render_cultivar_menu <- function(season_name, input, output, full_cache_data) {
7175

7276
output[[ paste0('cultivar_select_', season_name) ]] <- renderUI({
@@ -80,6 +84,8 @@ render_cultivar_menu <- function(season_name, input, output, full_cache_data) {
8084
})
8185
}
8286

87+
# render box plot time series from trait records in a given season, for the selected variable
88+
# if a cultivar is selected, render line plot from trait records for that cultivar
8389
render_trait_plot <- function(season_name, input, output, full_cache_data) {
8490

8591
output[[ paste0('trait_plot_', season_name) ]] <- renderPlot({
@@ -99,7 +105,6 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
99105
if (units != '')
100106
title <- paste0(selected_variable, ' (', units, ')')
101107

102-
# generate timeseries of boxplots from mean value
103108
ggplot(plot_data, aes(as.Date(date), mean)) +
104109
geom_boxplot(aes(group = cut_width(as.Date(date), 1)), outlier.alpha = 0.1) +
105110

@@ -123,6 +128,7 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
123128
})
124129
}
125130

131+
# render timeline from management records in a given season
126132
render_mgmt_timeline <- function(season_name, input, output, full_cache_data) {
127133

128134
output[[ paste0('mgmt_timeline_', season_name) ]] <- renderTimevis({
@@ -148,6 +154,7 @@ render_mgmt_timeline <- function(season_name, input, output, full_cache_data) {
148154
})
149155
}
150156

157+
# render info box for date and value of cursor when hovering box/line plot
151158
render_plot_hover <- function(season_name, input, output, full_cache_data) {
152159

153160
output[[ paste0('plot_hover_info_', season_name) ]] <- renderUI({
@@ -170,6 +177,7 @@ render_plot_hover <- function(season_name, input, output, full_cache_data) {
170177
})
171178
}
172179

180+
# render info box for date, type, and notes of selected (clicked) timeline item
173181
render_timeline_hover <- function(season_name, input, output, full_cache_data) {
174182

175183
output[[ paste0('mgmt_select_info_', season_name) ]] <- renderUI({
@@ -196,13 +204,15 @@ render_timeline_hover <- function(season_name, input, output, full_cache_data) {
196204

197205
render_map <- function(season_name, input, output, full_cache_data) {
198206

207+
# render slider input from dates in a given season
199208
output[[ paste0('map_date_slider_', season_name) ]] <- renderUI({
200209
sliderInput(paste0('map_date_', season_name), 'Date',
201210
as.Date(full_cache_data[[ season_name ]][[ 'start_date']]),
202211
as.Date(full_cache_data[[ season_name ]][[ 'end_date' ]]),
203212
as.Date(full_cache_data[[ season_name ]][[ 'end_date' ]]))
204213
})
205214

215+
# render heat map of sites from trait records in a given season, for the selected date, variable and cultivar
206216
output[[ paste0('site_map_', season_name) ]] <- renderLeaflet({
207217

208218
req(input[[ paste0('selected_variable_', season_name) ]])
@@ -227,7 +237,7 @@ render_map <- function(season_name, input, output, full_cache_data) {
227237
})
228238
}
229239

230-
240+
# render outputs for a given season
231241
render_season_output <- function(season_name, input, output, full_cache_data) {
232242

233243
render_variable_menu(season_name, output, full_cache_data)
@@ -243,20 +253,20 @@ render_season_output <- function(season_name, input, output, full_cache_data) {
243253
render_timeline_hover(season_name, input, output, full_cache_data)
244254

245255
render_map(season_name, input, output, full_cache_data)
246-
247256
}
248257

249-
# render page elements
250258
server <- function(input, output) {
251259

260+
# load 'full_cache_data' object from cache file
252261
load('cache.RData')
253262

263+
# render UI for all available seasons
254264
output$page_content <- renderUI({
255-
256265
season_tabs <- lapply(names(full_cache_data), render_season_ui)
257266
do.call(tabsetPanel, season_tabs)
258267
})
259268

269+
# render outputs for all available seasons
260270
lapply(names(full_cache_data), render_season_output, input, output, full_cache_data)
261271
}
262272

experiment-trait-data-visualizer/cache-refresh.R

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@ library(tools)
33
library(lubridate)
44
options(scipen=999)
55

6-
get_data_for_season <- function(season, bety_src) {
6+
# set up remote connection to BETYdb
7+
bety_src <- src_postgres(
8+
dbname = Sys.getenv('bety_dbname'),
9+
password = Sys.getenv('bety_password'),
10+
host = Sys.getenv('bety_host'),
11+
port = Sys.getenv('bety_port'),
12+
user = Sys.getenv('bety_user')
13+
)
14+
15+
# get all relevant data from BETYdb for a given season, write to cache file
16+
get_data_for_season <- function(season) {
717

18+
# destination for all data for given season
19+
# save season start date and end date
820
season_data <- list(start_date = season[[ 'start_date' ]], end_date = season[[ 'end_date' ]])
921

1022
season_site_ids <- tbl(bety_src, 'experiments_sites') %>%
@@ -15,8 +27,7 @@ get_data_for_season <- function(season, bety_src) {
1527
if (is.null(season_site_ids))
1628
return()
1729

18-
season_data[[ 'site_ids' ]] <- season_site_ids
19-
30+
# only use trait records associated with the relevant sites
2031
season_traits_table <- tbl(bety_src, 'traits') %>%
2132
filter(date >= season[[ 'start_date' ]] & date <= season[[ 'end_date' ]]) %>%
2233
filter(site_id %in% season_site_ids) %>%
@@ -52,14 +63,17 @@ get_data_for_season <- function(season, bety_src) {
5263
variable_data[[ 'id' ]] <- variable_record[[ 'id' ]]
5364
variable_data[[ 'traits' ]] <- variable_traits
5465

66+
# subset trait data by variable
5567
season_trait_data[[ variable_name ]] <- variable_data
5668
}
69+
# save trait data for all variables
5770
season_data[[ 'trait_data' ]] <- season_trait_data
5871

5972
season_treatment_ids <- unique(na.omit(
6073
season_traits[[ 'treatment_id' ]]
6174
))
6275

76+
# only use management records associated with the relevant treatments
6377
season_management_ids <- tbl(bety_src, 'managements_treatments') %>%
6478
filter(treatment_id %in% season_treatment_ids) %>%
6579
collect() %>% unlist(use.names = FALSE)
@@ -70,24 +84,20 @@ get_data_for_season <- function(season, bety_src) {
7084
select(id, date, mgmttype, notes) %>%
7185
collect()
7286

87+
# save management data
7388
season_data[[ 'managements' ]] <- season_managements
7489

90+
# load existing full_cache_data object if exists, otherwise use empty list object
7591
full_cache_data <- list()
7692
if (file.exists("cache.RData"))
7793
load("cache.RData")
78-
full_cache_data[[ toString(season[[ 'name' ]]) ]] <- season_data
7994

95+
# save data for given season
96+
full_cache_data[[ toString(season[[ 'name' ]]) ]] <- season_data
8097
save(full_cache_data, file = "cache.RData", compress = FALSE)
8198
}
8299

83-
bety_src <- src_postgres(
84-
dbname = Sys.getenv('bety_dbname'),
85-
password = Sys.getenv('bety_password'),
86-
host = Sys.getenv('bety_host'),
87-
port = Sys.getenv('bety_port'),
88-
user = Sys.getenv('bety_user')
89-
)
90-
100+
# get all experiments in BETYdb
91101
experiments <- tbl(bety_src, 'experiments') %>%
92102
select(id, name, start_date, end_date) %>%
93103
collect() %>% as.data.frame()
@@ -100,4 +110,5 @@ seasons <- experiments[
100110

101111
seasons[[ 'name' ]] <- gsub(":.*$","", seasons[[ 'name' ]])
102112

103-
apply(seasons, 1, get_data_for_season, bety_src)
113+
# get data for all seasons
114+
apply(seasons, 1, get_data_for_season)

experiment-trait-data-visualizer/render-site-map.R

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library(rgeos)
22
library(dplyr)
33

4+
# set up remote connection to BETYdb
45
bety_src <- src_postgres(
56
dbname = Sys.getenv('bety_dbname'),
67
password = Sys.getenv('bety_password'),
@@ -9,14 +10,17 @@ bety_src <- src_postgres(
910
user = Sys.getenv('bety_user')
1011
)
1112

13+
# render leaflet map from traits for a given date
1214
render_site_map <- function(traits, legend_title, render_date) {
1315

16+
# get associated sites
1417
site_ids <- na.omit(unique(traits[[ 'site_id' ]]))
1518
sites <- tbl(bety_src, sql("select ST_AsText(sites.geometry) AS geometry, id from sites")) %>%
1619
filter(!is.na(geometry)) %>%
1720
filter(id %in% site_ids) %>%
1821
collect() %>% data.frame()
1922

23+
# get most recent traits for each site
2024
latest_traits <- subset(traits, date <= render_date) %>% group_by(site_id) %>% top_n(1, date)
2125

2226
pal <- colorNumeric(
@@ -25,17 +29,16 @@ render_site_map <- function(traits, legend_title, render_date) {
2529
)
2630

2731
map <- leaflet(options = leafletOptions(minZoom = 20, maxZoom = 21)) %>% addTiles()
28-
29-
if (nrow(sites) > 0) {
30-
for (i in 1:nrow(sites)){
31-
site <- sites[i,]
32+
33+
# add polygon for each site, color by trait mean value
34+
for (i in 1:nrow(sites)){
35+
site <- sites[i,]
36+
37+
geo_object <- readWKT(site[['geometry']])
38+
if ('polygons' %in% names(attributes(geo_object))) {
3239

33-
geo_object <- readWKT(site[['geometry']])
34-
if ('polygons' %in% names(attributes(geo_object))) {
35-
36-
trait <- subset(latest_traits, site_id == site[[ 'id' ]])
37-
map <- addPolygons(map, data = geo_object, color = pal(trait[['mean']]))
38-
}
40+
trait <- subset(latest_traits, site_id == site[[ 'id' ]])
41+
map <- addPolygons(map, data = geo_object, color = pal(trait[['mean']]))
3942
}
4043
}
4144

0 commit comments

Comments
 (0)