@@ -4,21 +4,25 @@ library(ggplot2)
44library(lubridate )
55library(DataCache )
66
7+ # set options for BETYdb API
78knitr :: opts_chunk $ set(echo = FALSE , cache = TRUE )
8-
99options(betydb_key = readLines(' ~/.betykey' , warn = FALSE ),
1010 betydb_url = " https://terraref.ncsa.illinois.edu/bety/" ,
1111 betydb_api_version = ' beta' )
1212
13+ # get list of seasons for user to select from
1314experiments <- as.data.frame(betydb_query(table = ' experiments' ))
1415seasons <- unique(experiments [c(' start_date' , ' end_date' )])
1516rownames(seasons ) <- paste0(' [' , seasons $ start_date , ' ]' , ' - ' , ' [' , seasons $ end_date , ' ]' )
1617
18+ # set page UI
1719ui <- fluidPage(
1820 titlePanel(" BETYdb Trait Data" ),
1921 sidebarLayout (
2022 sidebarPanel(
23+ # season menu
2124 selectInput(' selectedSeason' , ' Season' , rownames(seasons )),
25+ # variable menu to be rendered when variables for a given season are parsed in server()
2226 uiOutput(' selectVariable' )
2327 ),
2428 mainPanel(
@@ -27,37 +31,56 @@ ui <- fluidPage(
2731 )
2832)
2933
30- # load traits function used for cache functionality
34+ # load trait data from BETYdb
35+ # function is used only by DataCache library to get and update data
3136loadTraitData <- function (startDate , endDate ) {
3237
33- fullTraitData <- data.frame ()
34-
35- currDate <- startDate
36- while (endDate - currDate != 0 ) {
37- currTraitData <- betydb_query(table = ' traits' , date = paste0(' ~' , currDate ))
38- fullTraitData <- rbind(fullTraitData , currTraitData )
39- currDate <- currDate + days(1 )
40- }
38+ # set progress bar while API is queried
39+ withProgress(message = " Retrieving BETYdb Data" , value = 0 , {
40+
41+ fullTraitData <- data.frame ()
42+
43+ initialDateDiff <- as.numeric(difftime(endDate , startDate , units = " days" ))
44+ currDate <- startDate
45+ # loop through all days in a given season
46+ while (endDate - currDate != 0 ) {
47+ # get trait data for each day
48+ currTraitData <- betydb_query(table = ' traits' , date = paste0(' ~' , currDate ), limit = ' 5' )
49+ fullTraitData <- rbind(fullTraitData , currTraitData )
50+ currDate <- currDate + days(1 )
51+
52+ # update progress bar
53+ incProgress(1 / initialDateDiff )
54+ }
4155
42- retData <- list (fullTraitData )
43- names(retData ) <- ' fullTraitData'
56+ # format data as for usability with DataCache library
57+ retData <- list (fullTraitData )
58+ names(retData ) <- ' fullTraitData'
4459
45- return (retData )
60+ return (retData )
61+ })
4662}
4763
64+ # handle all app logic
4865server <- function (input , output ) {
4966
67+ # set reactive start date and end date variables to change when selected season changes
5068 selectedSeasonRow <- reactive({ seasons [input $ selectedSeason ,] })
5169 seasonStartDate <- reactive({ as.Date(selectedSeasonRow()$ start_date ) })
5270 seasonEndDate <- reactive({ as.Date(selectedSeasonRow()$ end_date ) })
5371
72+ # set unique cache name for date range for usability with DataCache library
5473 cacheName <- reactive({ paste0(' TraitCache_' , seasonStartDate(), ' _' , seasonEndDate()) })
55-
74+
75+ # render menu for selecting variable to view data for
5676 output $ selectVariable <- renderUI({
5777
78+ # get access to 'fullTraitData'
5879 data.cache(cache.name = cacheName(), loadTraitData , startDate = seasonStartDate(), endDate = seasonEndDate())
5980
81+ # get unique variable ids from observations in current season
6082 variableIds <- unique(as.numeric(fullTraitData $ variable_id ))
83+ # query API for readable names for variable ids, set names
6184 variableNames <- vector()
6285 for (variableId in variableIds ) {
6386 varName <- betydb_query(table = ' variables' , id = variableId )$ name
@@ -69,18 +92,25 @@ server <- function(input, output) {
6992 selectInput(' selectedVariable' , ' Variable' , variableIds )
7093 })
7194
95+ # render plot for selected variable
7296 output $ traitPlot <- renderPlot({
7397
98+ # get access to 'fullTraitData'
7499 data.cache(cache.name = cacheName(), loadTraitData , startDate = seasonStartDate(), endDate = seasonEndDate())
75100
101+ # only render plot of a variable is selected
76102 if (! is.null(input $ selectedVariable )) {
103+ # get observations for selected variable
77104 variableIdData <- betydb_query(table = ' variables' , id = input $ selectedVariable )
78105 variableTraitData <- subset(fullTraitData , variable_id == as.numeric(variableIdData $ id ))
79-
106+
107+ # generate timeseries of boxplots from mean value
80108 ggplot(variableTraitData , aes(as.Date(date ), mean )) +
81109 geom_boxplot(aes(group = cut_width(as.Date(date ), 1 ))) +
82- xlab(" Dates" ) + ylab(variableIdData $ units )
110+ xlab(" Dates" ) + ylab(variableIdData $ units ) +
111+ theme(text = element_text(size = 20 ), axis.text.x = element_text(angle = 45 , hjust = 1 ))
83112 }
113+
84114 })
85115}
86116
0 commit comments