Skip to content

Commit 753952b

Browse files
committed
- added hard-coded connection parameters that work with ssh tunnel to bety6
- refactored dplyr code to use lazy eval and semi-joins instead of filter - removed outline from plot colors - changed basemap to semi-useful default
1 parent 9f5ee97 commit 753952b

3 files changed

Lines changed: 41 additions & 35 deletions

File tree

experiment-trait-data-visualizer/app.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
107107
title <- ifelse(units == '', selected_variable, paste0(selected_variable, ' (', units, ')'))
108108

109109
trait_plot <- ggplot() +
110-
geom_violin(data = plot_data, scale = 'width', width = 0.75,
110+
geom_violin(data = plot_data, scale = 'width', width = 1,
111111
aes(x = as.Date(date), y = mean,
112112
group = as.Date(date))) +
113113
geom_boxplot(data = plot_data,
@@ -124,7 +124,7 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
124124
geom_point(data = subset(plot_data, cultivar_name == selected_cultivar),
125125
color = 'red', aes(x = as.Date(date), y = mean, group = site_id)) +
126126
geom_line(data = subset(plot_data, cultivar_name == selected_cultivar),
127-
size = 0.5, color = 'red', aes(x = as.Date(date), y = mean, group = site_id))
127+
size = 0.25, color = 'red', alpha = 0.25, aes(x = as.Date(date), y = mean, group = site_id))
128128
}
129129

