Skip to content

Commit bdd3d47

Browse files
authored
Merge pull request #170 from terraref/shiny-app-edits
Shiny app edits
2 parents a4e635a + 2b95e8c commit bdd3d47

5 files changed

Lines changed: 138 additions & 62 deletions

File tree

experiment-trait-data-visualizer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ apt-get install cron
2929
#### Setup and Notes
3030

3131
* Data are cached in a file `cache.RData` and updated using the cronR R interface to cron
32-
* Access to BETYdb remote access requires ssh tunnel `ssh -Nf -L 5432:localhost:5432 bety6.ncsa.illinois.edu`
32+
* Access to BETYdb remote access requires ssh tunnel
33+
* `ssh -Nf -L 5432:localhost:5432 bety6.ncsa.illinois.edu`
3334
* requires access, which can be [requested here](https://identity.ncsa.illinois.edu/join/TU49BUUEDM)
3435
* Set the following environment variables
3536

experiment-trait-data-visualizer/app.R

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ source('render-site-map.R')
1111
# schedule daily execution of cache refresh
1212
cache_update_cmd <- cron_rscript('cache-refresh.R')
1313

14-
if(!grepl('cache-update', cronR::cron_ls())){
15-
cron_add(command = cache_update_cmd, frequency = 'daily',
16-
id = 'cache-update', description = 'daily update of BETYdb cache')
17-
}
18-
14+
cron_clear(ask = FALSE)
15+
cron_add(command = cache_update_cmd, frequency = 'daily',
16+
id = 'cache-update', description = 'daily update of BETYdb cache')
1917

2018
# set page UI
2119
ui <- fluidPage(theme = shinytheme('flatly'),
@@ -103,31 +101,40 @@ render_trait_plot <- function(season_name, input, output, full_cache_data) {
103101
plot_data <- selected_season_data[[ 'trait_data' ]][[ selected_variable ]][[ 'traits' ]]
104102
data_max <- max(plot_data[[ 'mean' ]])
105103

106-
title <- selected_variable
107104
units <- selected_season_data[[ 'trait_data' ]][[ selected_variable ]][[ 'units' ]]
108-
if (units != '')
109-
title <- paste0(selected_variable, ' (', units, ')')
110-
111-
ggplot(plot_data, aes(as.Date(date), mean)) +
112-
geom_boxplot(aes(group = cut_width(as.Date(date), 1)), outlier.alpha = 0.1) +
113-
114-
{
115-
if (selected_cultivar != 'None') {
116-
title <- paste0(title, ' for ', selected_cultivar)
117-
geom_line(data = subset(plot_data, cultivar_name == selected_cultivar),
118-
size = 0.5, color = '#00C49F', aes(x = as.Date(date), y = mean, group = site_id))
119-
}
120-
} +
105+
title <- ifelse(units == '', selected_variable, paste0(selected_variable, ' (', units, ')'))
121106

122-
labs(
123-
title = paste0(title, '\n'),
124-
x = "Observation Dates",
125-
y = units
126-
) +
107+
trait_plot <- ggplot() +
108+
geom_violin(data = plot_data, scale = 'width', width = 1,
109+
aes(x = as.Date(date), y = mean,
110+
group = as.Date(date))) +
111+
geom_boxplot(data = plot_data,
112+
aes(x = as.Date(date), y = mean,
113+
group = as.Date(date)),
114+
outlier.alpha = 0.25, width = 0.2)
115+
# geom_point(data = plot_data,
116+
# aes(x = as.Date(date), y = mean),
117+
# alpha = 0.1, size = 0.1, position = position_jitter(width = 0.1))
118+
119+
if (selected_cultivar != 'None') {
120+
title <- paste0(title, '\nCultivar ', selected_cultivar, ' in red')
121+
trait_plot <- trait_plot +
122+
geom_point(data = subset(plot_data, cultivar_name == selected_cultivar),
123+
color = 'red', aes(x = as.Date(date), y = mean, group = site_id)) +
124+
geom_line(data = subset(plot_data, cultivar_name == selected_cultivar),
125+
size = 0.25, color = 'red', alpha = 0.25, aes(x = as.Date(date), y = mean, group = site_id))
126+
}
127127

128-
theme(text = element_text(size = 20), axis.text.x = element_text(angle = 45, hjust = 1), legend.position = "none") +
129-
xlim(as.Date(selected_season_data[[ 'start_date' ]]), as.Date(selected_season_data[[ 'end_date' ]])) +
130-
ylim(0, data_max)
128+
trait_plot +
129+
labs(
130+
title = paste0(title, '\n'),
131+
x = "Date",
132+
y = units
133+
) +
134+
theme_bw() +
135+
theme(text = element_text(size = 20), axis.text.x = element_text(angle = 45, hjust = 1), legend.position = "none") +
136+
xlim(as.Date(selected_season_data[[ 'start_date' ]]), as.Date(selected_season_data[[ 'end_date' ]])) +
137+
ylim(0, data_max)
131138
})
132139
}
133140

