-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
368 lines (308 loc) · 11.6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# app.R
# INIT -----------------
## LIBRARIES -----------
# Load required libraries
library(brapi)
library(tidyverse)
library(shiny)
library(bs4Dash)
library(DT)
library(rjson)
library(reshape2)
library(AGHmatrix)
library(heatmaply)
library(shinyWidgets)
library(data.table)
library(writexl)
library(tis)
library(fresh)
library(networkD3)
library(visNetwork)
# Include necessary JavaScript libraries
tags$head(
tags$script(src = "https://d3js.org/d3.v5.min.js"),
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/networkD3/0.4.1/networkD3.min.js")
)
## CUSTOM DATA and FUNCTION LOAD -----------
# Source custom functions and configurations from separate files
source("app_functions.R")
source("app_configs.R")
source("modules/flowering.R")
source("modules/pedigree.R")
source("modules/performance.R")
source("modules/crosses.R")
source("modules/download_page.R")
## THEME
#TBA
## CHECK if true connection
brapi::ba_check(brap) # should be true, for debugging
# USER INTERFACE -------------------------------------------------------------
ui <- dashboardPage(
title = "STC",
## CONTROLBAR ----
controlbar = dashboardControlbar(
collapsed = TRUE,
div(class = "p-3", skinSelector()),
pinned = FALSE
),
## HEADER --------
header = dashboardHeader(title = "SCT"),
## SIDEBAR ------
sidebar = dashboardSidebar(
selectInput("location", "Select Location:", choices = location_iid_map),
#this is kind of confusing. The idea is that multiple breeders might be working at same location (Florida) and they should be able to track crosses independently, even though cane lines are combined
#so crossesid refers to crosses a specific breeder is making
selectInput("crossesid", "Select Breeder", choices=crosses_iid_map),
dateInput(
"date",
"Choose A Date:",
value = "2023-10-10"
),
p("for testing, select:", strong("October 10, 2023")),
actionButton(
"brapipull",
"Get Flower Inventory Data"
),
p("Don't forget to push 'Get Flower Inventory Data'", strong("each"), "each time you choose a new date"),
sidebarMenu(
menuItem("Home",
tabName = "home",
icon = icon("home")
),
menuItem("Flowering Inventory",
tabName = "flowering",
icon = icon("seedling")
),
menuItem("Kinship/Pedigree",
tabName = "kinship",
icon = icon("people-group")
),
menuItem("Clone Performance",
tabName = "performance",
icon = icon("star")
),
menuItem("Previous Crosses",
tabName = "crosses",
icon = icon("xmark")
),
menuItem("Download Data",
tabName = "download",
icon = icon("download")
)
)
),
## BODY -----
body = dashboardBody(
tabItems(
### Home content ----
tabItem(
tabName = "home",
h1("Sugarcane Integrated Breeding System (SIBS) Sugarcane Crossing Tool (SCT)"),
p("Welcome SCT! Click", a(href="https://github.com/USDA-ARS-GBRU/SugarcaneCrossingTool", "here"), "for instructions."),
h1("Login information"),
p("You've logged in to view inventory for this location: "),
textOutput("inventoryPointer"),
br(),
p("You've logged in as:"),
textOutput("crossPointer")
),
### Flowering tab content -----
tabItem(
tabName = "flowering",
fluidRow(
box(p("This table shows you the count and sex of each clone that is flowering on the day you selected.")),
textOutput("dataSourceText"),
DTOutput("inventoryTable")
)
),
### Pedigree tab content ----
tabItem(
tabName = "kinship",
tabsetPanel(
type = "tabs",
tabPanel(
"Pedigree Table",
fluidRow(
box(
actionButton(
inputId = "makepedigree",
label = "Get Pedigree Data"
),
p("This table shows you the pedigree of each clone that is flowering on the date you selected,
as well as the number of progeny it produced and its relatedness (0-1+) to LCP85-384.")
),
DTOutput("pedigreeTable"),
)
),
tabPanel(
"Relationship Matrix",
box(p("This is a relationship matrix of the clones that are flowering on the date you selected. Values closer to one indicate high relatedness. You can zoom in to particular regions of the matrix.")),
plotlyOutput("pedigreeMatrix")
),
tabPanel(
"Visualize Pedigrees",
fluidRow(
box(
uiOutput("cloneDropdown"),
visNetworkOutput("pedigreeGraph")
)
)
)
)
),
### Performance tab content ----
tabItem(
tabName = "performance",
tabsetPanel(
type = "tabs",
tabPanel(
"Performance Table",
fluidRow(
box(
actionButton(
inputId = "makeperformance",
label = "Get Performance Data"
),
p("This table shows the mean and sd of the performance for each clone
that is flowering on the date you selected. Once data has been pulled from the database, you will be able to select traits to view using the drop-down menu. This step may take several minutes, please be patient."),
# stuff for what phenotype to select
uiOutput(outputId = "colSelect"), # render html list output
actionButton("selectCol", "View Selected Data")
)
),
DTOutput("performanceTable")
),
tabPanel(
"Trait Scatter Plot",
fluidRow(
box(
uiOutput("scatterPlotDropdown_x"),
uiOutput("scatterPlotDropdown_y"),
plotlyOutput("traitScatterPlot")
)
)
)
)
),
### Crosses tab content ----
tabItem(
tabName = "crosses",
fluidRow(
box(
actionButton(
inputId = "makecrosses",
label = "Get Cross Data"
),
p("This table shows a count of crosses that have been made with the clones that are flowering on the
date you selected as well as the summed number of progeny produced from those crosses. If the cross was made earlier this year, the 'Progeny.Per.Cross' column will read 'None yet, new cross this year'.")
)
),
DTOutput("crossesTable")
),
#### Download tab content ----
tabItem(
tabName = "download",
fluidRow(
box(p("This button will allow you to download a full data report as an excel file.
A partial download will fail, so make sure you've pulled all the inventory, pedigree, performance and cross data.
A successful download will have a data in the file name."),
downloadButton("downloaddata", "Download Data"))
)
)
)
)
)
# Define the inventory_init function as a global variable
inventory_init <<- eventReactive(input$brapipull, withProgress(message = "Pulling Inventory Data", {
tryCatch({
inven <- data.frame(brapi::ba_studies_table(con = brap, studyDbId = reactive_iid(), rclass="data.frame")) %>%
filter(observationLevel == "plant") %>% # select just plant rows
set_names(~(.)%>% str_replace_all("SUGARCANE.*","") %>% str_replace_all("\\.","")) %>% # take CO term out of colnames
filter(FloweringTime== reactive_date()) %>%
select(germplasmName, germplasmDbId, SexMFWM) %>%
group_by(germplasmName, germplasmDbId, SexMFWM) %>%
summarise(count = n()) %>%
rename(Clone = germplasmName, FloweringCount = count, Sex = SexMFWM)
dataSource("Data pulled from BrAPI")
inven
}, error = function(e) {
dataSource("Saved data is being rendered")
data.frame(Clone = character(), FloweringCount = numeric(), Sex = character()) # Return an empty data frame with the expected columns
})
}))
# SERVER ---------------------------------------
server <- function(input, output, session) {
library(networkD3)
# Reactive value for selected date
reactive_date <- reactive({
input$date
})
# Reactive value for data source
dataSource <- reactiveVal()
# Reactive values for selected columns in performance tab
rv <- reactiveValues(selectedColumns = NULL)
rv_trait_scatter <- reactiveValues(selectedColumns = NULL)
# Reactive value for selected clone
selectedClone <- reactiveVal()
selected_clone <- reactive({
req(input$selectedClone)
input$selectedClone
})
# Update selectedColumns when the user selects new columns in the Performance tab
observeEvent(input$selectCol, {
rv$selectedColumns <- input$phenoPick
rv_trait_scatter$selectedColumns <- input$phenoPick
})
# Update selected clone when user selects a clone in pedigree tab
observeEvent(input$selectedClone, {
selectedClone <- input$selectedClone
updateSelectInput(session, "pedigreeGraphUI")
})
# Update X-axis and Y-axis dropdowns in performance scatter plot based on selected phenotypes
observe({
phenotypes <- input$phenoPick
# Update X-axis dropdown
updateSelectInput(session, "xAxis_scatter", choices = phenotypes, selected = phenotypes[1])
# Update Y-axis dropdown
updateSelectInput(session, "yAxis_scatter", choices = phenotypes, selected = phenotypes[2])
})
# Update selected columns for scatter plot when user selects new columns
observeEvent(input$selectCol_scatter, {
rv_trait_scatter$xAxis <- input$xAxis_scatter
rv_trait_scatter$yAxis <- input$yAxis_scatter
})
observeEvent(input$selectCol_scatter, {
rv_trait_scatter$selectedColumns <- input$phenoPick_scatter
})
# Reactive value for selected location
reactive_iid <- reactive({
as.character(input$location)
})
# Reactive value for selected cross ID
reactive_cid <- reactive({as.character(input$crossesid)})
# Add the renderText for dataSourceText
output$dataSourceText <- renderText({
dataSource()
})
# Call the server functions from separate files
inventory_init <- flowering_server(input, output, session, reactive_date, reactive_iid, dataSource)
pedigree_server(input, output, session, reactive_iid, selectedClone, inventory_init)
performance_server(input, output, session, reactive_iid, rv, rv_trait_scatter, inventory_init)
crosses_server(input, output, session, reactive_cid, inventory_init)
download_page_server(input, output, session, reactive_date)
# Output for inventory pointer
output$inventoryPointer <- renderText({
location <- names(location_iid_map)[location_iid_map == input$location]
paste("Location:", location, "-", unique(brapi::ba_studies_table(con = brap, studyDbId = input$location)$studyName))
})
# Output for cross pointer
output$crossPointer <- renderText({
validate(
need(input$crossesid != "", "Please chose a breeder login:")
)
crosses <- names(crosses_iid_map)[crosses_iid_map == input$crossesid]
paste("Breeder:", crosses, "-", unique(ba_crosses_study(con = brap2, crossingProjectDbId = input$crossesid, rclass = "data.frame")$data.crossingProjectName[[1]])) #crossing project name has a breedbase bug- should return text, not number
})
}
# Run the Shiny app
shinyApp(ui, server)