Skip to content

Commit

Permalink
Report for Timeseries
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianHordyk committed Sep 10, 2024
1 parent 93be54d commit 1d66d5c
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 191 deletions.
18 changes: 9 additions & 9 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ app_server <- function(input, output, session) {

waitress$inc(5)

mod_Boxplot_server("Boxplot", i18n, Slick_Object, window_dims, Report, session)
waitress$inc(5)
mod_Kobe_server("Kobe", i18n, Slick_Object, window_dims, Report, session)
waitress$inc(5)
mod_Quilt_server("Quilt", i18n, Slick_Object, window_dims, Report, session)
waitress$inc(5)
mod_Spider_server("Spider", i18n, Slick_Object, window_dims, Report, session)
waitress$inc(5)
# mod_Boxplot_server("Boxplot", i18n, Slick_Object, window_dims, Report, session)
# waitress$inc(5)
# mod_Kobe_server("Kobe", i18n, Slick_Object, window_dims, Report, session)
# waitress$inc(5)
# mod_Quilt_server("Quilt", i18n, Slick_Object, window_dims, Report, session)
# waitress$inc(5)
# mod_Spider_server("Spider", i18n, Slick_Object, window_dims, Report, session)
# waitress$inc(5)
mod_Timeseries_server("Timeseries_1", i18n, Slick_Object, window_dims, Report, session)
waitress$inc(5)
mod_Tradeoff_server("Tradeoff", i18n, Slick_Object, window_dims, Report, session)
# mod_Tradeoff_server("Tradeoff", i18n, Slick_Object, window_dims, Report, session)

waitress$close()
}
58 changes: 34 additions & 24 deletions R/fct_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,16 @@ plotTimeseries <- function(slick,
quants2=c(0.1, 0.9),
MP_label='Code',
size.title=18,
size.axis.title=14,
size.axis.text=14,
size.axis.title=18,
size.axis.text=16,
size.mp.label=6,
targ_color='green',
targ_name='Target',
lim_color='red',
lim_name='Limit',
inc_y_label=TRUE,
sims=NULL) {
sims=NULL,
ncol=4) {

# if (length(PM_ind)<1) return(NULL)

Expand Down Expand Up @@ -333,7 +334,8 @@ plotTimeseries <- function(slick,
sims <- 1:dim(values)[1]
}

p <- ggplot2::ggplot()
p <- ggplot2::ggplot() +
ggplot2::theme_bw()


if (includeHist) {
Expand Down Expand Up @@ -381,42 +383,50 @@ plotTimeseries <- function(slick,
meddf$MP <- factor(meddf$MP, ordered=TRUE, levels=MP_lab)

if (!is.null(MP_ind)) {
if (length(MP_ind)>1)
stop('`MP_ind` must be NULL or numeric length 1')
if (MP_ind>nMP) {
warning('`MP_ind` is greater than `nMP`. Setting to last MP')
MP_ind <- nMP
}
# if (length(MP_ind)>1)
# stop('`MP_ind` must be NULL or numeric length 1')
# if (MP_ind>nMP) {
# warning('`MP_ind` is greater than `nMP`. Setting to last MP')
# MP_ind <- nMP
# }

# by MP
meddf <- meddf |> dplyr::filter(MP%in%MP_lab[MP_ind])

quant.1.mp <- apply(mean.mps[,MP_ind,], 2, quantile, probs=quants1, na.rm=TRUE)
quant.2.mp <- apply(mean.mps[,MP_ind,], 2, quantile, probs=quants2, na.rm=TRUE)

quant.1.mp <- apply(mean.mps[,MP_ind,, drop=FALSE], 2:3, quantile, probs=quants1, na.rm=TRUE)
quant.2.mp <- apply(mean.mps[,MP_ind,,drop=FALSE], 2:3, quantile, probs=quants2, na.rm=TRUE)

# quant.1.mp <- apply(values[sims,oms,MP_ind, PM_ind,proj.yr.ind, drop=FALSE], c(3,5), quantile, probs=quants1, na.rm=TRUE)
# quant.2.mp <- apply(values[sims,oms,MP_ind, PM_ind,proj.yr.ind, drop=FALSE], c(3,5), quantile, probs=quants2, na.rm=TRUE)

quant_df <- data.frame(x=rep(proj.yrs, each=1),
quant_df <- data.frame(x=rep(proj.yrs, each=length(MP_ind)),
MP=MP_lab[MP_ind],
Lower1=as.vector(quant.1.mp[1,]),
Upper1=as.vector(quant.1.mp[2,]),
Lower2=as.vector(quant.2.mp[1,]),
Upper2=as.vector(quant.2.mp[2,]))
Lower1=as.vector(quant.1.mp[1,,]),
Upper1=as.vector(quant.1.mp[2,,]),
Lower2=as.vector(quant.2.mp[1,,]),
Upper2=as.vector(quant.2.mp[2,,]))
quant_df$MP <- factor(quant_df$MP, ordered=TRUE, levels=MP_lab[MP_ind])

p <- p +
ggplot2::facet_wrap(~MP, ncol=ncol) +
ggplot2::geom_ribbon(ggplot2::aes(x=x, ymin=Lower2, ymax=Upper2), data=quant_df,
color=col_ribbon2, fill=fill_ribbon2, linetype=linetype_ribbon2) +
ggplot2::geom_ribbon(ggplot2::aes(x=x, ymin=Lower1, ymax=Upper1), data=quant_df,
fill=fill_ribbon1, col=col_ribbon1) +
ggplot2::geom_line(ggplot2::aes(x=x, y=Median, color=MP[MP_ind]), data=meddf) +
ggplot2::geom_line(ggplot2::aes(x=x, y=Median, color=MP), data=meddf) +
ggplot2::scale_color_manual(values=MP_colors[MP_ind]) +
ggplot2::guides(color='none') +
ggplot2::labs(title=MP_lab[MP_ind]) +
ggplot2::theme(plot.title =ggplot2::element_text(colour=MP_colors[MP_ind],
face='bold', hjust = 0.5,
size=size.title))
ggplot2::guides(color='none')

if (length(MP_ind)<2) {
p <- p + ggplot2::labs(title=MP_lab[MP_ind]) +
ggplot2::theme(plot.title =ggplot2::element_text(colour=MP_colors[MP_ind],
face='bold', hjust = 0.5,
size=size.title))
} else {
p <- p + ggplot2::theme(strip.text = ggplot2::element_text(size=size.title))
}


} else {
meddf_last <- meddf |> dplyr::filter(x==max(x, na.rm=TRUE))
Expand Down Expand Up @@ -479,12 +489,12 @@ plotTimeseries <- function(slick,
}

if (!inc_y_label) PM_lab <- ''

p <- p +
ggplot2::labs(x=time_lab, y=PM_lab, color='') +
ggplot2::scale_y_continuous(label=scales::comma) +
ggplot2::theme(axis.title = ggplot2::element_text(size=size.axis.title, face='bold'),
axis.text = ggplot2::element_text(size=size.axis.text)) +
ggplot2::theme_bw() +
ggplot2::expand_limits(y=0) +
ggplot2::scale_x_continuous(expand = c(0, 0))

Expand Down
69 changes: 48 additions & 21 deletions R/mod_PM_Info.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,62 @@ mod_PM_Info_ui <- function(id){
)
}

firstup <- function(x) {
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
x
}

#' PM_Info Server Functions
#'
#' @noRd
mod_PM_Info_server <- function(id, i18n, Slick_Object){
moduleServer( id, function(input, output, session){
ns <- session$ns

output$PMinfo <- renderUI({

PITabs <- reactive({
slick <- Slick_Object()
i18n <- i18n()
tabsetPanel(type = "tabs", id='pmtabsetpanel',
tabPanel("Boxplot",
br(),
DT::dataTableOutput(session$ns('PM_Boxplot'))),
tabPanel("Kobe",
br(),
DT::dataTableOutput(session$ns('PM_Kobe'))),
tabPanel("Quilt",
br(),
DT::dataTableOutput(session$ns('PM_Quilt'))),
tabPanel("Spider",
br(),
DT::dataTableOutput(session$ns('PM_Spider'))),
tabPanel("Time Series",
br(),
DT::dataTableOutput(session$ns('PM_Timeseries'))),
tabPanel("Tradeoff",
br(),
DT::dataTableOutput(session$ns('PM_Tradeoff')))
)

ll <- list()
PIplots <- c('Boxplot', 'Kobe', 'Quilt', 'Spider', 'Time Series', 'Tradeoff')
for (i in seq_along(PIplots)) {
slotname <- PIplots[i]
slotname <- firstup(gsub(' ' ,'', tolower(slotname)))
tab_name <- paste('PM', slotname, sep='_')

if (!is_empty(slot(slick, slotname)))
ll[[i]] <- tabPanel(PIplots[i],
br(),
DT::dataTableOutput(ns(tab_name)))
}
ll
})


output$PMinfo <- renderUI({
# i18n <- i18n()
# tabsetPanel(type = "tabs", id='pmtabsetpanel',
# tabPanel("Boxplot",
# br(),
# DT::dataTableOutput(session$ns('PM_Boxplot'))),
# tabPanel("Kobe",
# br(),
# DT::dataTableOutput(session$ns('PM_Kobe'))),
# tabPanel("Quilt",
# br(),
# DT::dataTableOutput(session$ns('PM_Quilt'))),
# tabPanel("Spider",
# br(),
# DT::dataTableOutput(session$ns('PM_Spider'))),
# tabPanel("Time Series",
# br(),
# DT::dataTableOutput(session$ns('PM_Timeseries'))),
# tabPanel("Tradeoff",
# br(),
# DT::dataTableOutput(session$ns('PM_Tradeoff')))
# )
do.call(tabsetPanel, c(PITabs(), list(id='pmtabsetpanel')))
})

output$PM_Boxplot <- DT::renderDataTable({
Expand Down
106 changes: 12 additions & 94 deletions R/mod_Report_Page.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ mod_Report_Page_server <- function(id, i18n, Slick_Object, Report){
if (ready()>0) {
tagList(
uiOutput(ns('metadata')),
uiOutput(ns('boxplot')),
uiOutput(ns('quilt'))
mod_Report_Page_plot_ui(ns('timeseries')),
mod_Report_Page_plot_ui(ns('boxplot')),
mod_Report_Page_plot_ui(ns('kobe')),
mod_Report_Page_plot_ui(ns('quilt')),
mod_Report_Page_plot_ui(ns('spider')),
mod_Report_Page_plot_ui(ns('tradeoff'))
)
} else {
tagList(h3(i18n$t('Nothing added to Report yet')))
Expand All @@ -71,99 +75,13 @@ mod_Report_Page_server <- function(id, i18n, Slick_Object, Report){
)
})

# Time Series ----


# Boxplot ----

observeEvent(Report$Boxplot, {
nplot <- length(Report$Boxplot$plot)
if (nplot>0) {
for (x in 1:nplot) {
local({
this_x <- x
this_plot <- Report$Boxplot$plot[[this_x]]
if (!prod(is.na(this_plot)))
output[[paste(this_x, "boxplot", sep="")]] <- renderImage({
this_plot
}, deleteFile=FALSE)
})
}
}
})

boxplots <- reactive({
nplot <- length(Report$Boxplot$plot)
if (nplot>0) {
plot_output_list <- lapply(1:nplot, function(x) {
plotname <- paste0(x, 'boxplot')
caption <- Report$Boxplot$caption[[x]]
if (!is.na(caption)) {
caption <- p(caption)
} else {
caption <- NULL
}
if (!is.null(caption))
tagList(
hr(),
imageOutput(ns(plotname)),
caption,
shinyWidgets::actionBttn(ns(paste0('del-', plotname)),
label='Remove',
icon('remove'),
color='danger',size='sm'),
hr()
)
})
do.call('tagList', plot_output_list)
}
})

output$boxplot <- renderUI({
chk <- lapply(Report$Boxplot$plot, is.na) |> unlist()
if (!all(chk)) {
tagList(
h3('Boxplot'),
boxplots()
)
}
})

observe({
nplot <- length(Report$Boxplot$plot)
if (nplot>0) {
for (x in 1:nplot) {
this_x <- x
observeEvent(eventExpr = input[[paste0('del-', this_x, "boxplot")]],
handlerExpr = {
if (!all(is.na(Report$Boxplot$plot[[this_x]])))
file.remove(Report$Boxplot$plot[[this_x]]$src)
Report$Boxplot$plot[[this_x]] <- NA
Report$Boxplot$caption[[this_x]] <- NA
})
}
}
})


# Quilt ----
output$quilt <- renderUI({
i18n <- i18n()
if (length(Report$Quilt$plot)>0) {
ll <- lapply(1:length(Report$Quilt$plot), function(x) {
tagList(
Report$Quilt$plot[[x]] |>
flextable::autofit() |>
flextable::htmltools_value(),
Report$Quilt$caption[[x]]
)
})
tagList(
h3('Quilt'),
ll
)
}
})
mod_Report_Page_plot_server('timeseries', 'Timeseries', Report)
mod_Report_Page_plot_server('boxplot', 'Boxplot', Report)
mod_Report_Page_plot_server('kobe', 'Kobe', Report)
mod_Report_Page_plot_server('quilt', 'Quilt', Report)
mod_Report_Page_plot_server('spider', 'Spider', Report)
mod_Report_Page_plot_server('tradeoff', 'Trade-off', Report)

extension <- reactive({
req(input$format)
Expand Down
Loading

0 comments on commit 1d66d5c

Please sign in to comment.