Skip to content

Commit

Permalink
building
Browse files Browse the repository at this point in the history
  • Loading branch information
danswell committed Dec 30, 2019
1 parent 89c3a50 commit 48af78b
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
141 changes: 141 additions & 0 deletions App.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# This code does the following:

#1 load libraries
#2 extract/process data from data.act.gov.au
#3 extract/process site metadata from data.act.gov.au
#4 Create UI
#5 Create Server

#1 Load libraries

library(dplyr)
library(tidyr)
library(RSocrata)
library(soql)
library(lubridate)
library(ggplot2)
library(shiny)

#2 Extract and process data from data.act.gov.au
# This code uses the RSocrate API. It can be used to extract both the data and the metadata

Query <- soql() %>%
soql_add_endpoint("https://www.data.act.gov.au/resource/yuhh-28ai.json") %>%
soql_simple_filter("VariableName", "Rainfall") %>%
#soql_where("DatetimeAEST" > "2009-01-01T09:00:00.000") %>% #fix this somehow
soql_select("DatetimeAEST, Value, SiteID") %>%
as.character()


Rainfall_data <- read.socrata(Query)

Rainfall_data$Value <- as.numeric(Rainfall_data$Value)
Rainfall_data$DatetimeAEST <- as.POSIXct(Rainfall_data$DatetimeAEST, format = "%Y-%m-%dT%H:%M:%S")
Rainfall_data$SiteID <- as.factor(Rainfall_data$SiteID)


#3// Extract metadata from data.act.gov.au

Query2 <- soql() %>%
soql_add_endpoint("https://www.data.act.gov.au/resource/tsq4-63ge.json") %>%
soql_select("Siteid, siteName, latitude, longitude") %>%
as.character()

My_meta <- read.socrata(Query2)

My_meta$Siteid <- as.factor(My_meta$Siteid)
My_meta$latitude <- as.numeric(My_meta$latitude)
My_meta$longitude <- as.numeric(My_meta$longitude)

#filter to relevant sites in the Rainfall dataset
My_meta <- My_meta[My_meta$Siteid %in% Rainfall_data$SiteID,]


##4// UI

ui <- fluidPage(titlePanel("ACT Rainfall Explorer"),
sidebarLayout(
sidebarPanel(

# Select site to plot
selectInput(inputId = "site", label = strong("Rain Gauge"),
choices = unique(My_meta$siteName),
selected = "Licking Hole Creek above Cotter Junction"),

# Select date range to be plotted
uiOutput("dateRange"),


# Output: Description, lineplot, and reference
mainPanel(
plotOutput(outputId = "lineplot", height = "400px"),
plotOutput(outputId = "cumplot", height = "400px"),
tags$body("Data provided by ACT Government. Gauged streamflow data generated by in-situ streamflow gauges.
Modelled streamflow is derived from the ACT Government Source catchment model (Predevelopment Scenario),
which models streamflow in the absense of all water resource development and land-use change")
)
)
)

#5// Define server function
server <- function(input, output) {

# Subset data
selected_rain <- reactive({
req(input$Date)
validate(need(!is.na(input$Date[1]) & !is.na(input$Date[2]), "Error: Please provide both a start and an end date."))
validate(need(input$Date[1] < input$Date[2], "Error: Start date should be earlier than end date."))
shiny_data %>%
filter(
Site == input$site,
Date > as.Date(input$Date[1]) & Date < as.Date(input$Date[2])
)
})

output$dateRange <- renderUI({
sliderInput("Date", strong("Select the date range:"),
start =
as.character(format(as.Date(min(df()$date))),"yyyy-mm-dd"), # Start
end =
as.character(format(as.Date(max(df()$date))),"yyyy-mm-dd"), # End
min =
as.character(format(as.Date(min(df()$date))),"yyyy-mm-dd"),
max =
as.character(format(as.Date(max(df()$date))),"yyyy-mm-dd"),
format = "yyyy-mm-dd")

})
# https://stackoverflow.com/questions/48633984/pass-a-dynamic-date-date-range-to-daterangeinput-in-r-shiny

# Create scatterplot object the plotOutput function is expecting
output$lineplot <- renderPlot({
ggplot(selected_flow()) +
geom_line(aes(Date, Gauged_ML_day, color = "blue")) +
labs(x = "Date", y = "Streamflow (ML/day)", title = paste("Streamflow at" ,(input$site))) +
scale_color_manual(values = c("blue", "red"), labels = c("Gauged Streamflow", "Modelled Streamflow"), name = "Legend") +
# Display only if modelled flow is checked
if(input$modelled_flow){
geom_line(aes(Date, Modelled_ML_day, color = "red"))
}
})

#Create cumulative flow plot
output$cumplot <- renderPlot({
ggplot(selected_flow()) +
geom_line(aes(Date, cumsum(replace_na(Gauged_ML_day,0))/1000, color = "blue")) +
labs(x = "Date", y = "Cumulative flow (GL)", title = paste("Cumulative flow at", (input$site))) +
scale_color_manual(values = c("blue", "red"), labels = c("Gauged cumulative flow", "Modelled cumulative flow"), name = "Legend") +
#Disply only if a modelled flow is checked
if(input$modelled_flow){
geom_line(aes(Date, cumsum(replace_na(Modelled_ML_day,0))/1000, color = "red"))
}

})
}

# Create Shiny object
shinyApp(ui = ui, server = server)




13 changes: 13 additions & 0 deletions shiny_rainfall.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

0 comments on commit 48af78b

Please sign in to comment.