130130
trait_plot +
@@ -133,7 +133,6 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
133133
x = "Date",
134134
y = units
135135
) +
136-
137136
theme_bw() +
138137
theme(text = element_text(size = 20), axis.text.x = element_text(angle = 45, hjust = 1), legend.position = "none") +
139138
xlim(as.Date(selected_season_data[[ 'start_date' ]]), as.Date(selected_season_data[[ 'end_date' ]])) +
@@ -202,8 +201,10 @@ render_timeline_hover <- function(season_name, input, output, full_cache_data) {
202201
selected_record <- management_data[ as.numeric(selected), ]
203202

204203
formatted_notes <- ''
205-
if (selected_record[[ 'notes' ]] != '')
204+
if (selected_record[[ 'notes' ]] != '') {
206205
formatted_notes <- paste0('<br><br>', selected_record[[ 'notes' ]])
206+
}
207+
207208

208209
wellPanel(class = 'mgmt-select-info',
209210
HTML(paste0(
@@ -238,12 +239,16 @@ render_map <- function(season_name, input, output, full_cache_data) {
238239

239240
traits <- full_cache_data[[ season_name ]][[ 'trait_data' ]][[ selected_variable ]][[ 'traits' ]]
240241

241-
if (selected_cultivar != 'None')
242+
if (selected_cultivar != 'None'){
242243
traits <- subset(traits, cultivar_name == selected_cultivar)
244+
}
245+
243246

244247
units <- full_cache_data[[ season_name ]][[ 'trait_data' ]][[ selected_variable ]][[ 'units' ]]
245-
if (units != '')
248+
if (units != '') {
246249
units <- paste0('(', units, ')')
250+
}
251+
247252
legend_title <- paste0(selected_variable, ' ', units)
248253

249254
render_site_map(traits, render_date, legend_title)

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ options(scipen=999)
1313
# )
1414
bety_src <- src_postgres(dbname = "bety",
1515
password = 'bety',
16-
host = 'terra-bety.default',
16+
#host = 'terra-bety.default',
17+
host = 'localhost',
1718
user = 'bety',
1819
port = 5432)
1920

2021
# get all relevant data from BETYdb for a given season, write to cache file
2122
get_data_for_season <- function(season) {
22-
23+
2324
# destination for all data for given season
2425
# save season start date and end date
2526
season_data <- list(start_date = season[[ 'start_date' ]], end_date = season[[ 'end_date' ]])
@@ -29,50 +30,50 @@ get_data_for_season <- function(season) {
2930
select(site_id) %>%
3031
collect() %>% unlist(use.names = FALSE)
3132

32-
if (is.null(site_ids))
33+
if (is.null(site_ids)){
3334
return()
35+
}
3436

3537
# only use trait records associated with the relevant sites
36-
traits_table <- tbl(bety_src, 'traits') %>%
38+
traits_table <- tbl(bety_src, 'traits', n = Inf) %>%
3739
filter(date >= season[[ 'start_date' ]] & date <= season[[ 'end_date' ]]) %>%
3840
filter(site_id %in% site_ids) %>%
3941
select(date, mean, variable_id, cultivar_id, treatment_id, site_id)
4042

43+
n_traits <- traits_table %>% summarize(n = n()) %>% collect(n = Inf)
44+
if(n_traits == 0) return()
4145
sites_table <- tbl(bety_src, sql("select ST_AsText(sites.geometry) AS geometry, id from sites")) %>%
4246
filter(!is.na(geometry)) %>%
4347
filter(id %in% site_ids) %>%
44-
mutate(site_id = id)
48+
rename(site_id = id)
4549

4650
cultivars_table <- tbl(bety_src, 'cultivars') %>%
47-
mutate(cultivar_id = id, cultivar_name = name) %>%
51+
rename(cultivar_id = id, cultivar_name = name) %>%
4852
select(cultivar_id, cultivar_name)
4953

5054
traits <- traits_table %>%
5155
left_join(sites_table, by = 'site_id') %>%
52-
left_join(cultivars_table, by = 'cultivar_id') %>%
53-
collect() %>% as.data.frame()
56+
left_join(cultivars_table, by = 'cultivar_id')
5457

55-
if (nrow(traits) == 0)
56-
return()
58+
variables <- tbl(bety_src, 'variables') %>%
59+
rename(variable_id = id) %>%
60+
semi_join(traits, by = 'variable_id')
5761

58-
variable_ids <- unique(na.omit(
59-
traits[[ 'variable_id' ]]
60-
))
62+
variable_ids <- variables %>% select(variable_id) %>% collect
6163

6264
trait_data <- list()
63-
for (curr_variable_id in variable_ids) {
64-
65+
for (curr_variable_id in variable_ids$variable_id) {
66+
6567
variable_data <- list()
6668

67-
variable_record <- tbl(bety_src, 'variables') %>%
68-
filter(id == curr_variable_id) %>%
69-
select(id, name, units) %>% collect()
69+
variable_record <- variables %>% filter(variable_id == curr_variable_id) %>%
70+
select(variable_id, name, units) %>% collect
7071

71-
variable_name <- toTitleCase(gsub('_', ' ', variable_record[[ 'name' ]]))
72-
variable_traits <- subset(traits, variable_id == curr_variable_id)
72+
variable_name <- tools::toTitleCase(gsub('_', ' ', variable_record %>% select(name) %>% collect))
73+
variable_traits <- traits %>% filter(variable_id == curr_variable_id) %>% collect(n = Inf)
7374

74-
variable_data[[ 'units' ]] <- variable_record[[ 'units' ]]
75-
variable_data[[ 'id' ]] <- variable_record[[ 'id' ]]
75+
variable_data[[ 'units' ]] <- variable_record %>% select(units)
76+
variable_data[[ 'id' ]] <- variable_record %>% select(variable_id)
7677
variable_data[[ 'traits' ]] <- variable_traits
7778

7879
# subset trait data by variable
@@ -81,13 +82,9 @@ get_data_for_season <- function(season) {
8182
# save trait data for all variables
8283
season_data[[ 'trait_data' ]] <- trait_data
8384

84-
treatment_ids <- unique(na.omit(
85-
traits[[ 'treatment_id' ]]
86-
))
87-
8885
# only use management records associated with the relevant treatments
8986
management_ids <- tbl(bety_src, 'managements_treatments') %>%
90-
filter(treatment_id %in% treatment_ids) %>%
87+
semi_join(traits, by = 'treatment_id') %>%
9188
collect() %>% unlist(use.names = FALSE)
9289

9390
managements <- tbl(bety_src, 'managements') %>%

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ render_site_map <- function(traits, render_date, legend_title) {
1313
domain = traits[[ 'mean' ]]
1414
)
1515

16-
map <- leaflet(options = leafletOptions(minZoom = 20, maxZoom = 21)) %>% addTiles()
16+
map <- leaflet(options = leafletOptions(minZoom = 18, maxZoom = 21)) %>%
17+
addProviderTiles(providers$Esri.WorldImagery) # eventually want to overlay with stitched image from current day
1718

1819
# add polygon for each site, color by trait mean value
1920
if (nrow(latest_traits) > 0) {
@@ -24,7 +25,10 @@ render_site_map <- function(traits, render_date, legend_title) {
2425

2526
if ('polygons' %in% names(attributes(site_poly))) {
2627
trait_value <- curr_trait[[ 'mean' ]]
27-
map <- addPolygons(map, data = site_poly, color = pal(trait_value))
28+
map <- addPolygons(map, data = site_poly,
29+
color = pal(trait_value), opacity = 0,
30+
fillColor = pal(trait_value), fillOpacity = 0.8)
31+
#TODO add popup = plot name + trait value for plot
2832
}
2933
}
3034
}

0 commit comments

Comments
 (0)