@@ -192,8 +199,10 @@ render_timeline_hover <- function(season_name, input, output, full_cache_data) {
192199
selected_record <- management_data[ as.numeric(selected), ]
193200

194201
formatted_notes <- ''
195-
if (selected_record[[ 'notes' ]] != '')
202+
if (selected_record[[ 'notes' ]] != '') {
196203
formatted_notes <- paste0('<br><br>', selected_record[[ 'notes' ]])
204+
}
205+
197206

198207
wellPanel(class = 'mgmt-select-info',
199208
HTML(paste0(
@@ -228,12 +237,16 @@ render_map <- function(season_name, input, output, full_cache_data) {
228237

229238
traits <- full_cache_data[[ season_name ]][[ 'trait_data' ]][[ selected_variable ]][[ 'traits' ]]
230239

231-
if (selected_cultivar != 'None')
240+
if (selected_cultivar != 'None'){
232241
traits <- subset(traits, cultivar_name == selected_cultivar)
242+
}
243+
233244

234245
units <- full_cache_data[[ season_name ]][[ 'trait_data' ]][[ selected_variable ]][[ 'units' ]]
235-
if (units != '')
246+
if (units != '') {
236247
units <- paste0('(', units, ')')
248+
}
249+
237250
legend_title <- paste0(selected_variable, ' ', units)
238251

239252
render_site_map(traits, render_date, legend_title)
@@ -261,8 +274,10 @@ render_season_output <- function(season_name, input, output, full_cache_data) {
261274
server <- function(input, output) {
262275

263276
# load 'full_cache_data' object from cache file
264-
if (!file.exists('cache.RData'))
277+
if (!file.exists('cache.RData')){
265278
source('cache-refresh.R')
279+
}
280+
266281
load('cache.RData')
267282

268283
# render UI for all available seasons
@@ -275,4 +290,4 @@ server <- function(input, output) {
275290
lapply(names(full_cache_data), render_season_output, input, output, full_cache_data)
276291
}
277292

278-
shinyApp(ui = ui, server = server)
293+
shinyApp(ui = ui, server = server)

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

Lines changed: 26 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,52 @@ 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){
45+
return()
46+
}
4147
sites_table <- tbl(bety_src, sql("select ST_AsText(sites.geometry) AS geometry, id from sites")) %>%
4248
filter(!is.na(geometry)) %>%
4349
filter(id %in% site_ids) %>%
44-
mutate(site_id = id)
50+
rename(site_id = id)
4551

4652
cultivars_table <- tbl(bety_src, 'cultivars') %>%
47-
mutate(cultivar_id = id, cultivar_name = name) %>%
53+
rename(cultivar_id = id, cultivar_name = name) %>%
4854
select(cultivar_id, cultivar_name)
4955

5056
traits <- traits_table %>%
5157
left_join(sites_table, by = 'site_id') %>%
52-
left_join(cultivars_table, by = 'cultivar_id') %>%
53-
collect() %>% as.data.frame()
58+
left_join(cultivars_table, by = 'cultivar_id')
5459

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

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

6266
trait_data <- list()
63-
for (curr_variable_id in variable_ids) {
64-
67+
for (curr_variable_id in variable_ids$variable_id) {
68+
6569
variable_data <- list()
6670

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

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

74-
variable_data[[ 'units' ]] <- variable_record[[ 'units' ]]
75-
variable_data[[ 'id' ]] <- variable_record[[ 'id' ]]
77+
variable_data[[ 'units' ]] <- variable_record %>% select(units)
78+
variable_data[[ 'id' ]] <- variable_record %>% select(variable_id)
7679
variable_data[[ 'traits' ]] <- variable_traits
7780

7881
# subset trait data by variable
@@ -81,13 +84,9 @@ get_data_for_season <- function(season) {
8184
# save trait data for all variables
8285
season_data[[ 'trait_data' ]] <- trait_data
8386

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

9392
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
}

scripts/experimental_design.Rmd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Experimental Design
2+
3+
4+
```{r setup, echo=FALSE, message = FALSE}
5+
library(googlesheets)
6+
library(knitr)
7+
library(dplyr)
8+
9+
opts_knit$set(echo = FALSE, message = FALSE, cache = TRUE)
10+
11+
```
12+
## Accessions
13+
14+
```{r accessions, echo = FALSE, message = FALSE}
15+
ss <- gs_key("1Nfabx_n1rNlO6NW3olD8MAibJ3KHnOMmMwOYYw4wwGc")
16+
accessions <- gs_read(ss, ws = "Accessions")
17+
a <- accessions %>% mutate_each( funs_( lazyeval::interp( ~replace(., is.na(.), "") ) ) ) %>% select(Entry, Code, Source, Pedigree, Pedigree2)
18+
19+
a[1:193,] %>% kable
20+
21+
a[195:201,] %>% kable(caption = a[194,'Entry'])
22+
23+
a[204:252,] %>% kable(caption = a[203,'Entry'])
24+
25+
```
26+
27+
https://docs.google.com/spreadsheets/d/1Nfabx_n1rNlO6NW3olD8MAibJ3KHnOMmMwOYYw4wwGc/pubhtml?gid=239932660&amp;single=true
28+
29+
## Experiments
30+
31+
```{r experiments, echo = FALSE, message = FALSE}
32+
experiments <- gs_read(ss, ws = "Experiments")
33+
experiments %>% kable
34+
```
35+
36+
https://docs.google.com/spreadsheets/d/1Nfabx_n1rNlO6NW3olD8MAibJ3KHnOMmMwOYYw4wwGc/pubhtml?gid=890543376&amp;single=true
37+
38+
## 2016 Field Layout
39+
40+
### Under Gantry
41+
42+
```{r gantry-plot-layout, echo = FALSE, message = FALSE}
43+
gantry_plot_layout <- gs_read(ss, ws = "Gantry Plot Layout")
44+
gantry_plot_layout %>% kable
45+
```
46+
47+
https://docs.google.com/spreadsheets/d/1Nfabx_n1rNlO6NW3olD8MAibJ3KHnOMmMwOYYw4wwGc/pubhtml?gid=1231399646&amp;single=true
48+
49+
### West of Gantry
50+
51+
```{r west-of-gantry-plot-layout, echo = FALSE, message = FALSE}
52+
west_of_gantry_plot_layout <- gs_read(ss, ws = "West of Gantry Plot Layout")
53+
west_of_gantry_plot_layout %>% kable
54+
```
55+
56+
https://docs.google.com/spreadsheets/d/1Nfabx_n1rNlO6NW3olD8MAibJ3KHnOMmMwOYYw4wwGc/pubhtml?gid=728631369&amp;single=true
57+

0 commit comments

Comments
 (0)