Skip to content

Commit

Permalink
Merge pull request #39 from uace-azmet/battery-plots
Browse files Browse the repository at this point in the history
Battery plots
  • Loading branch information
Aariq authored Oct 5, 2023
2 parents de49b04 + e11dad5 commit d03ad9c
Show file tree
Hide file tree
Showing 4 changed files with 28,466 additions and 37 deletions.
9 changes: 7 additions & 2 deletions app/R/format_report_gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#' @param data original data frame used to generate the report
#'
#' @return a `gt` table
format_report_gt <- function(report, data) {
format_report_gt <- function(report, data, title = NULL) {
#TODO make warn_at and error_at arguments so they can be adjusted in the dashboard maybe?
#TODO would it be possible to add a header to the table that displays these thresholds?
warn_at <- 1 #warn if ≥ 1 row fails a validation
error_at <- 0.01 #error if ≥ 1% of rows fail validation

report |>
table <- report |>
#for some reason, report has duplicated rows. Seems like a bug in data.validator
slice_head(n = 1, by = assertion.id) |>
mutate(n_failed = if_else(is.na(num.violations), 0, num.violations),
Expand Down Expand Up @@ -52,6 +52,11 @@ format_report_gt <- function(report, data) {
) |>
fmt_percent(p_failed) |>
fmt_markdown(columns = c(validation, bad_rows))

if (!is.null(title)) {
table <- table |> tab_header(title = md(title))
}
table
}


Expand Down
61 changes: 61 additions & 0 deletions app/R/plot_battery.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# library(azmetr)
# library(tidyverse)
# library(plotly)
# library(colorspace)

# daily_all <- az_daily(start_date = "2020-12-30")
# daily_historic <-
# daily_all |>
# select(meta_station_id, meta_station_name, datetime, starts_with("meta_bat_volt"), temp_air_minC, temp_air_maxC, sol_rad_total)
# write_csv(daily_historic, "app/daily_historic.csv")


# from user input
# daily_check <- az_daily(start_date = "2023-08-01", end_date = "2023-08-15")


#pure plotly for speed

#TODO add toggle for min/max error bars?
#TODO make new points stand out even more (e.g. change shape)
#TODO use station name instead of ID in tooltip
plot_voltage <- function(check_data, xvar = c("temp_air_minC", "temp_air_maxC", "sol_rad_total")) {
daily_historic <- read_csv("daily_historic.csv")
xvar <- match.arg(xvar)
xlab <- switch(
xvar,
"temp_air_minC" = "Minimum Air Temp [ºC]",
"temp_air_maxC" = "Maximum Air Temp [ºC]",
"sol_rad_total" = "Solar Radiation Total [MJ m<sup>-2</sup>]"
)
plot_ly(
daily_historic,
y = ~meta_bat_volt_mean,
x = ~.data[[{{xvar}}]],
colors = colorspace::qualitative_hcl(n = 30)
) |>
add_markers(
color = I("black"),
# alpha = 0.2,
opacity = 0.2, #different from alpha, sets opacity of entire layer
hoverinfo = 'none' #disable for this layer only
) |>
add_markers(
data = check_data,
color = ~ meta_station_id,
## adds error bars for min and max voltage, but doesn't look great
# error_y = ~list(
# symmetric = FALSE, array = meta_bat_volt_max, arrayminus = meta_bat_volt_min
# ),
text = ~datetime,
hovertemplate = "%{text}",
showlegend = FALSE
) |>
layout(
xaxis = list(title = xlab),
yaxis = list(title = "Mean Voltage")
)

}
# plot_voltage(daily_check)

84 changes: 49 additions & 35 deletions app/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ ui <- page_navbar(
choices = c("Temperature", "Precip & Sun", "Wind")
)
),
# Unfortunately it is not easy to get plots to just fill their
# containers dynamically. I chose this height based on what looks
# good on my laptop, but this could be adjusted.
plotOutput(outputId = "plot_daily", height = 550)
plotOutput(outputId = "plot_daily")
)
)
)
Expand Down Expand Up @@ -200,7 +197,7 @@ ui <- page_navbar(
choices = c("Temperature", "Precip & Sun", "Wind")
)
),
plotOutput(outputId = "plot_hourly", height = 550)
plotOutput(outputId = "plot_hourly")
)
)
)
Expand Down Expand Up @@ -241,46 +238,53 @@ ui <- page_navbar(
choices = c("Temperature", "Precip & Sun", "Wind")
)
),
plotOutput(outputId = "plot_fc", height = 550)
plotOutput(outputId = "plot_fc")
)
)
)
),
## Battery ----
nav_panel(
title = "Battery",
"Battery",
layout_column_wrap(
width = NULL,
width = 1/2,
height = "100%",
fill = FALSE,
style = css(grid_template_columns = "1fr 1.5fr"),
card(
full_screen = TRUE,
card_header(
"Daily Data"
),
gt_output(outputId = "check_battery_daily")
card_header("Validation"),
gt_output(outputId = "check_battery_daily"),
gt_output(outputId = "check_battery_hourly")
),
card(
full_screen = TRUE,
card_header(
"Daily Data"
),
plotlyOutput(outputId = "plot_battery_daily", height = "300px")
),
card(
full_screen = TRUE,
card_header(
"Hourly Data"
),
gt_output(outputId = "check_battery_hourly")
),
card(
full_screen = TRUE,
card_header(
"Hourly Data"
layout_column_wrap(
width = 1,
navset_card_tab(
full_screen = TRUE,
title = "Timeseries",
nav_panel(
"Daily",
plotlyOutput(outputId = "plot_battery_daily")
),
nav_panel(
"Hourly",
plotlyOutput(outputId = "plot_battery_hourly")
)
),
plotlyOutput(outputId = "plot_battery_hourly", height = "300px")
navset_card_tab(
full_screen = TRUE,
title = "Voltage",
nav_panel(
"Min Temp",
plotlyOutput(outputId = "plot_battery_min_temp")
),
nav_panel(
"Max Temp",
plotlyOutput(outputId = "plot_battery_max_temp")
),
nav_panel(
"Solar Radiation",
plotlyOutput(outputId = "plot_battery_sol_rad")
)
)
)
)
)
Expand Down Expand Up @@ -447,7 +451,7 @@ server <- function(input, output, session) {
report_battery_daily <- check_battery_daily(daily)

#convert to gt table
format_report_gt(report_battery_daily, daily)
format_report_gt(report_battery_daily, daily, title = "Daily")
})

output$check_battery_hourly <- gt::render_gt({
Expand All @@ -458,7 +462,7 @@ server <- function(input, output, session) {
report_battery_hourly <- check_battery_hourly(hourly)

#convert to gt table
format_report_gt(report_battery_hourly, hourly)
format_report_gt(report_battery_hourly, hourly, title = "Hourly")
})

#TODO move plotting code to function?
Expand Down Expand Up @@ -496,6 +500,16 @@ server <- function(input, output, session) {

ggplotly(h_daily)
})
#TODO could probably streamline this by passing tab name to function as variable and conditionally rendering or something
output$plot_battery_min_temp <- renderPlotly({
plot_voltage(daily, "temp_air_minC")
})
output$plot_battery_max_temp <- renderPlotly({
plot_voltage(daily, "temp_air_maxC")
})
output$plot_battery_sol_rad <- renderPlotly({
plot_voltage(daily, "sol_rad_total")
})
shinybusy::remove_modal_spinner()
}
})
Expand Down
Loading

0 comments on commit d03ad9c

Please sign in to comment.