Spaces:
Running
Running
phantomas34 commited on
Commit Β·
efd0257
1
Parent(s): eee4ed0
pre-IMS-tab checkpoint: working dashboard before client intelligence tab
Browse files- .Rprofile +1 -0
- Payment Confirmation.pdf +0 -0
- cohort_wiring_patch.R +68 -0
- global.R +3 -0
- mod_cohort_analysis.R +443 -0
- renv.lock +0 -0
- renv/.gitignore +7 -0
- renv/activate.R +1419 -0
- scratch.R +0 -0
- server.R +611 -18
- tutorial.R +0 -0
- ui.R +117 -4
.Rprofile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
source("renv/activate.R")
|
Payment Confirmation.pdf
ADDED
|
Binary file (74.1 kB). View file
|
|
|
cohort_wiring_patch.R
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# WIRING PATCHES β Cohort Analysis Module
|
| 3 |
+
# Two surgical additions to your existing ui.R and server.R.
|
| 4 |
+
# No other lines change.
|
| 5 |
+
# =============================================================================
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
# PATCH 1 OF 2 β ui.R
|
| 10 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
#
|
| 12 |
+
# Location A: Inside sidebarMenu(), after the "Data Management" menuItem.
|
| 13 |
+
# Add this block:
|
| 14 |
+
|
| 15 |
+
menuItem("Cohort Win Rate", tabName = "cohort", icon = icon("layer-group")),
|
| 16 |
+
|
| 17 |
+
#
|
| 18 |
+
# Location B: Inside tabItems(), after the closing brace of the "admin" tabItem.
|
| 19 |
+
# Add this block:
|
| 20 |
+
|
| 21 |
+
# ββ COHORT WIN RATE ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
tabItem(tabName = "cohort",
|
| 23 |
+
cohortAnalysisUI("cohort")
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
#
|
| 27 |
+
# No other changes to ui.R are needed. The module's pickerInput uses
|
| 28 |
+
# shinyWidgets which is already loaded in global.R.
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
+
# PATCH 2 OF 2 β server.R
|
| 33 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
+
#
|
| 35 |
+
# Location A: At the very top of server.R, alongside any other source() calls.
|
| 36 |
+
# Add:
|
| 37 |
+
|
| 38 |
+
source("mod_cohort_analysis.R")
|
| 39 |
+
|
| 40 |
+
#
|
| 41 |
+
# Location B: Inside the server function, after the last output/observer block
|
| 42 |
+
# (e.g., after the download_report downloadHandler). Add:
|
| 43 |
+
|
| 44 |
+
# ββ COHORT WIN RATE MODULE ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
# Pass get_data (the unfiltered pool reactive) so the module has full
|
| 46 |
+
# history for attribution windows that extend past the sidebar date range.
|
| 47 |
+
cohortAnalysisServer("cohort", get_data = get_data)
|
| 48 |
+
|
| 49 |
+
#
|
| 50 |
+
# That's it. The module manages its own estimator picker, date range,
|
| 51 |
+
# forward window, and all outputs internally.
|
| 52 |
+
#
|
| 53 |
+
# NOTE: `get_data` in server.R is a reactive expression (not a reactive value),
|
| 54 |
+
# so pass it without parentheses: get_data = get_data β
|
| 55 |
+
# get_data = get_data() β
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
# DEPENDENCY CHECK
|
| 60 |
+
# All packages used by the module are already present in global.R:
|
| 61 |
+
# purrr β loaded via tidyverse
|
| 62 |
+
# tidyr β loaded via tidyverse
|
| 63 |
+
# scales β already library(scales)
|
| 64 |
+
# DT β already library(DT)
|
| 65 |
+
# plotly β already library(plotly)
|
| 66 |
+
# shinyWidgets β already library(shinyWidgets)
|
| 67 |
+
# No new library() calls needed.
|
| 68 |
+
# =============================================================================
|
global.R
CHANGED
|
@@ -15,6 +15,9 @@ library(strucchange) # ITS breakpoint detection
|
|
| 15 |
library(lmtest) # ITS coefficient testing
|
| 16 |
library(pool) # Connection pooling
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# 1. ESTABLISH DATABASE CONNECTION POOL
|
| 19 |
db_pool <- dbPool(
|
| 20 |
drv = RPostgres::Postgres(),
|
|
|
|
| 15 |
library(lmtest) # ITS coefficient testing
|
| 16 |
library(pool) # Connection pooling
|
| 17 |
|
| 18 |
+
# --- LOAD MODULES ---
|
| 19 |
+
source("mod_cohort_analysis.R") # <--- THIS IS THE MISSING LINE!
|
| 20 |
+
|
| 21 |
# 1. ESTABLISH DATABASE CONNECTION POOL
|
| 22 |
db_pool <- dbPool(
|
| 23 |
drv = RPostgres::Postgres(),
|
mod_cohort_analysis.R
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# MODULE: Cohort Win Rate Analysis
|
| 3 |
+
# File: mod_cohort_analysis.R
|
| 4 |
+
#
|
| 5 |
+
# Drop-in Shiny module for the Estimator Intelligence Dashboard.
|
| 6 |
+
# Measures how much work bid in a given period is eventually rewarded,
|
| 7 |
+
# regardless of when the booking is recorded.
|
| 8 |
+
#
|
| 9 |
+
# Usage in ui.R β tabItem(tabName = "cohort", cohortAnalysisUI("cohort"))
|
| 10 |
+
# Usage in server.R β cohortAnalysisServer("cohort", get_data = get_data)
|
| 11 |
+
#
|
| 12 |
+
# Pass `get_data` β the UNFILTERED pool reactive β so the module has full
|
| 13 |
+
# history for forward attribution windows beyond the sidebar date range.
|
| 14 |
+
# Expected columns: primary_name, report_date (Date), amt_bid, qty_bid,
|
| 15 |
+
# amt_booked, qty_booked
|
| 16 |
+
# =============================================================================
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# -----------------------------------------------------------------------------
|
| 20 |
+
# UI
|
| 21 |
+
# -----------------------------------------------------------------------------
|
| 22 |
+
|
| 23 |
+
cohortAnalysisUI <- function(id) {
|
| 24 |
+
ns <- NS(id)
|
| 25 |
+
|
| 26 |
+
tagList(
|
| 27 |
+
|
| 28 |
+
# --- Control Row ---
|
| 29 |
+
fluidRow(
|
| 30 |
+
box(
|
| 31 |
+
title = "Cohort Parameters",
|
| 32 |
+
status = "primary",
|
| 33 |
+
solidHeader = TRUE,
|
| 34 |
+
width = 12,
|
| 35 |
+
collapsible = TRUE,
|
| 36 |
+
|
| 37 |
+
fluidRow(
|
| 38 |
+
column(3,
|
| 39 |
+
dateRangeInput(
|
| 40 |
+
ns("cohort_range"),
|
| 41 |
+
label = "Bid Cohort Window",
|
| 42 |
+
start = floor_date(Sys.Date() %m-% months(12), "month"),
|
| 43 |
+
end = floor_date(Sys.Date() %m-% months(1), "month"),
|
| 44 |
+
format = "M yyyy",
|
| 45 |
+
startview = "year"
|
| 46 |
+
),
|
| 47 |
+
p(class = "text-muted", style = "font-size:11px; margin-top:-6px;",
|
| 48 |
+
"Define the period during which bids were submitted.")
|
| 49 |
+
),
|
| 50 |
+
column(3,
|
| 51 |
+
sliderInput(
|
| 52 |
+
ns("forward_window"),
|
| 53 |
+
label = "Forward Attribution Window (months)",
|
| 54 |
+
min = 1,
|
| 55 |
+
max = 18,
|
| 56 |
+
value = 9,
|
| 57 |
+
step = 1,
|
| 58 |
+
ticks = FALSE,
|
| 59 |
+
post = " mo"
|
| 60 |
+
),
|
| 61 |
+
p(class = "text-muted", style = "font-size:11px; margin-top:-6px;",
|
| 62 |
+
"How many months past the cohort end to capture delayed bookings.")
|
| 63 |
+
),
|
| 64 |
+
column(3,
|
| 65 |
+
pickerInput(
|
| 66 |
+
ns("estimator_filter"),
|
| 67 |
+
label = "Estimators:",
|
| 68 |
+
choices = NULL,
|
| 69 |
+
multiple = TRUE,
|
| 70 |
+
options = list(
|
| 71 |
+
`actions-box` = TRUE,
|
| 72 |
+
`live-search` = TRUE,
|
| 73 |
+
`selected-text-format` = "count > 2",
|
| 74 |
+
`count-selected-text` = "{0} Selected",
|
| 75 |
+
`none-selected-text` = "All Estimators"
|
| 76 |
+
)
|
| 77 |
+
)
|
| 78 |
+
),
|
| 79 |
+
column(3,
|
| 80 |
+
br(),
|
| 81 |
+
actionButton(ns("run_cohort"), "Calculate Cohort",
|
| 82 |
+
icon = icon("calculator"),
|
| 83 |
+
class = "btn-success btn-block",
|
| 84 |
+
width = "100%")
|
| 85 |
+
)
|
| 86 |
+
)
|
| 87 |
+
)
|
| 88 |
+
),
|
| 89 |
+
|
| 90 |
+
# --- KPI Row ---
|
| 91 |
+
fluidRow(
|
| 92 |
+
valueBoxOutput(ns("box_total_bid"), width = 3),
|
| 93 |
+
valueBoxOutput(ns("box_total_booked"), width = 3),
|
| 94 |
+
valueBoxOutput(ns("box_win_rate_amt"), width = 3),
|
| 95 |
+
valueBoxOutput(ns("box_win_rate_qty"), width = 3)
|
| 96 |
+
),
|
| 97 |
+
|
| 98 |
+
# --- Chart Row ---
|
| 99 |
+
fluidRow(
|
| 100 |
+
box(
|
| 101 |
+
title = "Cohort Win Rate vs. Same-Month Win Rate by Estimator",
|
| 102 |
+
status = "primary",
|
| 103 |
+
solidHeader = TRUE,
|
| 104 |
+
width = 8,
|
| 105 |
+
plotlyOutput(ns("comparison_chart"), height = "380px"),
|
| 106 |
+
p(class = "text-muted", style = "font-size:11px;",
|
| 107 |
+
"Blue = cohort win rate (bookings attributed forward across the full window).
|
| 108 |
+
Orange = same-month win rate (bookings recorded within the cohort window only).
|
| 109 |
+
A large positive gap means delayed wins are real β they just don't show up in-period.")
|
| 110 |
+
),
|
| 111 |
+
box(
|
| 112 |
+
title = "Attribution Window Sensitivity",
|
| 113 |
+
status = "warning",
|
| 114 |
+
solidHeader = TRUE,
|
| 115 |
+
width = 4,
|
| 116 |
+
plotlyOutput(ns("sensitivity_chart"), height = "340px"),
|
| 117 |
+
p(class = "text-muted", style = "font-size:11px;",
|
| 118 |
+
"Aggregate cohort win rate at each forward window length.
|
| 119 |
+
The plateau marks your empirical bid-to-close lag.")
|
| 120 |
+
)
|
| 121 |
+
),
|
| 122 |
+
|
| 123 |
+
# --- Table Row ---
|
| 124 |
+
fluidRow(
|
| 125 |
+
box(
|
| 126 |
+
title = "Cohort Detail by Estimator",
|
| 127 |
+
status = "warning",
|
| 128 |
+
solidHeader = TRUE,
|
| 129 |
+
width = 12,
|
| 130 |
+
dataTableOutput(ns("cohort_table")),
|
| 131 |
+
p(class = "text-muted", style = "font-size:11px; margin-top:6px;",
|
| 132 |
+
"Lag Gap = Cohort Win Rate minus Same-Month Win Rate.
|
| 133 |
+
Green = bookings trailing bids (work is being rewarded, just later).
|
| 134 |
+
Red = same-month rate exceeds cohort rate (bookings drawing from older pipelines).")
|
| 135 |
+
)
|
| 136 |
+
),
|
| 137 |
+
|
| 138 |
+
# --- Reading Guide ---
|
| 139 |
+
fluidRow(
|
| 140 |
+
box(
|
| 141 |
+
title = "Reading This Analysis",
|
| 142 |
+
status = "info",
|
| 143 |
+
solidHeader = FALSE,
|
| 144 |
+
width = 12,
|
| 145 |
+
collapsible = TRUE,
|
| 146 |
+
collapsed = TRUE,
|
| 147 |
+
tags$p(HTML(
|
| 148 |
+
"<b>Cohort Win Rate</b> answers: of every dollar bid during the selected window,
|
| 149 |
+
how much eventually converted β even if the booking appeared in a later month?"
|
| 150 |
+
)),
|
| 151 |
+
tags$p(HTML(
|
| 152 |
+
"<b>Same-Month Win Rate</b> (existing dashboard metric) answers: in the same period
|
| 153 |
+
the bids were submitted, what was the ratio of bookings recorded to bids?
|
| 154 |
+
These two figures diverge whenever your bid-to-award cycle spans multiple months."
|
| 155 |
+
)),
|
| 156 |
+
tags$p(HTML(
|
| 157 |
+
"The <b>sensitivity curve</b> sweeps the forward window from 0 to 18 months.
|
| 158 |
+
The plateau point is your approximate bid-to-close lag β where additional
|
| 159 |
+
window months stop adding meaningful bookings. That number is useful context for
|
| 160 |
+
the President/CEO: it's the lag between effort and outcome in the pipeline."
|
| 161 |
+
))
|
| 162 |
+
)
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
)
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# -----------------------------------------------------------------------------
|
| 170 |
+
# Server
|
| 171 |
+
# -----------------------------------------------------------------------------
|
| 172 |
+
|
| 173 |
+
cohortAnalysisServer <- function(id, get_data) {
|
| 174 |
+
moduleServer(id, function(input, output, session) {
|
| 175 |
+
|
| 176 |
+
ns <- session$ns
|
| 177 |
+
|
| 178 |
+
# --- Populate estimator picker from live pool ---
|
| 179 |
+
observe({
|
| 180 |
+
df <- get_data()
|
| 181 |
+
req(nrow(df) > 0)
|
| 182 |
+
estimators <- sort(unique(df$primary_name))
|
| 183 |
+
updatePickerInput(session, "estimator_filter",
|
| 184 |
+
choices = estimators,
|
| 185 |
+
selected = estimators)
|
| 186 |
+
})
|
| 187 |
+
|
| 188 |
+
# --- Core computation helper ---
|
| 189 |
+
# Returns per-estimator cohort stats for a given forward attribution window.
|
| 190 |
+
compute_cohort <- function(df, cohort_start, cohort_end, forward_months) {
|
| 191 |
+
|
| 192 |
+
attribution_end <- cohort_end %m+% months(forward_months)
|
| 193 |
+
|
| 194 |
+
# Bids placed within the cohort window
|
| 195 |
+
bids_in_cohort <- df %>%
|
| 196 |
+
filter(report_date >= cohort_start, report_date <= cohort_end) %>%
|
| 197 |
+
group_by(primary_name) %>%
|
| 198 |
+
summarise(
|
| 199 |
+
cohort_amt_bid = sum(amt_bid, na.rm = TRUE),
|
| 200 |
+
cohort_qty_bid = sum(qty_bid, na.rm = TRUE),
|
| 201 |
+
.groups = "drop"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Bookings recorded from cohort_start through attribution_end (full window)
|
| 205 |
+
bookings_forward <- df %>%
|
| 206 |
+
filter(report_date >= cohort_start, report_date <= attribution_end) %>%
|
| 207 |
+
group_by(primary_name) %>%
|
| 208 |
+
summarise(
|
| 209 |
+
cohort_amt_booked = sum(amt_booked, na.rm = TRUE),
|
| 210 |
+
cohort_qty_booked = sum(qty_booked, na.rm = TRUE),
|
| 211 |
+
.groups = "drop"
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# Same-month baseline: bookings only within cohort_start β cohort_end
|
| 215 |
+
bookings_same_mo <- df %>%
|
| 216 |
+
filter(report_date >= cohort_start, report_date <= cohort_end) %>%
|
| 217 |
+
group_by(primary_name) %>%
|
| 218 |
+
summarise(
|
| 219 |
+
sm_amt_booked = sum(amt_booked, na.rm = TRUE),
|
| 220 |
+
sm_qty_booked = sum(qty_booked, na.rm = TRUE),
|
| 221 |
+
.groups = "drop"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
bids_in_cohort %>%
|
| 225 |
+
left_join(bookings_forward, by = "primary_name") %>%
|
| 226 |
+
left_join(bookings_same_mo, by = "primary_name") %>%
|
| 227 |
+
mutate(
|
| 228 |
+
cohort_win_rate_amt = cohort_amt_booked / cohort_amt_bid,
|
| 229 |
+
cohort_win_rate_qty = cohort_qty_booked / cohort_qty_bid,
|
| 230 |
+
sm_win_rate_amt = sm_amt_booked / cohort_amt_bid,
|
| 231 |
+
sm_win_rate_qty = sm_qty_booked / cohort_qty_bid,
|
| 232 |
+
lag_gap_amt = cohort_win_rate_amt - sm_win_rate_amt
|
| 233 |
+
)
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
# --- Reactive result β fires on button click ---
|
| 237 |
+
cohort_result <- eventReactive(input$run_cohort, {
|
| 238 |
+
df <- get_data()
|
| 239 |
+
cohort_start <- as.Date(input$cohort_range[1])
|
| 240 |
+
cohort_end <- as.Date(input$cohort_range[2])
|
| 241 |
+
|
| 242 |
+
validate(
|
| 243 |
+
need(nrow(df) > 0, "No data available."),
|
| 244 |
+
need(cohort_start < cohort_end, "Cohort start must be before cohort end.")
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
sel <- input$estimator_filter
|
| 248 |
+
if (!is.null(sel) && length(sel) > 0) {
|
| 249 |
+
df <- df %>% filter(primary_name %in% sel)
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
compute_cohort(df, cohort_start, cohort_end, input$forward_window)
|
| 253 |
+
}, ignoreNULL = FALSE)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
# --- KPI Value Boxes ---
|
| 257 |
+
output$box_total_bid <- renderValueBox({
|
| 258 |
+
r <- cohort_result(); req(r)
|
| 259 |
+
valueBox(
|
| 260 |
+
value = dollar(sum(r$cohort_amt_bid, na.rm = TRUE), accuracy = 1),
|
| 261 |
+
subtitle = "Total Bid in Cohort",
|
| 262 |
+
icon = icon("file-invoice-dollar"),
|
| 263 |
+
color = "blue"
|
| 264 |
+
)
|
| 265 |
+
})
|
| 266 |
+
|
| 267 |
+
output$box_total_booked <- renderValueBox({
|
| 268 |
+
r <- cohort_result(); req(r)
|
| 269 |
+
valueBox(
|
| 270 |
+
value = dollar(sum(r$cohort_amt_booked, na.rm = TRUE), accuracy = 1),
|
| 271 |
+
subtitle = "Attributed Bookings",
|
| 272 |
+
icon = icon("check-circle"),
|
| 273 |
+
color = "green"
|
| 274 |
+
)
|
| 275 |
+
})
|
| 276 |
+
|
| 277 |
+
output$box_win_rate_amt <- renderValueBox({
|
| 278 |
+
r <- cohort_result(); req(r)
|
| 279 |
+
rate <- sum(r$cohort_amt_booked, na.rm = TRUE) /
|
| 280 |
+
sum(r$cohort_amt_bid, na.rm = TRUE)
|
| 281 |
+
valueBox(
|
| 282 |
+
value = percent(rate, accuracy = 0.1),
|
| 283 |
+
subtitle = HTML("Cohort Win Rate ($)<br><small style='font-size:10px;opacity:0.75;'>Forward-attributed</small>"),
|
| 284 |
+
icon = icon("trophy"),
|
| 285 |
+
color = "yellow"
|
| 286 |
+
)
|
| 287 |
+
})
|
| 288 |
+
|
| 289 |
+
output$box_win_rate_qty <- renderValueBox({
|
| 290 |
+
r <- cohort_result(); req(r)
|
| 291 |
+
rate <- sum(r$cohort_qty_booked, na.rm = TRUE) /
|
| 292 |
+
sum(r$cohort_qty_bid, na.rm = TRUE)
|
| 293 |
+
valueBox(
|
| 294 |
+
value = percent(rate, accuracy = 0.1),
|
| 295 |
+
subtitle = HTML("Cohort Win Rate (Jobs)<br><small style='font-size:10px;opacity:0.75;'>Forward-attributed</small>"),
|
| 296 |
+
icon = icon("hard-hat"),
|
| 297 |
+
color = "purple"
|
| 298 |
+
)
|
| 299 |
+
})
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
# --- Comparison Bar Chart ---
|
| 303 |
+
output$comparison_chart <- renderPlotly({
|
| 304 |
+
r <- cohort_result()
|
| 305 |
+
validate(need(!is.null(r) && nrow(r) > 0,
|
| 306 |
+
"Click 'Calculate Cohort' to run the analysis."))
|
| 307 |
+
|
| 308 |
+
plot_data <- r %>%
|
| 309 |
+
select(primary_name,
|
| 310 |
+
`Cohort (Forward)` = cohort_win_rate_amt,
|
| 311 |
+
`Same-Month` = sm_win_rate_amt) %>%
|
| 312 |
+
tidyr::pivot_longer(-primary_name,
|
| 313 |
+
names_to = "metric",
|
| 314 |
+
values_to = "win_rate") %>%
|
| 315 |
+
mutate(
|
| 316 |
+
primary_name = reorder(primary_name, win_rate, FUN = max),
|
| 317 |
+
win_rate = pmin(win_rate, 2) # cap display at 200%
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
p <- ggplot(plot_data,
|
| 321 |
+
aes(x = primary_name,
|
| 322 |
+
y = win_rate,
|
| 323 |
+
fill = metric,
|
| 324 |
+
text = paste0("<b>", primary_name, "</b><br>",
|
| 325 |
+
metric, ": ", percent(win_rate, 0.1)))) +
|
| 326 |
+
geom_col(position = position_dodge(width = 0.7),
|
| 327 |
+
width = 0.6, alpha = 0.9) +
|
| 328 |
+
geom_hline(yintercept = 1, linetype = "dashed",
|
| 329 |
+
color = "#95a5a6", linewidth = 0.5) +
|
| 330 |
+
scale_y_continuous(
|
| 331 |
+
labels = percent_format(accuracy = 1),
|
| 332 |
+
expand = expansion(mult = c(0, 0.12))
|
| 333 |
+
) +
|
| 334 |
+
scale_fill_manual(values = c("Cohort (Forward)" = "#3c8dbc",
|
| 335 |
+
"Same-Month" = "#f39c12")) +
|
| 336 |
+
coord_flip() +
|
| 337 |
+
labs(
|
| 338 |
+
x = NULL,
|
| 339 |
+
y = "Win Rate ($ Value)",
|
| 340 |
+
fill = NULL,
|
| 341 |
+
caption = paste0(
|
| 342 |
+
"Cohort: ", format(as.Date(input$cohort_range[1]), "%b %Y"),
|
| 343 |
+
" - ", format(as.Date(input$cohort_range[2]), "%b %Y"),
|
| 344 |
+
" | Attribution window: +", input$forward_window, " months"
|
| 345 |
+
)
|
| 346 |
+
) +
|
| 347 |
+
theme_minimal() +
|
| 348 |
+
theme(
|
| 349 |
+
legend.position = "top",
|
| 350 |
+
panel.grid.major.y = element_blank(),
|
| 351 |
+
plot.caption = element_text(size = 9, color = "#95a5a6")
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
ggplotly(p, tooltip = "text") %>%
|
| 355 |
+
layout(legend = list(orientation = "h", x = 0, y = 1.08))
|
| 356 |
+
})
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
# --- Sensitivity Curve ---
|
| 360 |
+
# Sweeps forward window 0β18 months; shows plateau = empirical lag
|
| 361 |
+
output$sensitivity_chart <- renderPlotly({
|
| 362 |
+
df <- get_data()
|
| 363 |
+
cohort_start <- as.Date(input$cohort_range[1])
|
| 364 |
+
cohort_end <- as.Date(input$cohort_range[2])
|
| 365 |
+
req(nrow(df) > 0, cohort_start, cohort_end)
|
| 366 |
+
|
| 367 |
+
sel <- input$estimator_filter
|
| 368 |
+
if (!is.null(sel) && length(sel) > 0) df <- df %>% filter(primary_name %in% sel)
|
| 369 |
+
|
| 370 |
+
sensitivity <- purrr::map_dfr(0:18, function(m) {
|
| 371 |
+
r <- compute_cohort(df, cohort_start, cohort_end, m)
|
| 372 |
+
tibble(
|
| 373 |
+
forward_months = m,
|
| 374 |
+
win_rate = sum(r$cohort_amt_booked, na.rm = TRUE) /
|
| 375 |
+
sum(r$cohort_amt_bid, na.rm = TRUE)
|
| 376 |
+
)
|
| 377 |
+
})
|
| 378 |
+
|
| 379 |
+
current_m <- input$forward_window
|
| 380 |
+
|
| 381 |
+
p <- ggplot(sensitivity,
|
| 382 |
+
aes(x = forward_months, y = win_rate)) +
|
| 383 |
+
geom_line(color = "#3c8dbc", linewidth = 1) +
|
| 384 |
+
geom_point(aes(text = paste0(forward_months, " mo: ",
|
| 385 |
+
percent(win_rate, 0.1))),
|
| 386 |
+
color = "#3c8dbc", size = 2) +
|
| 387 |
+
geom_vline(xintercept = current_m,
|
| 388 |
+
linetype = "dashed", color = "#f39c12", linewidth = 0.7) +
|
| 389 |
+
annotate("text",
|
| 390 |
+
x = current_m + 0.4,
|
| 391 |
+
y = max(sensitivity$win_rate, na.rm = TRUE) * 0.95,
|
| 392 |
+
label = paste0(current_m, " mo"),
|
| 393 |
+
color = "#f39c12", hjust = 0, size = 3.5) +
|
| 394 |
+
scale_x_continuous(breaks = seq(0, 18, 3)) +
|
| 395 |
+
scale_y_continuous(labels = percent_format(accuracy = 1)) +
|
| 396 |
+
labs(x = "Forward Window (months)", y = "Aggregate Win Rate") +
|
| 397 |
+
theme_minimal()
|
| 398 |
+
|
| 399 |
+
ggplotly(p, tooltip = "text")
|
| 400 |
+
})
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
# --- Detail Table ---
|
| 404 |
+
# FIXED: Replaced Unicode "\u2013" with standard hyphens "-" inside backticks.
|
| 405 |
+
output$cohort_table <- renderDataTable({
|
| 406 |
+
r <- cohort_result()
|
| 407 |
+
validate(need(!is.null(r) && nrow(r) > 0,
|
| 408 |
+
"Run the analysis to populate this table."))
|
| 409 |
+
|
| 410 |
+
display <- r %>%
|
| 411 |
+
arrange(desc(cohort_win_rate_amt)) %>%
|
| 412 |
+
transmute(
|
| 413 |
+
Estimator = primary_name,
|
| 414 |
+
`Bid ($)` = dollar(cohort_amt_bid, accuracy = 1),
|
| 415 |
+
`Bid (Jobs)` = cohort_qty_bid,
|
| 416 |
+
`Booked - Cohort ($)` = dollar(cohort_amt_booked, accuracy = 1),
|
| 417 |
+
`Booked - Cohort (Jobs)` = cohort_qty_booked,
|
| 418 |
+
`Win Rate - Cohort` = percent(cohort_win_rate_amt, accuracy = 0.1),
|
| 419 |
+
`Win Rate - Same Mo.` = percent(sm_win_rate_amt, accuracy = 0.1),
|
| 420 |
+
`Lag Gap` = percent(lag_gap_amt, accuracy = 0.1)
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
datatable(
|
| 424 |
+
display,
|
| 425 |
+
rownames = FALSE,
|
| 426 |
+
options = list(
|
| 427 |
+
pageLength = 15,
|
| 428 |
+
dom = "tp",
|
| 429 |
+
scrollX = TRUE,
|
| 430 |
+
order = list(list(5, "desc"))
|
| 431 |
+
)
|
| 432 |
+
) %>%
|
| 433 |
+
formatStyle(
|
| 434 |
+
"Lag Gap",
|
| 435 |
+
color = styleInterval(
|
| 436 |
+
c(-0.001, 0.001),
|
| 437 |
+
c("#c0392b", "#888888", "#27ae60")
|
| 438 |
+
)
|
| 439 |
+
)
|
| 440 |
+
})
|
| 441 |
+
|
| 442 |
+
})
|
| 443 |
+
}
|
renv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
renv/.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
library/
|
| 2 |
+
local/
|
| 3 |
+
cellar/
|
| 4 |
+
lock/
|
| 5 |
+
python/
|
| 6 |
+
sandbox/
|
| 7 |
+
staging/
|
renv/activate.R
ADDED
|
@@ -0,0 +1,1419 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
local({
|
| 3 |
+
|
| 4 |
+
# the requested version of renv
|
| 5 |
+
version <- "1.1.8"
|
| 6 |
+
attr(version, "md5") <- "cbffd086c66739a0fdaac7a30b4aa65c"
|
| 7 |
+
attr(version, "sha") <- NULL
|
| 8 |
+
|
| 9 |
+
# the project directory
|
| 10 |
+
project <- Sys.getenv("RENV_PROJECT")
|
| 11 |
+
if (!nzchar(project))
|
| 12 |
+
project <- getwd()
|
| 13 |
+
|
| 14 |
+
# use start-up diagnostics if enabled
|
| 15 |
+
diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE")
|
| 16 |
+
if (diagnostics) {
|
| 17 |
+
start <- Sys.time()
|
| 18 |
+
profile <- tempfile("renv-startup-", fileext = ".Rprof")
|
| 19 |
+
utils::Rprof(profile)
|
| 20 |
+
on.exit({
|
| 21 |
+
utils::Rprof(NULL)
|
| 22 |
+
elapsed <- signif(difftime(Sys.time(), start, units = "auto"), digits = 2L)
|
| 23 |
+
writeLines(sprintf("- renv took %s to run the autoloader.", format(elapsed)))
|
| 24 |
+
writeLines(sprintf("- Profile: %s", profile))
|
| 25 |
+
print(utils::summaryRprof(profile))
|
| 26 |
+
}, add = TRUE)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# figure out whether the autoloader is enabled
|
| 30 |
+
enabled <- local({
|
| 31 |
+
|
| 32 |
+
# first, check config option
|
| 33 |
+
override <- getOption("renv.config.autoloader.enabled")
|
| 34 |
+
if (!is.null(override))
|
| 35 |
+
return(override)
|
| 36 |
+
|
| 37 |
+
# if we're being run in a context where R_LIBS is already set,
|
| 38 |
+
# don't load -- presumably we're being run as a sub-process and
|
| 39 |
+
# the parent process has already set up library paths for us
|
| 40 |
+
rcmd <- Sys.getenv("R_CMD", unset = NA)
|
| 41 |
+
rlibs <- Sys.getenv("R_LIBS", unset = NA)
|
| 42 |
+
if (!is.na(rlibs) && !is.na(rcmd))
|
| 43 |
+
return(FALSE)
|
| 44 |
+
|
| 45 |
+
# next, check environment variables
|
| 46 |
+
# prefer using the configuration one in the future
|
| 47 |
+
envvars <- c(
|
| 48 |
+
"RENV_CONFIG_AUTOLOADER_ENABLED",
|
| 49 |
+
"RENV_AUTOLOADER_ENABLED",
|
| 50 |
+
"RENV_ACTIVATE_PROJECT"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
for (envvar in envvars) {
|
| 54 |
+
envval <- Sys.getenv(envvar, unset = NA)
|
| 55 |
+
if (!is.na(envval))
|
| 56 |
+
return(tolower(envval) %in% c("true", "t", "1"))
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# enable by default
|
| 60 |
+
TRUE
|
| 61 |
+
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# bail if we're not enabled
|
| 65 |
+
if (!enabled) {
|
| 66 |
+
|
| 67 |
+
# if we're not enabled, we might still need to manually load
|
| 68 |
+
# the user profile here
|
| 69 |
+
profile <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile")
|
| 70 |
+
if (file.exists(profile)) {
|
| 71 |
+
cfg <- Sys.getenv("RENV_CONFIG_USER_PROFILE", unset = "TRUE")
|
| 72 |
+
if (tolower(cfg) %in% c("true", "t", "1"))
|
| 73 |
+
sys.source(profile, envir = globalenv())
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return(FALSE)
|
| 77 |
+
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
# avoid recursion
|
| 81 |
+
if (identical(getOption("renv.autoloader.running"), TRUE)) {
|
| 82 |
+
warning("ignoring recursive attempt to run renv autoloader")
|
| 83 |
+
return(invisible(TRUE))
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# signal that we're loading renv during R startup
|
| 87 |
+
options(renv.autoloader.running = TRUE)
|
| 88 |
+
on.exit(options(renv.autoloader.running = NULL), add = TRUE)
|
| 89 |
+
|
| 90 |
+
# signal that we've consented to use renv
|
| 91 |
+
options(renv.consent = TRUE)
|
| 92 |
+
|
| 93 |
+
# load the 'utils' package eagerly -- this ensures that renv shims, which
|
| 94 |
+
# mask 'utils' packages, will come first on the search path
|
| 95 |
+
library(utils, lib.loc = .Library)
|
| 96 |
+
|
| 97 |
+
# unload renv if it's already been loaded
|
| 98 |
+
if ("renv" %in% loadedNamespaces())
|
| 99 |
+
unloadNamespace("renv")
|
| 100 |
+
|
| 101 |
+
# load bootstrap tools
|
| 102 |
+
ansify <- function(text) {
|
| 103 |
+
if (renv_ansify_enabled())
|
| 104 |
+
renv_ansify_enhanced(text)
|
| 105 |
+
else
|
| 106 |
+
renv_ansify_default(text)
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
renv_ansify_enabled <- function() {
|
| 110 |
+
|
| 111 |
+
override <- Sys.getenv("RENV_ANSIFY_ENABLED", unset = NA)
|
| 112 |
+
if (!is.na(override))
|
| 113 |
+
return(as.logical(override))
|
| 114 |
+
|
| 115 |
+
pane <- Sys.getenv("RSTUDIO_CHILD_PROCESS_PANE", unset = NA)
|
| 116 |
+
if (identical(pane, "build"))
|
| 117 |
+
return(FALSE)
|
| 118 |
+
|
| 119 |
+
testthat <- Sys.getenv("TESTTHAT", unset = "false")
|
| 120 |
+
if (tolower(testthat) %in% "true")
|
| 121 |
+
return(FALSE)
|
| 122 |
+
|
| 123 |
+
iderun <- Sys.getenv("R_CLI_HAS_HYPERLINK_IDE_RUN", unset = "false")
|
| 124 |
+
if (tolower(iderun) %in% "false")
|
| 125 |
+
return(FALSE)
|
| 126 |
+
|
| 127 |
+
TRUE
|
| 128 |
+
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
renv_ansify_default <- function(text) {
|
| 132 |
+
text
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
renv_ansify_enhanced <- function(text) {
|
| 136 |
+
|
| 137 |
+
# R help links
|
| 138 |
+
pattern <- "`\\?(renv::(?:[^`])+)`"
|
| 139 |
+
replacement <- "`\033]8;;x-r-help:\\1\a?\\1\033]8;;\a`"
|
| 140 |
+
text <- gsub(pattern, replacement, text, perl = TRUE)
|
| 141 |
+
|
| 142 |
+
# runnable code
|
| 143 |
+
pattern <- "`(renv::(?:[^`])+)`"
|
| 144 |
+
replacement <- "`\033]8;;x-r-run:\\1\a\\1\033]8;;\a`"
|
| 145 |
+
text <- gsub(pattern, replacement, text, perl = TRUE)
|
| 146 |
+
|
| 147 |
+
# return ansified text
|
| 148 |
+
text
|
| 149 |
+
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
renv_ansify_init <- function() {
|
| 153 |
+
|
| 154 |
+
envir <- renv_envir_self()
|
| 155 |
+
if (renv_ansify_enabled())
|
| 156 |
+
assign("ansify", renv_ansify_enhanced, envir = envir)
|
| 157 |
+
else
|
| 158 |
+
assign("ansify", renv_ansify_default, envir = envir)
|
| 159 |
+
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
`%||%` <- function(x, y) {
|
| 163 |
+
if (is.null(x)) y else x
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
catf <- function(fmt, ..., appendLF = TRUE) {
|
| 167 |
+
|
| 168 |
+
quiet <- getOption("renv.bootstrap.quiet", default = FALSE)
|
| 169 |
+
if (quiet)
|
| 170 |
+
return(invisible())
|
| 171 |
+
|
| 172 |
+
# also check for config environment variables that should suppress messages
|
| 173 |
+
# https://github.com/rstudio/renv/issues/2214
|
| 174 |
+
enabled <- Sys.getenv("RENV_CONFIG_STARTUP_QUIET", unset = NA)
|
| 175 |
+
if (!is.na(enabled) && tolower(enabled) %in% c("true", "1"))
|
| 176 |
+
return(invisible())
|
| 177 |
+
|
| 178 |
+
enabled <- Sys.getenv("RENV_CONFIG_SYNCHRONIZED_CHECK", unset = NA)
|
| 179 |
+
if (!is.na(enabled) && tolower(enabled) %in% c("false", "0"))
|
| 180 |
+
return(invisible())
|
| 181 |
+
|
| 182 |
+
msg <- sprintf(fmt, ...)
|
| 183 |
+
cat(msg, file = stdout(), sep = if (appendLF) "\n" else "")
|
| 184 |
+
|
| 185 |
+
invisible(msg)
|
| 186 |
+
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
header <- function(label,
|
| 190 |
+
...,
|
| 191 |
+
prefix = "#",
|
| 192 |
+
suffix = "-",
|
| 193 |
+
n = min(getOption("width"), 78))
|
| 194 |
+
{
|
| 195 |
+
label <- sprintf(label, ...)
|
| 196 |
+
n <- max(n - nchar(label) - nchar(prefix) - 2L, 8L)
|
| 197 |
+
if (n <= 0)
|
| 198 |
+
return(paste(prefix, label))
|
| 199 |
+
|
| 200 |
+
tail <- paste(rep.int(suffix, n), collapse = "")
|
| 201 |
+
paste0(prefix, " ", label, " ", tail)
|
| 202 |
+
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
heredoc <- function(text, leave = 0) {
|
| 206 |
+
|
| 207 |
+
# remove leading, trailing whitespace
|
| 208 |
+
trimmed <- gsub("^\\s*\\n|\\n\\s*$", "", text)
|
| 209 |
+
|
| 210 |
+
# split into lines
|
| 211 |
+
lines <- strsplit(trimmed, "\n", fixed = TRUE)[[1L]]
|
| 212 |
+
|
| 213 |
+
# compute common indent
|
| 214 |
+
indent <- regexpr("[^[:space:]]", lines)
|
| 215 |
+
common <- min(setdiff(indent, -1L)) - leave
|
| 216 |
+
text <- paste(substring(lines, common), collapse = "\n")
|
| 217 |
+
|
| 218 |
+
# substitute in ANSI links for executable renv code
|
| 219 |
+
ansify(text)
|
| 220 |
+
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
bootstrap <- function(version, library) {
|
| 224 |
+
|
| 225 |
+
friendly <- renv_bootstrap_version_friendly(version)
|
| 226 |
+
section <- header(sprintf("Bootstrapping renv %s", friendly))
|
| 227 |
+
catf(section)
|
| 228 |
+
|
| 229 |
+
# try to install renv from cache
|
| 230 |
+
md5 <- attr(version, "md5", exact = TRUE)
|
| 231 |
+
if (length(md5)) {
|
| 232 |
+
pkgpath <- renv_bootstrap_find(version)
|
| 233 |
+
if (length(pkgpath) && file.exists(pkgpath)) {
|
| 234 |
+
file.copy(pkgpath, library, recursive = TRUE)
|
| 235 |
+
return(invisible())
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
# attempt to download renv
|
| 240 |
+
catf("- Downloading renv ... ", appendLF = FALSE)
|
| 241 |
+
withCallingHandlers(
|
| 242 |
+
tarball <- renv_bootstrap_download(version),
|
| 243 |
+
error = function(err) {
|
| 244 |
+
catf("FAILED")
|
| 245 |
+
stop("failed to download:\n", conditionMessage(err))
|
| 246 |
+
}
|
| 247 |
+
)
|
| 248 |
+
catf("OK")
|
| 249 |
+
on.exit(unlink(tarball), add = TRUE)
|
| 250 |
+
|
| 251 |
+
# now attempt to install
|
| 252 |
+
catf("- Installing renv ... ", appendLF = FALSE)
|
| 253 |
+
withCallingHandlers(
|
| 254 |
+
status <- renv_bootstrap_install(version, tarball, library),
|
| 255 |
+
error = function(err) {
|
| 256 |
+
catf("FAILED")
|
| 257 |
+
stop("failed to install:\n", conditionMessage(err))
|
| 258 |
+
}
|
| 259 |
+
)
|
| 260 |
+
catf("OK")
|
| 261 |
+
|
| 262 |
+
# add empty line to break up bootstrapping from normal output
|
| 263 |
+
catf("")
|
| 264 |
+
return(invisible())
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
renv_bootstrap_tests_running <- function() {
|
| 268 |
+
getOption("renv.tests.running", default = FALSE)
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
renv_bootstrap_repos <- function() {
|
| 272 |
+
|
| 273 |
+
# get CRAN repository
|
| 274 |
+
cran <- getOption("renv.repos.cran", "https://cloud.r-project.org")
|
| 275 |
+
|
| 276 |
+
# check for repos override
|
| 277 |
+
repos <- Sys.getenv("RENV_CONFIG_REPOS_OVERRIDE", unset = NA)
|
| 278 |
+
if (!is.na(repos)) {
|
| 279 |
+
|
| 280 |
+
# split on ';' if present
|
| 281 |
+
parts <- strsplit(repos, ";", fixed = TRUE)[[1L]]
|
| 282 |
+
|
| 283 |
+
# split into named repositories if present
|
| 284 |
+
idx <- regexpr("=", parts, fixed = TRUE)
|
| 285 |
+
keys <- substring(parts, 1L, idx - 1L)
|
| 286 |
+
vals <- substring(parts, idx + 1L)
|
| 287 |
+
names(vals) <- keys
|
| 288 |
+
|
| 289 |
+
# if we have a single unnamed repository, call it CRAN
|
| 290 |
+
if (length(vals) == 1L && identical(keys, ""))
|
| 291 |
+
names(vals) <- "CRAN"
|
| 292 |
+
|
| 293 |
+
return(vals)
|
| 294 |
+
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
# check for lockfile repositories
|
| 298 |
+
repos <- tryCatch(renv_bootstrap_repos_lockfile(), error = identity)
|
| 299 |
+
if (!inherits(repos, "error") && length(repos))
|
| 300 |
+
return(repos)
|
| 301 |
+
|
| 302 |
+
# retrieve current repos
|
| 303 |
+
repos <- getOption("repos")
|
| 304 |
+
|
| 305 |
+
# ensure @CRAN@ entries are resolved
|
| 306 |
+
repos[repos == "@CRAN@"] <- cran
|
| 307 |
+
|
| 308 |
+
# add in renv.bootstrap.repos if set
|
| 309 |
+
default <- c(FALLBACK = "https://cloud.r-project.org")
|
| 310 |
+
extra <- getOption("renv.bootstrap.repos", default = default)
|
| 311 |
+
repos <- c(repos, extra)
|
| 312 |
+
|
| 313 |
+
# remove duplicates that might've snuck in
|
| 314 |
+
dupes <- duplicated(repos) | duplicated(names(repos))
|
| 315 |
+
repos[!dupes]
|
| 316 |
+
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
renv_bootstrap_repos_lockfile <- function() {
|
| 320 |
+
|
| 321 |
+
lockpath <- Sys.getenv("RENV_PATHS_LOCKFILE", unset = "renv.lock")
|
| 322 |
+
if (!file.exists(lockpath))
|
| 323 |
+
return(NULL)
|
| 324 |
+
|
| 325 |
+
lockfile <- tryCatch(renv_json_read(lockpath), error = identity)
|
| 326 |
+
if (inherits(lockfile, "error")) {
|
| 327 |
+
warning(lockfile)
|
| 328 |
+
return(NULL)
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
repos <- lockfile$R$Repositories
|
| 332 |
+
if (length(repos) == 0)
|
| 333 |
+
return(NULL)
|
| 334 |
+
|
| 335 |
+
keys <- vapply(repos, `[[`, "Name", FUN.VALUE = character(1))
|
| 336 |
+
vals <- vapply(repos, `[[`, "URL", FUN.VALUE = character(1))
|
| 337 |
+
names(vals) <- keys
|
| 338 |
+
|
| 339 |
+
return(vals)
|
| 340 |
+
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
renv_bootstrap_download <- function(version) {
|
| 344 |
+
|
| 345 |
+
sha <- attr(version, "sha", exact = TRUE)
|
| 346 |
+
|
| 347 |
+
methods <- if (!is.null(sha)) {
|
| 348 |
+
|
| 349 |
+
# attempting to bootstrap a development version of renv
|
| 350 |
+
c(
|
| 351 |
+
function() renv_bootstrap_download_tarball(sha),
|
| 352 |
+
function() renv_bootstrap_download_github(sha)
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
} else {
|
| 356 |
+
|
| 357 |
+
# attempting to bootstrap a release version of renv
|
| 358 |
+
c(
|
| 359 |
+
function() renv_bootstrap_download_tarball(version),
|
| 360 |
+
function() renv_bootstrap_download_cran_latest(version),
|
| 361 |
+
function() renv_bootstrap_download_cran_archive(version)
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
for (method in methods) {
|
| 367 |
+
path <- tryCatch(method(), error = identity)
|
| 368 |
+
if (is.character(path) && file.exists(path))
|
| 369 |
+
return(path)
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
stop("All download methods failed")
|
| 373 |
+
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
renv_bootstrap_download_impl <- function(url, destfile) {
|
| 377 |
+
|
| 378 |
+
mode <- "wb"
|
| 379 |
+
|
| 380 |
+
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17715
|
| 381 |
+
fixup <-
|
| 382 |
+
Sys.info()[["sysname"]] == "Windows" &&
|
| 383 |
+
substring(url, 1L, 5L) == "file:"
|
| 384 |
+
|
| 385 |
+
if (fixup)
|
| 386 |
+
mode <- "w+b"
|
| 387 |
+
|
| 388 |
+
args <- list(
|
| 389 |
+
url = url,
|
| 390 |
+
destfile = destfile,
|
| 391 |
+
mode = mode,
|
| 392 |
+
quiet = TRUE
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
if ("headers" %in% names(formals(utils::download.file))) {
|
| 396 |
+
headers <- renv_bootstrap_download_custom_headers(url)
|
| 397 |
+
if (length(headers) && is.character(headers))
|
| 398 |
+
args$headers <- headers
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
do.call(utils::download.file, args)
|
| 402 |
+
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
renv_bootstrap_download_custom_headers <- function(url) {
|
| 406 |
+
|
| 407 |
+
headers <- getOption("renv.download.headers")
|
| 408 |
+
if (is.null(headers))
|
| 409 |
+
return(character())
|
| 410 |
+
|
| 411 |
+
if (!is.function(headers))
|
| 412 |
+
stopf("'renv.download.headers' is not a function")
|
| 413 |
+
|
| 414 |
+
headers <- headers(url)
|
| 415 |
+
if (length(headers) == 0L)
|
| 416 |
+
return(character())
|
| 417 |
+
|
| 418 |
+
if (is.list(headers))
|
| 419 |
+
headers <- unlist(headers, recursive = FALSE, use.names = TRUE)
|
| 420 |
+
|
| 421 |
+
ok <-
|
| 422 |
+
is.character(headers) &&
|
| 423 |
+
is.character(names(headers)) &&
|
| 424 |
+
all(nzchar(names(headers)))
|
| 425 |
+
|
| 426 |
+
if (!ok)
|
| 427 |
+
stop("invocation of 'renv.download.headers' did not return a named character vector")
|
| 428 |
+
|
| 429 |
+
headers
|
| 430 |
+
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
renv_bootstrap_download_cran_latest <- function(version) {
|
| 434 |
+
|
| 435 |
+
spec <- renv_bootstrap_download_cran_latest_find(version)
|
| 436 |
+
type <- spec$type
|
| 437 |
+
repos <- spec$repos
|
| 438 |
+
|
| 439 |
+
baseurl <- utils::contrib.url(repos = repos, type = type)
|
| 440 |
+
ext <- if (identical(type, "source"))
|
| 441 |
+
".tar.gz"
|
| 442 |
+
else if (Sys.info()[["sysname"]] == "Windows")
|
| 443 |
+
".zip"
|
| 444 |
+
else
|
| 445 |
+
".tgz"
|
| 446 |
+
name <- sprintf("renv_%s%s", version, ext)
|
| 447 |
+
url <- paste(baseurl, name, sep = "/")
|
| 448 |
+
|
| 449 |
+
destfile <- file.path(tempdir(), name)
|
| 450 |
+
status <- tryCatch(
|
| 451 |
+
renv_bootstrap_download_impl(url, destfile),
|
| 452 |
+
condition = identity
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
if (inherits(status, "condition"))
|
| 456 |
+
return(FALSE)
|
| 457 |
+
|
| 458 |
+
# report success and return
|
| 459 |
+
destfile
|
| 460 |
+
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
renv_bootstrap_download_cran_latest_find <- function(version) {
|
| 464 |
+
|
| 465 |
+
# check whether binaries are supported on this system
|
| 466 |
+
binary <-
|
| 467 |
+
getOption("renv.bootstrap.binary", default = TRUE) &&
|
| 468 |
+
!identical(.Platform$pkgType, "source") &&
|
| 469 |
+
!identical(getOption("pkgType"), "source") &&
|
| 470 |
+
Sys.info()[["sysname"]] %in% c("Darwin", "Windows")
|
| 471 |
+
|
| 472 |
+
types <- c(if (binary) "binary", "source")
|
| 473 |
+
|
| 474 |
+
# iterate over types + repositories
|
| 475 |
+
for (type in types) {
|
| 476 |
+
for (repos in renv_bootstrap_repos()) {
|
| 477 |
+
|
| 478 |
+
# build arguments for utils::available.packages() call
|
| 479 |
+
args <- list(type = type, repos = repos)
|
| 480 |
+
|
| 481 |
+
# add custom headers if available -- note that
|
| 482 |
+
# utils::available.packages() will pass this to download.file()
|
| 483 |
+
if ("headers" %in% names(formals(utils::download.file))) {
|
| 484 |
+
headers <- renv_bootstrap_download_custom_headers(repos)
|
| 485 |
+
if (length(headers) && is.character(headers))
|
| 486 |
+
args$headers <- headers
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
# retrieve package database
|
| 490 |
+
db <- tryCatch(
|
| 491 |
+
as.data.frame(
|
| 492 |
+
do.call(utils::available.packages, args),
|
| 493 |
+
stringsAsFactors = FALSE
|
| 494 |
+
),
|
| 495 |
+
error = identity
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
if (inherits(db, "error"))
|
| 499 |
+
next
|
| 500 |
+
|
| 501 |
+
# check for compatible entry
|
| 502 |
+
entry <- db[db$Package %in% "renv" & db$Version %in% version, ]
|
| 503 |
+
if (nrow(entry) == 0)
|
| 504 |
+
next
|
| 505 |
+
|
| 506 |
+
# found it; return spec to caller
|
| 507 |
+
spec <- list(entry = entry, type = type, repos = repos)
|
| 508 |
+
return(spec)
|
| 509 |
+
|
| 510 |
+
}
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
# if we got here, we failed to find renv
|
| 514 |
+
fmt <- "renv %s is not available from your declared package repositories"
|
| 515 |
+
stop(sprintf(fmt, version))
|
| 516 |
+
|
| 517 |
+
}
|
| 518 |
+
|
| 519 |
+
renv_bootstrap_download_cran_archive <- function(version) {
|
| 520 |
+
|
| 521 |
+
name <- sprintf("renv_%s.tar.gz", version)
|
| 522 |
+
repos <- renv_bootstrap_repos()
|
| 523 |
+
urls <- file.path(repos, "src/contrib/Archive/renv", name)
|
| 524 |
+
destfile <- file.path(tempdir(), name)
|
| 525 |
+
|
| 526 |
+
for (url in urls) {
|
| 527 |
+
|
| 528 |
+
status <- tryCatch(
|
| 529 |
+
renv_bootstrap_download_impl(url, destfile),
|
| 530 |
+
condition = identity
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
if (identical(status, 0L))
|
| 534 |
+
return(destfile)
|
| 535 |
+
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
return(FALSE)
|
| 539 |
+
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
renv_bootstrap_find <- function(version) {
|
| 543 |
+
|
| 544 |
+
path <- renv_bootstrap_find_cache(version)
|
| 545 |
+
if (length(path) && file.exists(path)) {
|
| 546 |
+
catf("- Using renv %s from global package cache", version)
|
| 547 |
+
return(path)
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
renv_bootstrap_find_cache <- function(version) {
|
| 553 |
+
|
| 554 |
+
md5 <- attr(version, "md5", exact = TRUE)
|
| 555 |
+
if (is.null(md5))
|
| 556 |
+
return()
|
| 557 |
+
|
| 558 |
+
# infer path to renv cache
|
| 559 |
+
cache <- Sys.getenv("RENV_PATHS_CACHE", unset = "")
|
| 560 |
+
if (!nzchar(cache)) {
|
| 561 |
+
root <- Sys.getenv("RENV_PATHS_ROOT", unset = NA)
|
| 562 |
+
if (!is.na(root))
|
| 563 |
+
cache <- file.path(root, "cache")
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
if (!nzchar(cache)) {
|
| 567 |
+
tools <- asNamespace("tools")
|
| 568 |
+
if (is.function(tools$R_user_dir)) {
|
| 569 |
+
root <- tools$R_user_dir("renv", "cache")
|
| 570 |
+
cache <- file.path(root, "cache")
|
| 571 |
+
}
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
# start completing path to cache
|
| 575 |
+
file.path(
|
| 576 |
+
cache,
|
| 577 |
+
renv_bootstrap_cache_version(),
|
| 578 |
+
renv_bootstrap_platform_prefix(),
|
| 579 |
+
"renv",
|
| 580 |
+
version,
|
| 581 |
+
md5,
|
| 582 |
+
"renv"
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
renv_bootstrap_download_tarball <- function(version) {
|
| 588 |
+
|
| 589 |
+
# if the user has provided the path to a tarball via
|
| 590 |
+
# an environment variable, then use it
|
| 591 |
+
tarball <- Sys.getenv("RENV_BOOTSTRAP_TARBALL", unset = NA)
|
| 592 |
+
if (is.na(tarball))
|
| 593 |
+
return()
|
| 594 |
+
|
| 595 |
+
# allow directories
|
| 596 |
+
if (dir.exists(tarball)) {
|
| 597 |
+
name <- sprintf("renv_%s.tar.gz", version)
|
| 598 |
+
tarball <- file.path(tarball, name)
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
# bail if it doesn't exist
|
| 602 |
+
if (!file.exists(tarball)) {
|
| 603 |
+
|
| 604 |
+
# let the user know we weren't able to honour their request
|
| 605 |
+
fmt <- "- RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist."
|
| 606 |
+
msg <- sprintf(fmt, tarball)
|
| 607 |
+
warning(msg)
|
| 608 |
+
|
| 609 |
+
# bail
|
| 610 |
+
return()
|
| 611 |
+
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
catf("- Using local tarball '%s'.", tarball)
|
| 615 |
+
tarball
|
| 616 |
+
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
renv_bootstrap_github_token <- function() {
|
| 620 |
+
for (envvar in c("GITHUB_TOKEN", "GITHUB_PAT", "GH_TOKEN")) {
|
| 621 |
+
envval <- Sys.getenv(envvar, unset = NA)
|
| 622 |
+
if (!is.na(envval))
|
| 623 |
+
return(envval)
|
| 624 |
+
}
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
renv_bootstrap_download_github <- function(version) {
|
| 628 |
+
|
| 629 |
+
enabled <- Sys.getenv("RENV_BOOTSTRAP_FROM_GITHUB", unset = "TRUE")
|
| 630 |
+
if (!identical(enabled, "TRUE"))
|
| 631 |
+
return(FALSE)
|
| 632 |
+
|
| 633 |
+
# prepare download options
|
| 634 |
+
token <- renv_bootstrap_github_token()
|
| 635 |
+
if (is.null(token))
|
| 636 |
+
token <- ""
|
| 637 |
+
|
| 638 |
+
if (nzchar(Sys.which("curl")) && nzchar(token)) {
|
| 639 |
+
fmt <- "--location --fail --header \"Authorization: token %s\""
|
| 640 |
+
extra <- sprintf(fmt, token)
|
| 641 |
+
saved <- options("download.file.method", "download.file.extra")
|
| 642 |
+
options(download.file.method = "curl", download.file.extra = extra)
|
| 643 |
+
on.exit(do.call(base::options, saved), add = TRUE)
|
| 644 |
+
} else if (nzchar(Sys.which("wget")) && nzchar(token)) {
|
| 645 |
+
fmt <- "--header=\"Authorization: token %s\""
|
| 646 |
+
extra <- sprintf(fmt, token)
|
| 647 |
+
saved <- options("download.file.method", "download.file.extra")
|
| 648 |
+
options(download.file.method = "wget", download.file.extra = extra)
|
| 649 |
+
on.exit(do.call(base::options, saved), add = TRUE)
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version)
|
| 653 |
+
name <- sprintf("renv_%s.tar.gz", version)
|
| 654 |
+
destfile <- file.path(tempdir(), name)
|
| 655 |
+
|
| 656 |
+
status <- tryCatch(
|
| 657 |
+
renv_bootstrap_download_impl(url, destfile),
|
| 658 |
+
condition = identity
|
| 659 |
+
)
|
| 660 |
+
|
| 661 |
+
if (!identical(status, 0L))
|
| 662 |
+
return(FALSE)
|
| 663 |
+
|
| 664 |
+
renv_bootstrap_download_augment(destfile)
|
| 665 |
+
|
| 666 |
+
return(destfile)
|
| 667 |
+
|
| 668 |
+
}
|
| 669 |
+
|
| 670 |
+
# Add Sha to DESCRIPTION. This is stop gap until #890, after which we
|
| 671 |
+
# can use renv::install() to fully capture metadata.
|
| 672 |
+
renv_bootstrap_download_augment <- function(destfile) {
|
| 673 |
+
sha <- renv_bootstrap_git_extract_sha1_tar(destfile)
|
| 674 |
+
if (is.null(sha)) {
|
| 675 |
+
return()
|
| 676 |
+
}
|
| 677 |
+
|
| 678 |
+
# Untar
|
| 679 |
+
tempdir <- tempfile("renv-github-")
|
| 680 |
+
on.exit(unlink(tempdir, recursive = TRUE), add = TRUE)
|
| 681 |
+
untar(destfile, exdir = tempdir)
|
| 682 |
+
pkgdir <- dir(tempdir, full.names = TRUE)[[1]]
|
| 683 |
+
|
| 684 |
+
# Modify description
|
| 685 |
+
desc_path <- file.path(pkgdir, "DESCRIPTION")
|
| 686 |
+
desc_lines <- readLines(desc_path)
|
| 687 |
+
remotes_fields <- c(
|
| 688 |
+
"RemoteType: github",
|
| 689 |
+
"RemoteHost: api.github.com",
|
| 690 |
+
"RemoteRepo: renv",
|
| 691 |
+
"RemoteUsername: rstudio",
|
| 692 |
+
"RemotePkgRef: rstudio/renv",
|
| 693 |
+
paste("RemoteRef: ", sha),
|
| 694 |
+
paste("RemoteSha: ", sha)
|
| 695 |
+
)
|
| 696 |
+
writeLines(c(desc_lines[desc_lines != ""], remotes_fields), con = desc_path)
|
| 697 |
+
|
| 698 |
+
# Re-tar
|
| 699 |
+
local({
|
| 700 |
+
old <- setwd(tempdir)
|
| 701 |
+
on.exit(setwd(old), add = TRUE)
|
| 702 |
+
|
| 703 |
+
tar(destfile, compression = "gzip")
|
| 704 |
+
})
|
| 705 |
+
invisible()
|
| 706 |
+
}
|
| 707 |
+
|
| 708 |
+
# Extract the commit hash from a git archive. Git archives include the SHA1
|
| 709 |
+
# hash as the comment field of the tarball pax extended header
|
| 710 |
+
# (see https://www.kernel.org/pub/software/scm/git/docs/git-archive.html)
|
| 711 |
+
# For GitHub archives this should be the first header after the default one
|
| 712 |
+
# (512 byte) header.
|
| 713 |
+
renv_bootstrap_git_extract_sha1_tar <- function(bundle) {
|
| 714 |
+
|
| 715 |
+
# open the bundle for reading
|
| 716 |
+
# We use gzcon for everything because (from ?gzcon)
|
| 717 |
+
# > Reading from a connection which does not supply a 'gzip' magic
|
| 718 |
+
# > header is equivalent to reading from the original connection
|
| 719 |
+
conn <- gzcon(file(bundle, open = "rb", raw = TRUE))
|
| 720 |
+
on.exit(close(conn))
|
| 721 |
+
|
| 722 |
+
# The default pax header is 512 bytes long and the first pax extended header
|
| 723 |
+
# with the comment should be 51 bytes long
|
| 724 |
+
# `52 comment=` (11 chars) + 40 byte SHA1 hash
|
| 725 |
+
len <- 0x200 + 0x33
|
| 726 |
+
res <- rawToChar(readBin(conn, "raw", n = len)[0x201:len])
|
| 727 |
+
|
| 728 |
+
if (grepl("^52 comment=", res)) {
|
| 729 |
+
sub("52 comment=", "", res)
|
| 730 |
+
} else {
|
| 731 |
+
NULL
|
| 732 |
+
}
|
| 733 |
+
}
|
| 734 |
+
|
| 735 |
+
renv_bootstrap_install <- function(version, tarball, library) {
|
| 736 |
+
|
| 737 |
+
# attempt to install it into project library
|
| 738 |
+
dir.create(library, showWarnings = FALSE, recursive = TRUE)
|
| 739 |
+
output <- renv_bootstrap_install_impl(library, tarball)
|
| 740 |
+
|
| 741 |
+
# check for successful install
|
| 742 |
+
status <- attr(output, "status")
|
| 743 |
+
if (is.null(status) || identical(status, 0L))
|
| 744 |
+
return(status)
|
| 745 |
+
|
| 746 |
+
# an error occurred; report it
|
| 747 |
+
header <- "installation of renv failed"
|
| 748 |
+
lines <- paste(rep.int("=", nchar(header)), collapse = "")
|
| 749 |
+
text <- paste(c(header, lines, output), collapse = "\n")
|
| 750 |
+
stop(text)
|
| 751 |
+
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
renv_bootstrap_install_impl <- function(library, tarball) {
|
| 755 |
+
|
| 756 |
+
# invoke using system2 so we can capture and report output
|
| 757 |
+
bin <- R.home("bin")
|
| 758 |
+
exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
|
| 759 |
+
R <- file.path(bin, exe)
|
| 760 |
+
|
| 761 |
+
args <- c(
|
| 762 |
+
"--vanilla", "CMD", "INSTALL", "--no-multiarch",
|
| 763 |
+
"-l", shQuote(path.expand(library)),
|
| 764 |
+
shQuote(path.expand(tarball))
|
| 765 |
+
)
|
| 766 |
+
|
| 767 |
+
system2(R, args, stdout = TRUE, stderr = TRUE)
|
| 768 |
+
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
renv_bootstrap_platform_prefix_default <- function() {
|
| 772 |
+
|
| 773 |
+
# read version component
|
| 774 |
+
version <- Sys.getenv("RENV_PATHS_VERSION", unset = "R-%v")
|
| 775 |
+
|
| 776 |
+
# expand placeholders
|
| 777 |
+
placeholders <- list(
|
| 778 |
+
list("%v", format(getRversion()[1, 1:2])),
|
| 779 |
+
list("%V", format(getRversion()[1, 1:3]))
|
| 780 |
+
)
|
| 781 |
+
|
| 782 |
+
for (placeholder in placeholders)
|
| 783 |
+
version <- gsub(placeholder[[1L]], placeholder[[2L]], version, fixed = TRUE)
|
| 784 |
+
|
| 785 |
+
# include SVN revision for development versions of R
|
| 786 |
+
# (to avoid sharing platform-specific artefacts with released versions of R)
|
| 787 |
+
devel <-
|
| 788 |
+
identical(R.version[["status"]], "Under development (unstable)") ||
|
| 789 |
+
identical(R.version[["nickname"]], "Unsuffered Consequences")
|
| 790 |
+
|
| 791 |
+
if (devel)
|
| 792 |
+
version <- paste(version, R.version[["svn rev"]], sep = "-r")
|
| 793 |
+
|
| 794 |
+
version
|
| 795 |
+
|
| 796 |
+
}
|
| 797 |
+
|
| 798 |
+
renv_bootstrap_platform_prefix <- function() {
|
| 799 |
+
|
| 800 |
+
# construct version prefix
|
| 801 |
+
version <- renv_bootstrap_platform_prefix_default()
|
| 802 |
+
|
| 803 |
+
# build list of path components
|
| 804 |
+
components <- c(version, R.version$platform)
|
| 805 |
+
|
| 806 |
+
# include prefix if provided by user
|
| 807 |
+
prefix <- renv_bootstrap_platform_prefix_impl()
|
| 808 |
+
if (!is.na(prefix) && nzchar(prefix))
|
| 809 |
+
components <- c(prefix, components)
|
| 810 |
+
|
| 811 |
+
# build prefix
|
| 812 |
+
paste(components, collapse = "/")
|
| 813 |
+
|
| 814 |
+
}
|
| 815 |
+
|
| 816 |
+
renv_bootstrap_platform_prefix_impl <- function() {
|
| 817 |
+
|
| 818 |
+
# if an explicit prefix has been supplied, use it
|
| 819 |
+
prefix <- Sys.getenv("RENV_PATHS_PREFIX", unset = NA)
|
| 820 |
+
if (!is.na(prefix))
|
| 821 |
+
return(prefix)
|
| 822 |
+
|
| 823 |
+
# if the user has requested an automatic prefix, generate it
|
| 824 |
+
auto <- Sys.getenv("RENV_PATHS_PREFIX_AUTO", unset = NA)
|
| 825 |
+
if (is.na(auto) && getRversion() >= "4.4.0")
|
| 826 |
+
auto <- "TRUE"
|
| 827 |
+
|
| 828 |
+
if (auto %in% c("TRUE", "True", "true", "1"))
|
| 829 |
+
return(renv_bootstrap_platform_prefix_auto())
|
| 830 |
+
|
| 831 |
+
# empty string on failure
|
| 832 |
+
""
|
| 833 |
+
|
| 834 |
+
}
|
| 835 |
+
|
| 836 |
+
renv_bootstrap_platform_prefix_auto <- function() {
|
| 837 |
+
|
| 838 |
+
prefix <- tryCatch(renv_bootstrap_platform_os(), error = identity)
|
| 839 |
+
if (inherits(prefix, "error") || prefix %in% "unknown") {
|
| 840 |
+
|
| 841 |
+
msg <- paste(
|
| 842 |
+
"failed to infer current operating system",
|
| 843 |
+
"please file a bug report at https://github.com/rstudio/renv/issues",
|
| 844 |
+
sep = "; "
|
| 845 |
+
)
|
| 846 |
+
|
| 847 |
+
warning(msg)
|
| 848 |
+
|
| 849 |
+
}
|
| 850 |
+
|
| 851 |
+
prefix
|
| 852 |
+
|
| 853 |
+
}
|
| 854 |
+
|
| 855 |
+
renv_bootstrap_platform_os <- function() {
|
| 856 |
+
|
| 857 |
+
sysinfo <- Sys.info()
|
| 858 |
+
sysname <- sysinfo[["sysname"]]
|
| 859 |
+
|
| 860 |
+
# handle Windows + macOS up front
|
| 861 |
+
if (sysname == "Windows")
|
| 862 |
+
return("windows")
|
| 863 |
+
else if (sysname == "Darwin")
|
| 864 |
+
return("macos")
|
| 865 |
+
|
| 866 |
+
# check for os-release files
|
| 867 |
+
for (file in c("/etc/os-release", "/usr/lib/os-release"))
|
| 868 |
+
if (file.exists(file))
|
| 869 |
+
return(renv_bootstrap_platform_os_via_os_release(file, sysinfo))
|
| 870 |
+
|
| 871 |
+
# check for redhat-release files
|
| 872 |
+
if (file.exists("/etc/redhat-release"))
|
| 873 |
+
return(renv_bootstrap_platform_os_via_redhat_release())
|
| 874 |
+
|
| 875 |
+
"unknown"
|
| 876 |
+
|
| 877 |
+
}
|
| 878 |
+
|
| 879 |
+
renv_bootstrap_platform_os_via_os_release <- function(file, sysinfo) {
|
| 880 |
+
|
| 881 |
+
# read /etc/os-release
|
| 882 |
+
release <- utils::read.table(
|
| 883 |
+
file = file,
|
| 884 |
+
sep = "=",
|
| 885 |
+
quote = c("\"", "'"),
|
| 886 |
+
col.names = c("Key", "Value"),
|
| 887 |
+
comment.char = "#",
|
| 888 |
+
stringsAsFactors = FALSE
|
| 889 |
+
)
|
| 890 |
+
|
| 891 |
+
vars <- as.list(release$Value)
|
| 892 |
+
names(vars) <- release$Key
|
| 893 |
+
|
| 894 |
+
# get os name
|
| 895 |
+
os <- tolower(sysinfo[["sysname"]])
|
| 896 |
+
|
| 897 |
+
# read id
|
| 898 |
+
id <- "unknown"
|
| 899 |
+
for (field in c("ID", "ID_LIKE")) {
|
| 900 |
+
if (field %in% names(vars) && nzchar(vars[[field]])) {
|
| 901 |
+
id <- vars[[field]]
|
| 902 |
+
break
|
| 903 |
+
}
|
| 904 |
+
}
|
| 905 |
+
|
| 906 |
+
# read version
|
| 907 |
+
version <- "unknown"
|
| 908 |
+
for (field in c("UBUNTU_CODENAME", "VERSION_CODENAME", "VERSION_ID", "BUILD_ID")) {
|
| 909 |
+
if (field %in% names(vars) && nzchar(vars[[field]])) {
|
| 910 |
+
version <- vars[[field]]
|
| 911 |
+
break
|
| 912 |
+
}
|
| 913 |
+
}
|
| 914 |
+
|
| 915 |
+
# join together
|
| 916 |
+
paste(c(os, id, version), collapse = "-")
|
| 917 |
+
|
| 918 |
+
}
|
| 919 |
+
|
| 920 |
+
renv_bootstrap_platform_os_via_redhat_release <- function() {
|
| 921 |
+
|
| 922 |
+
# read /etc/redhat-release
|
| 923 |
+
contents <- readLines("/etc/redhat-release", warn = FALSE)
|
| 924 |
+
|
| 925 |
+
# infer id
|
| 926 |
+
id <- if (grepl("centos", contents, ignore.case = TRUE))
|
| 927 |
+
"centos"
|
| 928 |
+
else if (grepl("redhat", contents, ignore.case = TRUE))
|
| 929 |
+
"redhat"
|
| 930 |
+
else
|
| 931 |
+
"unknown"
|
| 932 |
+
|
| 933 |
+
# try to find a version component (very hacky)
|
| 934 |
+
version <- "unknown"
|
| 935 |
+
|
| 936 |
+
parts <- strsplit(contents, "[[:space:]]")[[1L]]
|
| 937 |
+
for (part in parts) {
|
| 938 |
+
|
| 939 |
+
nv <- tryCatch(numeric_version(part), error = identity)
|
| 940 |
+
if (inherits(nv, "error"))
|
| 941 |
+
next
|
| 942 |
+
|
| 943 |
+
version <- nv[1, 1]
|
| 944 |
+
break
|
| 945 |
+
|
| 946 |
+
}
|
| 947 |
+
|
| 948 |
+
paste(c("linux", id, version), collapse = "-")
|
| 949 |
+
|
| 950 |
+
}
|
| 951 |
+
|
| 952 |
+
renv_bootstrap_library_root_name <- function(project) {
|
| 953 |
+
|
| 954 |
+
# use project name as-is if requested
|
| 955 |
+
asis <- Sys.getenv("RENV_PATHS_LIBRARY_ROOT_ASIS", unset = "FALSE")
|
| 956 |
+
if (asis)
|
| 957 |
+
return(basename(project))
|
| 958 |
+
|
| 959 |
+
# otherwise, disambiguate based on project's path
|
| 960 |
+
id <- substring(renv_bootstrap_hash_text(project), 1L, 8L)
|
| 961 |
+
paste(basename(project), id, sep = "-")
|
| 962 |
+
|
| 963 |
+
}
|
| 964 |
+
|
| 965 |
+
renv_bootstrap_library_root <- function(project) {
|
| 966 |
+
|
| 967 |
+
prefix <- renv_bootstrap_profile_prefix()
|
| 968 |
+
|
| 969 |
+
path <- Sys.getenv("RENV_PATHS_LIBRARY", unset = NA)
|
| 970 |
+
if (!is.na(path))
|
| 971 |
+
return(paste(c(path, prefix), collapse = "/"))
|
| 972 |
+
|
| 973 |
+
path <- renv_bootstrap_library_root_impl(project)
|
| 974 |
+
if (!is.null(path)) {
|
| 975 |
+
name <- renv_bootstrap_library_root_name(project)
|
| 976 |
+
return(paste(c(path, prefix, name), collapse = "/"))
|
| 977 |
+
}
|
| 978 |
+
|
| 979 |
+
renv_bootstrap_paths_renv("library", project = project)
|
| 980 |
+
|
| 981 |
+
}
|
| 982 |
+
|
| 983 |
+
renv_bootstrap_library_root_impl <- function(project) {
|
| 984 |
+
|
| 985 |
+
root <- Sys.getenv("RENV_PATHS_LIBRARY_ROOT", unset = NA)
|
| 986 |
+
if (!is.na(root))
|
| 987 |
+
return(root)
|
| 988 |
+
|
| 989 |
+
type <- renv_bootstrap_project_type(project)
|
| 990 |
+
if (identical(type, "package")) {
|
| 991 |
+
userdir <- renv_bootstrap_user_dir()
|
| 992 |
+
return(file.path(userdir, "library"))
|
| 993 |
+
}
|
| 994 |
+
|
| 995 |
+
}
|
| 996 |
+
|
| 997 |
+
renv_bootstrap_validate_version <- function(version, description = NULL) {
|
| 998 |
+
|
| 999 |
+
# resolve description file
|
| 1000 |
+
#
|
| 1001 |
+
# avoid passing lib.loc to `packageDescription()` below, since R will
|
| 1002 |
+
# use the loaded version of the package by default anyhow. note that
|
| 1003 |
+
# this function should only be called after 'renv' is loaded
|
| 1004 |
+
# https://github.com/rstudio/renv/issues/1625
|
| 1005 |
+
description <- description %||% packageDescription("renv")
|
| 1006 |
+
|
| 1007 |
+
# check whether requested version 'version' matches loaded version of renv
|
| 1008 |
+
sha <- attr(version, "sha", exact = TRUE)
|
| 1009 |
+
valid <- if (!is.null(sha))
|
| 1010 |
+
renv_bootstrap_validate_version_dev(sha, description)
|
| 1011 |
+
else
|
| 1012 |
+
renv_bootstrap_validate_version_release(version, description)
|
| 1013 |
+
|
| 1014 |
+
if (valid)
|
| 1015 |
+
return(TRUE)
|
| 1016 |
+
|
| 1017 |
+
# the loaded version of renv doesn't match the requested version;
|
| 1018 |
+
# give the user instructions on how to proceed
|
| 1019 |
+
dev <- identical(description[["RemoteType"]], "github")
|
| 1020 |
+
remote <- if (dev)
|
| 1021 |
+
paste("rstudio/renv", description[["RemoteSha"]], sep = "@")
|
| 1022 |
+
else
|
| 1023 |
+
paste("renv", description[["Version"]], sep = "@")
|
| 1024 |
+
|
| 1025 |
+
# display both loaded version + sha if available
|
| 1026 |
+
friendly <- renv_bootstrap_version_friendly(
|
| 1027 |
+
version = description[["Version"]],
|
| 1028 |
+
sha = if (dev) description[["RemoteSha"]]
|
| 1029 |
+
)
|
| 1030 |
+
|
| 1031 |
+
fmt <- heredoc("
|
| 1032 |
+
renv %1$s was loaded from project library, but this project is configured to use renv %2$s.
|
| 1033 |
+
- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.
|
| 1034 |
+
- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.
|
| 1035 |
+
")
|
| 1036 |
+
catf(fmt, friendly, renv_bootstrap_version_friendly(version), remote)
|
| 1037 |
+
|
| 1038 |
+
FALSE
|
| 1039 |
+
|
| 1040 |
+
}
|
| 1041 |
+
|
| 1042 |
+
renv_bootstrap_validate_version_dev <- function(version, description) {
|
| 1043 |
+
|
| 1044 |
+
expected <- description[["RemoteSha"]]
|
| 1045 |
+
if (!is.character(expected))
|
| 1046 |
+
return(FALSE)
|
| 1047 |
+
|
| 1048 |
+
pattern <- sprintf("^\\Q%s\\E", version)
|
| 1049 |
+
grepl(pattern, expected, perl = TRUE)
|
| 1050 |
+
|
| 1051 |
+
}
|
| 1052 |
+
|
| 1053 |
+
renv_bootstrap_validate_version_release <- function(version, description) {
|
| 1054 |
+
expected <- description[["Version"]]
|
| 1055 |
+
is.character(expected) && identical(c(expected), c(version))
|
| 1056 |
+
}
|
| 1057 |
+
|
| 1058 |
+
renv_bootstrap_hash_text <- function(text) {
|
| 1059 |
+
|
| 1060 |
+
hashfile <- tempfile("renv-hash-")
|
| 1061 |
+
on.exit(unlink(hashfile), add = TRUE)
|
| 1062 |
+
|
| 1063 |
+
writeLines(text, con = hashfile)
|
| 1064 |
+
tools::md5sum(hashfile)
|
| 1065 |
+
|
| 1066 |
+
}
|
| 1067 |
+
|
| 1068 |
+
renv_bootstrap_load <- function(project, libpath, version) {
|
| 1069 |
+
|
| 1070 |
+
# try to load renv from the project library
|
| 1071 |
+
if (!requireNamespace("renv", lib.loc = libpath, quietly = TRUE))
|
| 1072 |
+
return(FALSE)
|
| 1073 |
+
|
| 1074 |
+
# warn if the version of renv loaded does not match
|
| 1075 |
+
renv_bootstrap_validate_version(version)
|
| 1076 |
+
|
| 1077 |
+
# execute renv load hooks, if any
|
| 1078 |
+
hooks <- getHook("renv::autoload")
|
| 1079 |
+
for (hook in hooks)
|
| 1080 |
+
if (is.function(hook))
|
| 1081 |
+
tryCatch(hook(), error = warnify)
|
| 1082 |
+
|
| 1083 |
+
# load the project
|
| 1084 |
+
renv::load(project)
|
| 1085 |
+
|
| 1086 |
+
TRUE
|
| 1087 |
+
|
| 1088 |
+
}
|
| 1089 |
+
|
| 1090 |
+
renv_bootstrap_profile_load <- function(project) {
|
| 1091 |
+
|
| 1092 |
+
# if RENV_PROFILE is already set, just use that
|
| 1093 |
+
profile <- Sys.getenv("RENV_PROFILE", unset = NA)
|
| 1094 |
+
if (!is.na(profile) && nzchar(profile))
|
| 1095 |
+
return(profile)
|
| 1096 |
+
|
| 1097 |
+
# check for a profile file (nothing to do if it doesn't exist)
|
| 1098 |
+
path <- renv_bootstrap_paths_renv("profile", profile = FALSE, project = project)
|
| 1099 |
+
if (!file.exists(path))
|
| 1100 |
+
return(NULL)
|
| 1101 |
+
|
| 1102 |
+
# read the profile, and set it if it exists
|
| 1103 |
+
contents <- readLines(path, warn = FALSE)
|
| 1104 |
+
if (length(contents) == 0L)
|
| 1105 |
+
return(NULL)
|
| 1106 |
+
|
| 1107 |
+
# set RENV_PROFILE
|
| 1108 |
+
profile <- contents[[1L]]
|
| 1109 |
+
if (!profile %in% c("", "default"))
|
| 1110 |
+
Sys.setenv(RENV_PROFILE = profile)
|
| 1111 |
+
|
| 1112 |
+
profile
|
| 1113 |
+
|
| 1114 |
+
}
|
| 1115 |
+
|
| 1116 |
+
renv_bootstrap_profile_prefix <- function() {
|
| 1117 |
+
profile <- renv_bootstrap_profile_get()
|
| 1118 |
+
if (!is.null(profile))
|
| 1119 |
+
return(file.path("profiles", profile, "renv"))
|
| 1120 |
+
}
|
| 1121 |
+
|
| 1122 |
+
renv_bootstrap_profile_get <- function() {
|
| 1123 |
+
profile <- Sys.getenv("RENV_PROFILE", unset = "")
|
| 1124 |
+
renv_bootstrap_profile_normalize(profile)
|
| 1125 |
+
}
|
| 1126 |
+
|
| 1127 |
+
renv_bootstrap_profile_set <- function(profile) {
|
| 1128 |
+
profile <- renv_bootstrap_profile_normalize(profile)
|
| 1129 |
+
if (is.null(profile))
|
| 1130 |
+
Sys.unsetenv("RENV_PROFILE")
|
| 1131 |
+
else
|
| 1132 |
+
Sys.setenv(RENV_PROFILE = profile)
|
| 1133 |
+
}
|
| 1134 |
+
|
| 1135 |
+
renv_bootstrap_profile_normalize <- function(profile) {
|
| 1136 |
+
|
| 1137 |
+
if (is.null(profile) || profile %in% c("", "default"))
|
| 1138 |
+
return(NULL)
|
| 1139 |
+
|
| 1140 |
+
profile
|
| 1141 |
+
|
| 1142 |
+
}
|
| 1143 |
+
|
| 1144 |
+
renv_bootstrap_path_absolute <- function(path) {
|
| 1145 |
+
|
| 1146 |
+
substr(path, 1L, 1L) %in% c("~", "/", "\\") || (
|
| 1147 |
+
substr(path, 1L, 1L) %in% c(letters, LETTERS) &&
|
| 1148 |
+
substr(path, 2L, 3L) %in% c(":/", ":\\")
|
| 1149 |
+
)
|
| 1150 |
+
|
| 1151 |
+
}
|
| 1152 |
+
|
| 1153 |
+
renv_bootstrap_paths_renv <- function(..., profile = TRUE, project = NULL) {
|
| 1154 |
+
renv <- Sys.getenv("RENV_PATHS_RENV", unset = "renv")
|
| 1155 |
+
root <- if (renv_bootstrap_path_absolute(renv)) NULL else project
|
| 1156 |
+
prefix <- if (profile) renv_bootstrap_profile_prefix()
|
| 1157 |
+
components <- c(root, renv, prefix, ...)
|
| 1158 |
+
paste(components, collapse = "/")
|
| 1159 |
+
}
|
| 1160 |
+
|
| 1161 |
+
renv_bootstrap_project_type <- function(path) {
|
| 1162 |
+
|
| 1163 |
+
descpath <- file.path(path, "DESCRIPTION")
|
| 1164 |
+
if (!file.exists(descpath))
|
| 1165 |
+
return("unknown")
|
| 1166 |
+
|
| 1167 |
+
desc <- tryCatch(
|
| 1168 |
+
read.dcf(descpath, all = TRUE),
|
| 1169 |
+
error = identity
|
| 1170 |
+
)
|
| 1171 |
+
|
| 1172 |
+
if (inherits(desc, "error"))
|
| 1173 |
+
return("unknown")
|
| 1174 |
+
|
| 1175 |
+
type <- desc$Type
|
| 1176 |
+
if (!is.null(type))
|
| 1177 |
+
return(tolower(type))
|
| 1178 |
+
|
| 1179 |
+
package <- desc$Package
|
| 1180 |
+
if (!is.null(package))
|
| 1181 |
+
return("package")
|
| 1182 |
+
|
| 1183 |
+
"unknown"
|
| 1184 |
+
|
| 1185 |
+
}
|
| 1186 |
+
|
| 1187 |
+
renv_bootstrap_user_dir <- function() {
|
| 1188 |
+
dir <- renv_bootstrap_user_dir_impl()
|
| 1189 |
+
path.expand(chartr("\\", "/", dir))
|
| 1190 |
+
}
|
| 1191 |
+
|
| 1192 |
+
renv_bootstrap_user_dir_impl <- function() {
|
| 1193 |
+
|
| 1194 |
+
# use local override if set
|
| 1195 |
+
override <- getOption("renv.userdir.override")
|
| 1196 |
+
if (!is.null(override))
|
| 1197 |
+
return(override)
|
| 1198 |
+
|
| 1199 |
+
# use R_user_dir if available
|
| 1200 |
+
tools <- asNamespace("tools")
|
| 1201 |
+
if (is.function(tools$R_user_dir))
|
| 1202 |
+
return(tools$R_user_dir("renv", "cache"))
|
| 1203 |
+
|
| 1204 |
+
# try using our own backfill for older versions of R
|
| 1205 |
+
envvars <- c("R_USER_CACHE_DIR", "XDG_CACHE_HOME")
|
| 1206 |
+
for (envvar in envvars) {
|
| 1207 |
+
root <- Sys.getenv(envvar, unset = NA)
|
| 1208 |
+
if (!is.na(root))
|
| 1209 |
+
return(file.path(root, "R/renv"))
|
| 1210 |
+
}
|
| 1211 |
+
|
| 1212 |
+
# use platform-specific default fallbacks
|
| 1213 |
+
if (Sys.info()[["sysname"]] == "Windows")
|
| 1214 |
+
file.path(Sys.getenv("LOCALAPPDATA"), "R/cache/R/renv")
|
| 1215 |
+
else if (Sys.info()[["sysname"]] == "Darwin")
|
| 1216 |
+
"~/Library/Caches/org.R-project.R/R/renv"
|
| 1217 |
+
else
|
| 1218 |
+
"~/.cache/R/renv"
|
| 1219 |
+
|
| 1220 |
+
}
|
| 1221 |
+
|
| 1222 |
+
renv_bootstrap_version_friendly <- function(version, shafmt = NULL, sha = NULL) {
|
| 1223 |
+
sha <- sha %||% attr(version, "sha", exact = TRUE)
|
| 1224 |
+
parts <- c(version, sprintf(shafmt %||% " [sha: %s]", substring(sha, 1L, 7L)))
|
| 1225 |
+
paste(parts, collapse = "")
|
| 1226 |
+
}
|
| 1227 |
+
|
| 1228 |
+
renv_bootstrap_exec <- function(project, libpath, version) {
|
| 1229 |
+
if (!renv_bootstrap_load(project, libpath, version))
|
| 1230 |
+
renv_bootstrap_run(project, libpath, version)
|
| 1231 |
+
}
|
| 1232 |
+
|
| 1233 |
+
renv_bootstrap_run <- function(project, libpath, version) {
|
| 1234 |
+
|
| 1235 |
+
# perform bootstrap
|
| 1236 |
+
bootstrap(version, libpath)
|
| 1237 |
+
|
| 1238 |
+
# exit early if we're just testing bootstrap
|
| 1239 |
+
if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA)))
|
| 1240 |
+
return(TRUE)
|
| 1241 |
+
|
| 1242 |
+
# try again to load
|
| 1243 |
+
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
|
| 1244 |
+
return(renv::load(project = project))
|
| 1245 |
+
}
|
| 1246 |
+
|
| 1247 |
+
# failed to download or load renv; warn the user
|
| 1248 |
+
msg <- c(
|
| 1249 |
+
"Failed to find an renv installation: the project will not be loaded.",
|
| 1250 |
+
"Use `renv::activate()` to re-initialize the project."
|
| 1251 |
+
)
|
| 1252 |
+
|
| 1253 |
+
warning(paste(msg, collapse = "\n"), call. = FALSE)
|
| 1254 |
+
|
| 1255 |
+
}
|
| 1256 |
+
|
| 1257 |
+
renv_bootstrap_cache_version <- function() {
|
| 1258 |
+
# NOTE: users should normally not override the cache version;
|
| 1259 |
+
# this is provided just to make testing easier
|
| 1260 |
+
Sys.getenv("RENV_CACHE_VERSION", unset = "v5")
|
| 1261 |
+
}
|
| 1262 |
+
|
| 1263 |
+
renv_bootstrap_cache_version_previous <- function() {
|
| 1264 |
+
version <- renv_bootstrap_cache_version()
|
| 1265 |
+
number <- as.integer(substring(version, 2L))
|
| 1266 |
+
paste("v", number - 1L, sep = "")
|
| 1267 |
+
}
|
| 1268 |
+
|
| 1269 |
+
renv_json_read <- function(file = NULL, text = NULL) {
|
| 1270 |
+
|
| 1271 |
+
jlerr <- NULL
|
| 1272 |
+
|
| 1273 |
+
# if jsonlite is loaded, use that instead
|
| 1274 |
+
if ("jsonlite" %in% loadedNamespaces()) {
|
| 1275 |
+
|
| 1276 |
+
json <- tryCatch(renv_json_read_jsonlite(file, text), error = identity)
|
| 1277 |
+
if (!inherits(json, "error"))
|
| 1278 |
+
return(json)
|
| 1279 |
+
|
| 1280 |
+
jlerr <- json
|
| 1281 |
+
|
| 1282 |
+
}
|
| 1283 |
+
|
| 1284 |
+
# otherwise, fall back to the default JSON reader
|
| 1285 |
+
json <- tryCatch(renv_json_read_default(file, text), error = identity)
|
| 1286 |
+
if (!inherits(json, "error"))
|
| 1287 |
+
return(json)
|
| 1288 |
+
|
| 1289 |
+
# report an error
|
| 1290 |
+
if (!is.null(jlerr))
|
| 1291 |
+
stop(jlerr)
|
| 1292 |
+
else
|
| 1293 |
+
stop(json)
|
| 1294 |
+
|
| 1295 |
+
}
|
| 1296 |
+
|
| 1297 |
+
renv_json_read_jsonlite <- function(file = NULL, text = NULL) {
|
| 1298 |
+
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
|
| 1299 |
+
jsonlite::fromJSON(txt = text, simplifyVector = FALSE)
|
| 1300 |
+
}
|
| 1301 |
+
|
| 1302 |
+
renv_json_read_patterns <- function() {
|
| 1303 |
+
|
| 1304 |
+
list(
|
| 1305 |
+
|
| 1306 |
+
# objects
|
| 1307 |
+
list("{", "\t\n\tobject(\t\n\t", TRUE),
|
| 1308 |
+
list("}", "\t\n\t)\t\n\t", TRUE),
|
| 1309 |
+
|
| 1310 |
+
# arrays
|
| 1311 |
+
list("[", "\t\n\tarray(\t\n\t", TRUE),
|
| 1312 |
+
list("]", "\n\t\n)\n\t\n", TRUE),
|
| 1313 |
+
|
| 1314 |
+
# maps
|
| 1315 |
+
list(":", "\t\n\t=\t\n\t", TRUE),
|
| 1316 |
+
|
| 1317 |
+
# newlines
|
| 1318 |
+
list("\\u000a", "\n", FALSE)
|
| 1319 |
+
|
| 1320 |
+
)
|
| 1321 |
+
|
| 1322 |
+
}
|
| 1323 |
+
|
| 1324 |
+
renv_json_read_envir <- function() {
|
| 1325 |
+
|
| 1326 |
+
envir <- new.env(parent = emptyenv())
|
| 1327 |
+
|
| 1328 |
+
envir[["+"]] <- `+`
|
| 1329 |
+
envir[["-"]] <- `-`
|
| 1330 |
+
|
| 1331 |
+
envir[["object"]] <- function(...) {
|
| 1332 |
+
result <- list(...)
|
| 1333 |
+
names(result) <- as.character(names(result))
|
| 1334 |
+
result
|
| 1335 |
+
}
|
| 1336 |
+
|
| 1337 |
+
envir[["array"]] <- list
|
| 1338 |
+
|
| 1339 |
+
envir[["true"]] <- TRUE
|
| 1340 |
+
envir[["false"]] <- FALSE
|
| 1341 |
+
envir[["null"]] <- NULL
|
| 1342 |
+
|
| 1343 |
+
envir
|
| 1344 |
+
|
| 1345 |
+
}
|
| 1346 |
+
|
| 1347 |
+
renv_json_read_remap <- function(object, patterns) {
|
| 1348 |
+
|
| 1349 |
+
# repair names if necessary
|
| 1350 |
+
if (!is.null(names(object))) {
|
| 1351 |
+
|
| 1352 |
+
nms <- names(object)
|
| 1353 |
+
for (pattern in patterns)
|
| 1354 |
+
nms <- gsub(pattern[[2L]], pattern[[1L]], nms, fixed = TRUE)
|
| 1355 |
+
names(object) <- nms
|
| 1356 |
+
|
| 1357 |
+
}
|
| 1358 |
+
|
| 1359 |
+
# repair strings if necessary
|
| 1360 |
+
if (is.character(object)) {
|
| 1361 |
+
for (pattern in patterns)
|
| 1362 |
+
object <- gsub(pattern[[2L]], pattern[[1L]], object, fixed = TRUE)
|
| 1363 |
+
}
|
| 1364 |
+
|
| 1365 |
+
# recurse for other objects
|
| 1366 |
+
if (is.recursive(object))
|
| 1367 |
+
for (i in seq_along(object))
|
| 1368 |
+
object[i] <- list(renv_json_read_remap(object[[i]], patterns))
|
| 1369 |
+
|
| 1370 |
+
# return remapped object
|
| 1371 |
+
object
|
| 1372 |
+
|
| 1373 |
+
}
|
| 1374 |
+
|
| 1375 |
+
renv_json_read_default <- function(file = NULL, text = NULL) {
|
| 1376 |
+
|
| 1377 |
+
# read json text
|
| 1378 |
+
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
|
| 1379 |
+
|
| 1380 |
+
# convert into something the R parser will understand
|
| 1381 |
+
patterns <- renv_json_read_patterns()
|
| 1382 |
+
transformed <- text
|
| 1383 |
+
for (pattern in patterns)
|
| 1384 |
+
transformed <- gsub(pattern[[1L]], pattern[[2L]], transformed, fixed = TRUE)
|
| 1385 |
+
|
| 1386 |
+
# parse it
|
| 1387 |
+
rfile <- tempfile("renv-json-", fileext = ".R")
|
| 1388 |
+
on.exit(unlink(rfile), add = TRUE)
|
| 1389 |
+
writeLines(transformed, con = rfile)
|
| 1390 |
+
json <- parse(rfile, keep.source = FALSE, srcfile = NULL)[[1L]]
|
| 1391 |
+
|
| 1392 |
+
# evaluate in safe environment
|
| 1393 |
+
result <- eval(json, envir = renv_json_read_envir())
|
| 1394 |
+
|
| 1395 |
+
# fix up strings if necessary -- do so only with reversible patterns
|
| 1396 |
+
patterns <- Filter(function(pattern) pattern[[3L]], patterns)
|
| 1397 |
+
renv_json_read_remap(result, patterns)
|
| 1398 |
+
|
| 1399 |
+
}
|
| 1400 |
+
|
| 1401 |
+
|
| 1402 |
+
# load the renv profile, if any
|
| 1403 |
+
renv_bootstrap_profile_load(project)
|
| 1404 |
+
|
| 1405 |
+
# construct path to library root
|
| 1406 |
+
root <- renv_bootstrap_library_root(project)
|
| 1407 |
+
|
| 1408 |
+
# construct library prefix for platform
|
| 1409 |
+
prefix <- renv_bootstrap_platform_prefix()
|
| 1410 |
+
|
| 1411 |
+
# construct full libpath
|
| 1412 |
+
libpath <- file.path(root, prefix)
|
| 1413 |
+
|
| 1414 |
+
# run bootstrap code
|
| 1415 |
+
renv_bootstrap_exec(project, libpath, version)
|
| 1416 |
+
|
| 1417 |
+
invisible()
|
| 1418 |
+
|
| 1419 |
+
})
|
scratch.R
ADDED
|
File without changes
|
server.R
CHANGED
|
@@ -88,6 +88,181 @@ function(input, output, session) {
|
|
| 88 |
result
|
| 89 |
})
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# ββ MAIN DROPDOWN UPDATERS ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 92 |
|
| 93 |
observe({
|
|
@@ -99,9 +274,6 @@ function(input, output, session) {
|
|
| 99 |
updatePickerInput(session, "estimatorSelect", choices = est_list, selected = isolate(input$estimatorSelect))
|
| 100 |
|
| 101 |
# ββ BUG FIX 1: its_estimator auto-selection βββββββββββββββββββββββββββ
|
| 102 |
-
# pickerInput returns character(0) (NOT NULL) when empty, so !is.null() always
|
| 103 |
-
# passed and selected = character(0) was being sent, leaving nothing pre-selected.
|
| 104 |
-
# Now we check length() > 0 AND that the current value is still a valid choice.
|
| 105 |
eligible <- df %>%
|
| 106 |
group_by(primary_name) %>%
|
| 107 |
summarise(n = n(), .groups = "drop") %>%
|
|
@@ -109,7 +281,7 @@ function(input, output, session) {
|
|
| 109 |
pull(primary_name) %>%
|
| 110 |
sort()
|
| 111 |
|
| 112 |
-
cur_its
|
| 113 |
selected_its <- if (length(cur_its) > 0 && cur_its %in% eligible) cur_its else eligible[1]
|
| 114 |
updatePickerInput(session, "its_estimator", choices = eligible, selected = selected_its)
|
| 115 |
# ββ END BUG FIX 1 ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -345,9 +517,6 @@ function(input, output, session) {
|
|
| 345 |
observeEvent(input$its_run, {
|
| 346 |
|
| 347 |
# ββ BUG FIX 2: tryCatch around its_series() call ββββββββββββββββββββββ
|
| 348 |
-
# req() inside its_series throws a silent condition that previously
|
| 349 |
-
# propagated here and caused the entire observeEvent to abort with zero
|
| 350 |
-
# user feedback. Now we catch that and display a helpful message instead.
|
| 351 |
series <- tryCatch(its_series(), error = function(e) NULL)
|
| 352 |
if (is.null(series)) {
|
| 353 |
its_result(list(error = "Please select an estimator from the dropdown, then click Run Analysis."))
|
|
@@ -375,10 +544,6 @@ function(input, output, session) {
|
|
| 375 |
bp_dates <- series$date_group[bp_idx]
|
| 376 |
|
| 377 |
# ββ BUG FIX 3: Fstats time-series subscripting ββββββββββββββββββ
|
| 378 |
-
# Fstats() returns a TRIMMED time series. bp_idx is a row position
|
| 379 |
-
# in series (e.g. 20), but fs$Fstats[20] returns the 20th element
|
| 380 |
-
# of the trimmed ts β not the stat at row 20. We now use time()
|
| 381 |
-
# to map bp_idx values onto the correct ts positions.
|
| 382 |
fs <- Fstats(value ~ t, data = series)
|
| 383 |
fs_times <- as.integer(time(fs$Fstats))
|
| 384 |
fs_vals <- as.numeric(fs$Fstats)
|
|
@@ -419,11 +584,6 @@ function(input, output, session) {
|
|
| 419 |
|
| 420 |
cf <- coeftest(fit)
|
| 421 |
|
| 422 |
-
# Build cf_df by indexing the coeftest matrix directly by column name.
|
| 423 |
-
# as.data.frame(cf) is unreliable β R mangles column names like
|
| 424 |
-
# "Std. Error" β "Std..Error" and "Pr(>|t|)" β "Pr...t.." depending
|
| 425 |
-
# on R/lmtest version, causing downstream mutate() to crash with
|
| 426 |
-
# "object 'Estimate' not found".
|
| 427 |
cf_df <- data.frame(
|
| 428 |
Term = recode(rownames(cf),
|
| 429 |
`(Intercept)` = "Intercept (baseline level)",
|
|
@@ -672,7 +832,7 @@ function(input, output, session) {
|
|
| 672 |
km <- kmeans(feats, centers = k, nstart = 25)
|
| 673 |
ds_df$Cluster <- as.factor(km$cluster)
|
| 674 |
cl_means <- ds_df %>% group_by(Cluster) %>% summarise(mean_wr = mean(Win_Rate, na.rm = TRUE), .groups = "drop") %>% arrange(desc(mean_wr))
|
| 675 |
-
arch_map <- setNames(c("High-Efficiency", "Selective", "
|
| 676 |
ds_df$Archetype <- arch_map[as.character(ds_df$Cluster)]
|
| 677 |
}
|
| 678 |
|
|
@@ -692,6 +852,169 @@ function(input, output, session) {
|
|
| 692 |
ggplotly(p, tooltip = "text")
|
| 693 |
})
|
| 694 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
# ββ ANNUAL, YOY, QUARTERLY ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 696 |
|
| 697 |
output$annual_bar_chart <- renderPlotly({
|
|
@@ -825,6 +1148,276 @@ function(input, output, session) {
|
|
| 825 |
datatable(lb, options = list(pageLength = 15, scrollX = TRUE, order = list(list(2, "desc"))), rownames = FALSE)
|
| 826 |
})
|
| 827 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 828 |
output$db_status_text <- renderText({ paste("Database records loaded:", comma(nrow(get_data()))) })
|
| 829 |
|
| 830 |
output$preview_table <- renderDataTable({
|
|
@@ -917,4 +1510,4 @@ function(input, output, session) {
|
|
| 917 |
})
|
| 918 |
}
|
| 919 |
)
|
| 920 |
-
}
|
|
|
|
| 88 |
result
|
| 89 |
})
|
| 90 |
|
| 91 |
+
ceo_scorecard <- reactive({
|
| 92 |
+
d <- filtered_data()
|
| 93 |
+
validate(need(nrow(d) > 0, "No data in selected range."))
|
| 94 |
+
|
| 95 |
+
monthly <- d %>%
|
| 96 |
+
mutate(period = floor_date(report_date, "month")) %>%
|
| 97 |
+
group_by(primary_name, period) %>%
|
| 98 |
+
summarise(
|
| 99 |
+
amt_bid = sum(amt_bid, na.rm = TRUE),
|
| 100 |
+
amt_booked = sum(amt_booked, na.rm = TRUE),
|
| 101 |
+
qty_bid = sum(qty_bid, na.rm = TRUE),
|
| 102 |
+
qty_booked = sum(qty_booked, na.rm = TRUE),
|
| 103 |
+
.groups = "drop"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
base <- monthly %>%
|
| 107 |
+
group_by(primary_name) %>%
|
| 108 |
+
summarise(
|
| 109 |
+
total_bid = sum(amt_bid, na.rm = TRUE),
|
| 110 |
+
total_booked = sum(amt_booked, na.rm = TRUE),
|
| 111 |
+
jobs_bid = sum(qty_bid, na.rm = TRUE),
|
| 112 |
+
jobs_won = sum(qty_booked, na.rm = TRUE),
|
| 113 |
+
same_month_win_rate = ifelse(total_bid > 0, total_booked / total_bid, NA_real_),
|
| 114 |
+
avg_job_size = ifelse(jobs_bid > 0, total_bid / jobs_bid, NA_real_),
|
| 115 |
+
months_active = n(),
|
| 116 |
+
booked_cv = ifelse(mean(amt_booked, na.rm = TRUE) > 0,
|
| 117 |
+
sd(amt_booked, na.rm = TRUE) / mean(amt_booked, na.rm = TRUE), NA_real_),
|
| 118 |
+
.groups = "drop"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
lwr <- lag_win_rate()
|
| 122 |
+
if (!is.null(lwr) && nrow(lwr) > 0) {
|
| 123 |
+
lag_summary <- lwr %>%
|
| 124 |
+
group_by(primary_name) %>%
|
| 125 |
+
summarise(lag_win_rate = sum(lag_booked_amt, na.rm = TRUE) / sum(amt_bid, na.rm = TRUE), .groups = "drop")
|
| 126 |
+
base <- base %>%
|
| 127 |
+
left_join(lag_summary, by = "primary_name") %>%
|
| 128 |
+
mutate(win_rate_used = coalesce(lag_win_rate, same_month_win_rate))
|
| 129 |
+
} else {
|
| 130 |
+
base <- base %>% mutate(lag_win_rate = NA_real_, win_rate_used = same_month_win_rate)
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
safe_rescale <- function(x) {
|
| 134 |
+
if (all(is.na(x))) return(rep(0.5, length(x)))
|
| 135 |
+
rng <- range(x, na.rm = TRUE)
|
| 136 |
+
if (isTRUE(all.equal(rng[1], rng[2]))) return(rep(0.5, length(x)))
|
| 137 |
+
(x - rng[1]) / (rng[2] - rng[1])
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
w_raw <- c(
|
| 141 |
+
win = input$ceo_w_win %||% 35,
|
| 142 |
+
eff = input$ceo_w_eff %||% 20,
|
| 143 |
+
stab = input$ceo_w_stab %||% 20,
|
| 144 |
+
out = input$ceo_w_out %||% 25
|
| 145 |
+
)
|
| 146 |
+
w_sum <- sum(w_raw, na.rm = TRUE)
|
| 147 |
+
if (is.na(w_sum) || w_sum <= 0) {
|
| 148 |
+
w <- c(win = 0.35, eff = 0.20, stab = 0.20, out = 0.25)
|
| 149 |
+
} else {
|
| 150 |
+
w <- w_raw / w_sum
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
risk_win_cut <- (input$ceo_risk_win %||% 12) / 100
|
| 154 |
+
risk_cv_cut <- input$ceo_risk_cv %||% 1.25
|
| 155 |
+
|
| 156 |
+
base %>%
|
| 157 |
+
mutate(
|
| 158 |
+
s_win = safe_rescale(win_rate_used),
|
| 159 |
+
s_efficiency = safe_rescale(ifelse(total_bid > 0, total_booked / total_bid, NA_real_)),
|
| 160 |
+
s_stability = 1 - safe_rescale(booked_cv),
|
| 161 |
+
s_output = safe_rescale(total_booked),
|
| 162 |
+
composite_score = 100 * (w["win"] * s_win + w["eff"] * s_efficiency + w["stab"] * s_stability + w["out"] * s_output),
|
| 163 |
+
risk_flag = case_when(
|
| 164 |
+
is.na(win_rate_used) ~ "Watch",
|
| 165 |
+
win_rate_used < risk_win_cut | booked_cv > risk_cv_cut ~ "At Risk",
|
| 166 |
+
win_rate_used < (risk_win_cut + 0.06) | booked_cv > (risk_cv_cut * 0.8) ~ "Watch",
|
| 167 |
+
TRUE ~ "Healthy"
|
| 168 |
+
)
|
| 169 |
+
) %>%
|
| 170 |
+
arrange(desc(composite_score))
|
| 171 |
+
})
|
| 172 |
+
|
| 173 |
+
ceo_forecast <- reactive({
|
| 174 |
+
d <- filtered_data()
|
| 175 |
+
validate(need(nrow(d) > 0, "No data in selected range."))
|
| 176 |
+
|
| 177 |
+
monthly <- d %>%
|
| 178 |
+
mutate(period = floor_date(report_date, "month")) %>%
|
| 179 |
+
group_by(primary_name, period) %>%
|
| 180 |
+
summarise(
|
| 181 |
+
amt_bid = sum(amt_bid, na.rm = TRUE),
|
| 182 |
+
amt_booked = sum(amt_booked, na.rm = TRUE),
|
| 183 |
+
.groups = "drop"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
estimators <- sort(unique(monthly$primary_name))
|
| 187 |
+
out <- vector("list", length(estimators))
|
| 188 |
+
|
| 189 |
+
for (i in seq_along(estimators)) {
|
| 190 |
+
est <- estimators[i]
|
| 191 |
+
e <- monthly %>% filter(primary_name == est) %>% arrange(period)
|
| 192 |
+
if (nrow(e) < 10) next
|
| 193 |
+
|
| 194 |
+
e <- e %>%
|
| 195 |
+
mutate(
|
| 196 |
+
t = row_number(),
|
| 197 |
+
month_num = month(period),
|
| 198 |
+
lag_booked = lag(amt_booked, 1),
|
| 199 |
+
lag_bid = lag(amt_bid, 1)
|
| 200 |
+
) %>%
|
| 201 |
+
drop_na(lag_booked, lag_bid)
|
| 202 |
+
|
| 203 |
+
if (nrow(e) < 8) next
|
| 204 |
+
|
| 205 |
+
fit <- tryCatch(
|
| 206 |
+
lm(log1p(amt_booked) ~ log1p(amt_bid) + log1p(lag_booked) + month_num + t, data = e),
|
| 207 |
+
error = function(err) NULL
|
| 208 |
+
)
|
| 209 |
+
if (is.null(fit)) next
|
| 210 |
+
|
| 211 |
+
fit_resid <- residuals(fit)
|
| 212 |
+
resid_sd <- sd(fit_resid, na.rm = TRUE)
|
| 213 |
+
if (is.na(resid_sd) || resid_sd == 0) resid_sd <- 0.10
|
| 214 |
+
|
| 215 |
+
hist <- monthly %>% filter(primary_name == est) %>% arrange(period)
|
| 216 |
+
next_periods <- seq(max(hist$period) %m+% months(1), by = "month", length.out = 3)
|
| 217 |
+
|
| 218 |
+
last_booked <- tail(hist$amt_booked, 1)
|
| 219 |
+
last_bid <- tail(hist$amt_bid, 1)
|
| 220 |
+
avg_recent_bid <- mean(tail(hist$amt_bid, 3), na.rm = TRUE)
|
| 221 |
+
if (is.na(avg_recent_bid) || avg_recent_bid <= 0) avg_recent_bid <- max(last_bid, 1, na.rm = TRUE)
|
| 222 |
+
if (is.na(last_booked)) last_booked <- 0
|
| 223 |
+
if (is.na(last_bid)) last_bid <- avg_recent_bid
|
| 224 |
+
|
| 225 |
+
pred_rows <- vector("list", length(next_periods))
|
| 226 |
+
current_t <- max(e$t, na.rm = TRUE)
|
| 227 |
+
current_lag_booked <- tail(hist$amt_booked, 1)
|
| 228 |
+
current_lag_bid <- tail(hist$amt_bid, 1)
|
| 229 |
+
if (is.na(current_lag_booked)) current_lag_booked <- 0
|
| 230 |
+
if (is.na(current_lag_bid) || current_lag_bid <= 0) current_lag_bid <- avg_recent_bid
|
| 231 |
+
|
| 232 |
+
for (j in seq_along(next_periods)) {
|
| 233 |
+
prd <- next_periods[j]
|
| 234 |
+
current_t <- current_t + 1
|
| 235 |
+
new_x <- tibble(
|
| 236 |
+
amt_bid = avg_recent_bid,
|
| 237 |
+
lag_booked = current_lag_booked,
|
| 238 |
+
lag_bid = current_lag_bid,
|
| 239 |
+
month_num = month(prd),
|
| 240 |
+
t = current_t
|
| 241 |
+
)
|
| 242 |
+
pred_log <- as.numeric(predict(fit, newdata = new_x))
|
| 243 |
+
pred_amt <- pmax(0, expm1(pred_log))
|
| 244 |
+
lo <- pmax(0, expm1(pred_log - 1.28 * resid_sd))
|
| 245 |
+
hi <- pmax(0, expm1(pred_log + 1.28 * resid_sd))
|
| 246 |
+
|
| 247 |
+
pred_rows[[j]] <- tibble(
|
| 248 |
+
primary_name = est,
|
| 249 |
+
period = prd,
|
| 250 |
+
predicted_booked = pred_amt,
|
| 251 |
+
lo80 = lo,
|
| 252 |
+
hi80 = hi,
|
| 253 |
+
model_mae = mean(abs(expm1(fitted(fit)) - e$amt_booked), na.rm = TRUE)
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
current_lag_booked <- pred_amt
|
| 257 |
+
current_lag_bid <- avg_recent_bid
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
out[[i]] <- bind_rows(pred_rows)
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
bind_rows(out)
|
| 264 |
+
})
|
| 265 |
+
|
| 266 |
# ββ MAIN DROPDOWN UPDATERS ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 267 |
|
| 268 |
observe({
|
|
|
|
| 274 |
updatePickerInput(session, "estimatorSelect", choices = est_list, selected = isolate(input$estimatorSelect))
|
| 275 |
|
| 276 |
# ββ BUG FIX 1: its_estimator auto-selection βββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
| 277 |
eligible <- df %>%
|
| 278 |
group_by(primary_name) %>%
|
| 279 |
summarise(n = n(), .groups = "drop") %>%
|
|
|
|
| 281 |
pull(primary_name) %>%
|
| 282 |
sort()
|
| 283 |
|
| 284 |
+
cur_its <- isolate(input$its_estimator)
|
| 285 |
selected_its <- if (length(cur_its) > 0 && cur_its %in% eligible) cur_its else eligible[1]
|
| 286 |
updatePickerInput(session, "its_estimator", choices = eligible, selected = selected_its)
|
| 287 |
# ββ END BUG FIX 1 ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 517 |
observeEvent(input$its_run, {
|
| 518 |
|
| 519 |
# ββ BUG FIX 2: tryCatch around its_series() call ββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
| 520 |
series <- tryCatch(its_series(), error = function(e) NULL)
|
| 521 |
if (is.null(series)) {
|
| 522 |
its_result(list(error = "Please select an estimator from the dropdown, then click Run Analysis."))
|
|
|
|
| 544 |
bp_dates <- series$date_group[bp_idx]
|
| 545 |
|
| 546 |
# ββ BUG FIX 3: Fstats time-series subscripting ββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
fs <- Fstats(value ~ t, data = series)
|
| 548 |
fs_times <- as.integer(time(fs$Fstats))
|
| 549 |
fs_vals <- as.numeric(fs$Fstats)
|
|
|
|
| 584 |
|
| 585 |
cf <- coeftest(fit)
|
| 586 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
cf_df <- data.frame(
|
| 588 |
Term = recode(rownames(cf),
|
| 589 |
`(Intercept)` = "Intercept (baseline level)",
|
|
|
|
| 832 |
km <- kmeans(feats, centers = k, nstart = 25)
|
| 833 |
ds_df$Cluster <- as.factor(km$cluster)
|
| 834 |
cl_means <- ds_df %>% group_by(Cluster) %>% summarise(mean_wr = mean(Win_Rate, na.rm = TRUE), .groups = "drop") %>% arrange(desc(mean_wr))
|
| 835 |
+
arch_map <- setNames(c("High-Efficiency", "Selective", "New/Developing")[seq_len(nrow(cl_means))], cl_means$Cluster)
|
| 836 |
ds_df$Archetype <- arch_map[as.character(ds_df$Cluster)]
|
| 837 |
}
|
| 838 |
|
|
|
|
| 852 |
ggplotly(p, tooltip = "text")
|
| 853 |
})
|
| 854 |
|
| 855 |
+
# ββ ESTIMATOR SCATTER βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 856 |
+
|
| 857 |
+
# Aggregate filtered_data() per estimator β no separate DB query needed.
|
| 858 |
+
# Inherits the sidebar date range and estimator picker automatically.
|
| 859 |
+
scatter_agg <- reactive({
|
| 860 |
+
d <- filtered_data()
|
| 861 |
+
lwr <- lag_win_rate()
|
| 862 |
+
validate(need(nrow(d) > 0, "No data in selected range."))
|
| 863 |
+
|
| 864 |
+
agg <- d %>%
|
| 865 |
+
group_by(primary_name) %>%
|
| 866 |
+
summarise(
|
| 867 |
+
total_amt_bid = sum(amt_bid, na.rm = TRUE),
|
| 868 |
+
total_amt_booked = sum(amt_booked, na.rm = TRUE),
|
| 869 |
+
total_qty_bid = sum(qty_bid, na.rm = TRUE),
|
| 870 |
+
total_qty_booked = sum(qty_booked, na.rm = TRUE),
|
| 871 |
+
.groups = "drop"
|
| 872 |
+
) %>%
|
| 873 |
+
filter(total_qty_bid >= 3) %>% # exclude trivial sample sizes
|
| 874 |
+
mutate(
|
| 875 |
+
win_rate_sm = ifelse(total_amt_bid > 0, total_amt_booked / total_amt_bid, NA_real_),
|
| 876 |
+
avg_bid_size = ifelse(total_qty_bid > 0, total_amt_bid / total_qty_bid, NA_real_),
|
| 877 |
+
avg_booked_size = ifelse(total_qty_booked > 0, total_amt_booked / total_qty_booked, NA_real_)
|
| 878 |
+
)
|
| 879 |
+
|
| 880 |
+
# Prefer lag-adjusted win rate when available (mirrors K-Means behaviour)
|
| 881 |
+
if (!is.null(lwr) && nrow(lwr) > 0) {
|
| 882 |
+
lag_summary <- lwr %>%
|
| 883 |
+
group_by(primary_name) %>%
|
| 884 |
+
summarise(win_rate_lag = sum(lag_booked_amt, na.rm = TRUE) / sum(amt_bid, na.rm = TRUE),
|
| 885 |
+
.groups = "drop")
|
| 886 |
+
agg <- agg %>%
|
| 887 |
+
left_join(lag_summary, by = "primary_name") %>%
|
| 888 |
+
mutate(win_rate = coalesce(win_rate_lag, win_rate_sm)) %>%
|
| 889 |
+
select(-win_rate_lag, -win_rate_sm)
|
| 890 |
+
} else {
|
| 891 |
+
agg <- agg %>% rename(win_rate = win_rate_sm)
|
| 892 |
+
}
|
| 893 |
+
|
| 894 |
+
agg %>% filter(!is.na(win_rate), !is.infinite(win_rate),
|
| 895 |
+
!is.na(avg_bid_size), !is.infinite(avg_bid_size))
|
| 896 |
+
})
|
| 897 |
+
|
| 898 |
+
output$scatter_plot <- renderPlotly({
|
| 899 |
+
df <- scatter_agg()
|
| 900 |
+
validate(need(nrow(df) > 0, "No estimators meet the minimum bid threshold for the selected period."))
|
| 901 |
+
|
| 902 |
+
# ββ Axis / size column resolution ββββββββββββββββββββββββββββββββββββ
|
| 903 |
+
x_col <- input$scatter_x
|
| 904 |
+
y_col <- input$scatter_y
|
| 905 |
+
size_col <- input$scatter_size
|
| 906 |
+
|
| 907 |
+
axis_labels <- c(
|
| 908 |
+
avg_bid_size = "Avg Bid Size ($)",
|
| 909 |
+
total_amt_bid = "Total Bid Volume ($)",
|
| 910 |
+
total_qty_bid = "Bid Count",
|
| 911 |
+
win_rate = "Win Rate (%)",
|
| 912 |
+
avg_booked_size = "Avg Booked Size ($)",
|
| 913 |
+
total_amt_booked = "Total Booked Volume ($)"
|
| 914 |
+
)
|
| 915 |
+
|
| 916 |
+
x_vals <- df[[x_col]]
|
| 917 |
+
y_vals <- df[[y_col]]
|
| 918 |
+
size_vals <- df[[size_col]]
|
| 919 |
+
|
| 920 |
+
# Guard: if avg_booked_size is all NA (no bookings in period), fall back gracefully
|
| 921 |
+
validate(need(!all(is.na(y_vals)), paste0("No data available for Y axis: ", axis_labels[y_col])))
|
| 922 |
+
validate(need(!all(is.na(size_vals)), paste0("No data available for size: ", axis_labels[size_col])))
|
| 923 |
+
|
| 924 |
+
# Normalise bubble size to a sensible pixel range (10β55px)
|
| 925 |
+
size_norm <- scales::rescale(replace_na(size_vals, 0), to = c(10, 55))
|
| 926 |
+
|
| 927 |
+
# ββ Hover text βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 928 |
+
fmt_val <- function(vals, col_id) {
|
| 929 |
+
lbl <- axis_labels[col_id]
|
| 930 |
+
if (grepl("\\$", lbl)) dollar(vals, accuracy = 1)
|
| 931 |
+
else if (col_id == "win_rate") paste0(round(vals * 100, 1), "%")
|
| 932 |
+
else comma(vals, accuracy = 1)
|
| 933 |
+
}
|
| 934 |
+
|
| 935 |
+
hover_text <- paste0(
|
| 936 |
+
"<b>", df$primary_name, "</b><br>",
|
| 937 |
+
axis_labels[x_col], ": ", fmt_val(x_vals, x_col), "<br>",
|
| 938 |
+
axis_labels[y_col], ": ", fmt_val(y_vals, y_col), "<br>",
|
| 939 |
+
axis_labels[size_col], " (bubble): ", fmt_val(size_vals, size_col), "<br>",
|
| 940 |
+
"Bids submitted: ", comma(df$total_qty_bid, accuracy = 1), "<br>",
|
| 941 |
+
"Bids won: ", comma(df$total_qty_booked, accuracy = 1)
|
| 942 |
+
)
|
| 943 |
+
|
| 944 |
+
# ββ AdminLTE-matching palette β one colour per estimator βββββββββββββ
|
| 945 |
+
palette_hex <- c("#3c8dbc", "#00a65a", "#f39c12", "#dd4b39",
|
| 946 |
+
"#605ca8", "#d81b60", "#00c0ef", "#39cccc",
|
| 947 |
+
"#ff851b", "#b0bec5")
|
| 948 |
+
dot_colors <- palette_hex[(seq_len(nrow(df)) - 1L) %% length(palette_hex) + 1L]
|
| 949 |
+
|
| 950 |
+
# ββ Y-axis tick format βββββββββββββββββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββ
|
| 951 |
+
y_tickformat <- if (y_col == "win_rate") ".1%"
|
| 952 |
+
else if (grepl("\\$", axis_labels[y_col])) "$,.0f"
|
| 953 |
+
else ""
|
| 954 |
+
x_tickformat <- if (grepl("\\$", axis_labels[x_col])) "$,.0f"
|
| 955 |
+
else ""
|
| 956 |
+
|
| 957 |
+
# ββ Build plotly βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 958 |
+
plot_ly(
|
| 959 |
+
data = df,
|
| 960 |
+
x = ~x_vals,
|
| 961 |
+
y = ~y_vals,
|
| 962 |
+
text = ~hover_text,
|
| 963 |
+
hoverinfo = "text",
|
| 964 |
+
type = "scatter",
|
| 965 |
+
mode = "markers+text",
|
| 966 |
+
marker = list(
|
| 967 |
+
size = size_norm,
|
| 968 |
+
color = dot_colors,
|
| 969 |
+
opacity = 0.85,
|
| 970 |
+
line = list(color = "rgba(255,255,255,0.3)", width = 1.2)
|
| 971 |
+
),
|
| 972 |
+
texttemplate = "%{text}", # plotly ignores this for hoverinfo="text"; set separately
|
| 973 |
+
showlegend = FALSE
|
| 974 |
+
) %>%
|
| 975 |
+
# Estimator name labels floating above each bubble
|
| 976 |
+
add_text(
|
| 977 |
+
x = ~x_vals,
|
| 978 |
+
y = ~y_vals,
|
| 979 |
+
text = ~primary_name,
|
| 980 |
+
textposition = "top center",
|
| 981 |
+
textfont = list(size = 10, color = "#444444"),
|
| 982 |
+
hoverinfo = "none",
|
| 983 |
+
showlegend = FALSE
|
| 984 |
+
) %>%
|
| 985 |
+
layout(
|
| 986 |
+
paper_bgcolor = "rgba(0,0,0,0)",
|
| 987 |
+
plot_bgcolor = "rgba(0,0,0,0)",
|
| 988 |
+
font = list(color = "#2c3e50", family = "Source Sans Pro, sans-serif"),
|
| 989 |
+
xaxis = list(
|
| 990 |
+
title = axis_labels[x_col],
|
| 991 |
+
gridcolor = "rgba(0,0,0,0.06)",
|
| 992 |
+
zerolinecolor = "rgba(0,0,0,0.15)",
|
| 993 |
+
tickformat = x_tickformat,
|
| 994 |
+
titlefont = list(size = 12, color = "#2c3e50")
|
| 995 |
+
),
|
| 996 |
+
yaxis = list(
|
| 997 |
+
title = axis_labels[y_col],
|
| 998 |
+
gridcolor = "rgba(0,0,0,0.06)",
|
| 999 |
+
zerolinecolor = "rgba(0,0,0,0.15)",
|
| 1000 |
+
tickformat = y_tickformat,
|
| 1001 |
+
titlefont = list(size = 12, color = "#2c3e50")
|
| 1002 |
+
),
|
| 1003 |
+
showlegend = FALSE,
|
| 1004 |
+
margin = list(t = 30, r = 20, b = 60, l = 80),
|
| 1005 |
+
hoverlabel = list(
|
| 1006 |
+
bgcolor = "#2c3e50",
|
| 1007 |
+
bordercolor = "#3c8dbc",
|
| 1008 |
+
font = list(color = "#ffffff", size = 12)
|
| 1009 |
+
)
|
| 1010 |
+
) %>%
|
| 1011 |
+
config(
|
| 1012 |
+
displayModeBar = TRUE,
|
| 1013 |
+
modeBarButtonsToRemove = c("lasso2d", "select2d", "autoScale2d"),
|
| 1014 |
+
displaylogo = FALSE
|
| 1015 |
+
)
|
| 1016 |
+
})
|
| 1017 |
+
|
| 1018 |
# ββ ANNUAL, YOY, QUARTERLY ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1019 |
|
| 1020 |
output$annual_bar_chart <- renderPlotly({
|
|
|
|
| 1148 |
datatable(lb, options = list(pageLength = 15, scrollX = TRUE, order = list(list(2, "desc"))), rownames = FALSE)
|
| 1149 |
})
|
| 1150 |
|
| 1151 |
+
output$ceo_total_pred_90d <- renderValueBox({
|
| 1152 |
+
f <- ceo_forecast()
|
| 1153 |
+
total_pred <- sum(f$predicted_booked, na.rm = TRUE)
|
| 1154 |
+
valueBox(
|
| 1155 |
+
value = dollar(total_pred, accuracy = 1),
|
| 1156 |
+
subtitle = "Forecasted Bookings (Next 90 Days)",
|
| 1157 |
+
icon = icon("chart-line"),
|
| 1158 |
+
color = "blue"
|
| 1159 |
+
)
|
| 1160 |
+
})
|
| 1161 |
+
|
| 1162 |
+
output$ceo_top_estimator <- renderValueBox({
|
| 1163 |
+
f <- ceo_forecast()
|
| 1164 |
+
top <- f %>%
|
| 1165 |
+
group_by(primary_name) %>%
|
| 1166 |
+
summarise(pred_90d = sum(predicted_booked, na.rm = TRUE), .groups = "drop") %>%
|
| 1167 |
+
arrange(desc(pred_90d)) %>%
|
| 1168 |
+
slice(1)
|
| 1169 |
+
validate(need(nrow(top) > 0, "No forecast available yet."))
|
| 1170 |
+
valueBox(
|
| 1171 |
+
value = top$primary_name[[1]],
|
| 1172 |
+
subtitle = paste0("Top Expected Contributor (", dollar(top$pred_90d[[1]], accuracy = 1), ")"),
|
| 1173 |
+
icon = icon("trophy"),
|
| 1174 |
+
color = "green"
|
| 1175 |
+
)
|
| 1176 |
+
})
|
| 1177 |
+
|
| 1178 |
+
output$ceo_at_risk_count <- renderValueBox({
|
| 1179 |
+
sc <- ceo_scorecard()
|
| 1180 |
+
n_risk <- sum(sc$risk_flag == "At Risk", na.rm = TRUE)
|
| 1181 |
+
valueBox(
|
| 1182 |
+
value = n_risk,
|
| 1183 |
+
subtitle = "Estimators Flagged At Risk",
|
| 1184 |
+
icon = icon("triangle-exclamation"),
|
| 1185 |
+
color = ifelse(n_risk > 0, "red", "olive")
|
| 1186 |
+
)
|
| 1187 |
+
})
|
| 1188 |
+
|
| 1189 |
+
output$ceo_model_quality <- renderValueBox({
|
| 1190 |
+
f <- ceo_forecast()
|
| 1191 |
+
mae <- mean(f$model_mae, na.rm = TRUE)
|
| 1192 |
+
if (is.na(mae)) mae <- 0
|
| 1193 |
+
valueBox(
|
| 1194 |
+
value = dollar(mae, accuracy = 1),
|
| 1195 |
+
subtitle = "Average Forecast MAE (In-Sample)",
|
| 1196 |
+
icon = icon("ruler"),
|
| 1197 |
+
color = "purple"
|
| 1198 |
+
)
|
| 1199 |
+
})
|
| 1200 |
+
|
| 1201 |
+
output$ceo_forecast_plot <- renderPlotly({
|
| 1202 |
+
f <- ceo_forecast()
|
| 1203 |
+
validate(need(nrow(f) > 0, "Insufficient history to forecast."))
|
| 1204 |
+
|
| 1205 |
+
p <- f %>%
|
| 1206 |
+
group_by(primary_name) %>%
|
| 1207 |
+
summarise(
|
| 1208 |
+
pred_90d = sum(predicted_booked, na.rm = TRUE),
|
| 1209 |
+
lo80 = sum(lo80, na.rm = TRUE),
|
| 1210 |
+
hi80 = sum(hi80, na.rm = TRUE),
|
| 1211 |
+
.groups = "drop"
|
| 1212 |
+
) %>%
|
| 1213 |
+
arrange(desc(pred_90d)) %>%
|
| 1214 |
+
mutate(primary_name = fct_reorder(primary_name, pred_90d)) %>%
|
| 1215 |
+
ggplot(aes(x = primary_name, y = pred_90d,
|
| 1216 |
+
text = paste0(
|
| 1217 |
+
"<b>", primary_name, "</b><br>",
|
| 1218 |
+
"Predicted 90D: ", dollar(pred_90d, accuracy = 1), "<br>",
|
| 1219 |
+
"80% band: ", dollar(lo80, accuracy = 1), " - ", dollar(hi80, accuracy = 1)
|
| 1220 |
+
))) +
|
| 1221 |
+
geom_col(fill = "#3c8dbc", width = 0.65) +
|
| 1222 |
+
geom_errorbar(aes(ymin = lo80, ymax = hi80), width = 0.2, color = "#2c3e50") +
|
| 1223 |
+
coord_flip() +
|
| 1224 |
+
scale_y_continuous(labels = dollar_format(accuracy = 1)) +
|
| 1225 |
+
labs(x = "", y = "Predicted Booked Revenue ($)") +
|
| 1226 |
+
theme_minimal()
|
| 1227 |
+
|
| 1228 |
+
ggplotly(p, tooltip = "text")
|
| 1229 |
+
})
|
| 1230 |
+
|
| 1231 |
+
output$ceo_action_summary <- renderUI({
|
| 1232 |
+
sc <- ceo_scorecard()
|
| 1233 |
+
f <- ceo_forecast()
|
| 1234 |
+
validate(need(nrow(sc) > 0, "No scorecard data."))
|
| 1235 |
+
|
| 1236 |
+
top_strength <- sc %>% arrange(desc(composite_score)) %>% slice(1)
|
| 1237 |
+
top_risk <- sc %>% filter(risk_flag == "At Risk") %>% arrange(composite_score) %>% slice(1)
|
| 1238 |
+
|
| 1239 |
+
f_rollup <- f %>%
|
| 1240 |
+
group_by(primary_name) %>%
|
| 1241 |
+
summarise(pred_90d = sum(predicted_booked, na.rm = TRUE), .groups = "drop")
|
| 1242 |
+
upside <- f_rollup %>% arrange(desc(pred_90d)) %>% slice(1)
|
| 1243 |
+
|
| 1244 |
+
tagList(
|
| 1245 |
+
tags$p(tags$b("Top performer:"), paste0(" ", top_strength$primary_name[[1]], " (score ", round(top_strength$composite_score[[1]], 1), ").")),
|
| 1246 |
+
tags$p(tags$b("Highest upside:"), paste0(" ", upside$primary_name[[1]], " with projected ", dollar(upside$pred_90d[[1]], accuracy = 1), " in next 90 days.")),
|
| 1247 |
+
if (nrow(top_risk) > 0) {
|
| 1248 |
+
tags$p(tags$b("Immediate coaching target:"), paste0(" ", top_risk$primary_name[[1]], " (low conversion or unstable output)."))
|
| 1249 |
+
} else {
|
| 1250 |
+
tags$p(tags$b("Risk status:"), " No estimator currently flagged as high risk.")
|
| 1251 |
+
}
|
| 1252 |
+
)
|
| 1253 |
+
})
|
| 1254 |
+
|
| 1255 |
+
output$ceo_scorecard_table <- renderDataTable({
|
| 1256 |
+
sc <- ceo_scorecard()
|
| 1257 |
+
f <- ceo_forecast() %>%
|
| 1258 |
+
group_by(primary_name) %>%
|
| 1259 |
+
summarise(pred_90d = sum(predicted_booked, na.rm = TRUE), .groups = "drop")
|
| 1260 |
+
|
| 1261 |
+
view <- sc %>%
|
| 1262 |
+
left_join(f, by = "primary_name") %>%
|
| 1263 |
+
mutate(
|
| 1264 |
+
pred_90d = replace_na(pred_90d, 0),
|
| 1265 |
+
win_rate_used = percent(win_rate_used, accuracy = 0.1),
|
| 1266 |
+
same_month_win_rate = percent(same_month_win_rate, accuracy = 0.1),
|
| 1267 |
+
lag_win_rate = ifelse(is.na(lag_win_rate), "N/A", percent(lag_win_rate, accuracy = 0.1)),
|
| 1268 |
+
total_bid = dollar(total_bid, accuracy = 1),
|
| 1269 |
+
total_booked = dollar(total_booked, accuracy = 1),
|
| 1270 |
+
avg_job_size = dollar(avg_job_size, accuracy = 1),
|
| 1271 |
+
pred_90d = dollar(pred_90d, accuracy = 1),
|
| 1272 |
+
composite_score = round(composite_score, 1)
|
| 1273 |
+
) %>%
|
| 1274 |
+
transmute(
|
| 1275 |
+
Estimator = primary_name,
|
| 1276 |
+
`Score (0-100)` = composite_score,
|
| 1277 |
+
`Risk` = risk_flag,
|
| 1278 |
+
`Win % (Used)` = win_rate_used,
|
| 1279 |
+
`Win % (Same-Mo)` = same_month_win_rate,
|
| 1280 |
+
`Win % (Lag-Adj)` = lag_win_rate,
|
| 1281 |
+
`Booked Forecast 90D` = pred_90d,
|
| 1282 |
+
`Total Booked` = total_booked,
|
| 1283 |
+
`Total Bid` = total_bid,
|
| 1284 |
+
`Avg Job Size` = avg_job_size,
|
| 1285 |
+
`Months Active` = months_active
|
| 1286 |
+
)
|
| 1287 |
+
|
| 1288 |
+
datatable(
|
| 1289 |
+
view,
|
| 1290 |
+
options = list(pageLength = 15, scrollX = TRUE, order = list(list(1, "desc"))),
|
| 1291 |
+
rownames = FALSE
|
| 1292 |
+
)
|
| 1293 |
+
})
|
| 1294 |
+
|
| 1295 |
+
output$download_top_brief <- downloadHandler(
|
| 1296 |
+
filename = function() { paste0("Top_Brief_", format(Sys.Date(), "%Y-%m-%d"), ".xlsx") },
|
| 1297 |
+
content = function(file) {
|
| 1298 |
+
sc <- ceo_scorecard()
|
| 1299 |
+
fc <- ceo_forecast()
|
| 1300 |
+
req(nrow(sc) > 0)
|
| 1301 |
+
|
| 1302 |
+
withProgress(message = "Building Top Brief export...", value = 0.2, {
|
| 1303 |
+
wb <- createWorkbook()
|
| 1304 |
+
header_style <- createStyle(fontName = "Calibri", fontSize = 11, fontColour = "#FFFFFF", fgFill = "#2c3e50",
|
| 1305 |
+
halign = "LEFT", valign = "CENTER", textDecoration = "bold",
|
| 1306 |
+
border = "Bottom", borderColour = "#27ae60")
|
| 1307 |
+
title_style <- createStyle(fontName = "Calibri", fontSize = 14, textDecoration = "bold", fontColour = "#2c3e50")
|
| 1308 |
+
dollar_style <- createStyle(numFmt = "$#,##0")
|
| 1309 |
+
pct_style <- createStyle(numFmt = "0.0%")
|
| 1310 |
+
|
| 1311 |
+
fc_roll <- fc %>%
|
| 1312 |
+
group_by(primary_name) %>%
|
| 1313 |
+
summarise(
|
| 1314 |
+
pred_90d = sum(predicted_booked, na.rm = TRUE),
|
| 1315 |
+
lo80 = sum(lo80, na.rm = TRUE),
|
| 1316 |
+
hi80 = sum(hi80, na.rm = TRUE),
|
| 1317 |
+
model_mae = mean(model_mae, na.rm = TRUE),
|
| 1318 |
+
.groups = "drop"
|
| 1319 |
+
)
|
| 1320 |
+
|
| 1321 |
+
top_strength <- sc %>% arrange(desc(composite_score)) %>% slice(1)
|
| 1322 |
+
top_risk <- sc %>% filter(risk_flag == "At Risk") %>% arrange(composite_score) %>% slice(1)
|
| 1323 |
+
top_upside <- fc_roll %>% arrange(desc(pred_90d)) %>% slice(1)
|
| 1324 |
+
|
| 1325 |
+
addWorksheet(wb, "Top Brief Summary")
|
| 1326 |
+
setColWidths(wb, "Top Brief Summary", cols = 1:3, widths = c(36, 26, 26))
|
| 1327 |
+
writeData(wb, "Top Brief Summary", "Top Brief - Executive Summary", startRow = 1, startCol = 1)
|
| 1328 |
+
addStyle(wb, "Top Brief Summary", title_style, rows = 1, cols = 1)
|
| 1329 |
+
|
| 1330 |
+
total_pred <- sum(fc_roll$pred_90d, na.rm = TRUE)
|
| 1331 |
+
at_risk_n <- sum(sc$risk_flag == "At Risk", na.rm = TRUE)
|
| 1332 |
+
avg_mae <- mean(fc_roll$model_mae, na.rm = TRUE)
|
| 1333 |
+
|
| 1334 |
+
kpi <- data.frame(
|
| 1335 |
+
Metric = c("Forecasted Bookings (Next 90 Days)", "Top Expected Contributor", "Estimators Flagged At Risk", "Average Forecast MAE"),
|
| 1336 |
+
Value = c(
|
| 1337 |
+
dollar(total_pred, accuracy = 1),
|
| 1338 |
+
if (nrow(top_upside) > 0) paste0(top_upside$primary_name[[1]], " (", dollar(top_upside$pred_90d[[1]], accuracy = 1), ")") else "N/A",
|
| 1339 |
+
as.character(at_risk_n),
|
| 1340 |
+
dollar(ifelse(is.na(avg_mae), 0, avg_mae), accuracy = 1)
|
| 1341 |
+
),
|
| 1342 |
+
stringsAsFactors = FALSE
|
| 1343 |
+
)
|
| 1344 |
+
writeData(wb, "Top Brief Summary", kpi, startRow = 3, startCol = 1, colNames = TRUE)
|
| 1345 |
+
addStyle(wb, "Top Brief Summary", header_style, rows = 3, cols = 1:2)
|
| 1346 |
+
|
| 1347 |
+
action_lines <- data.frame(
|
| 1348 |
+
`Action Summary` = c(
|
| 1349 |
+
paste0("Top performer: ", top_strength$primary_name[[1]], " (score ", round(top_strength$composite_score[[1]], 1), ")."),
|
| 1350 |
+
if (nrow(top_upside) > 0) paste0("Highest upside: ", top_upside$primary_name[[1]], " projected ", dollar(top_upside$pred_90d[[1]], accuracy = 1), " over next 90 days.") else "Highest upside: N/A",
|
| 1351 |
+
if (nrow(top_risk) > 0) paste0("Immediate coaching target: ", top_risk$primary_name[[1]], ".") else "Risk status: no estimator currently flagged as high risk."
|
| 1352 |
+
),
|
| 1353 |
+
stringsAsFactors = FALSE
|
| 1354 |
+
)
|
| 1355 |
+
writeData(wb, "Top Brief Summary", action_lines, startRow = 9, startCol = 1, colNames = TRUE)
|
| 1356 |
+
addStyle(wb, "Top Brief Summary", header_style, rows = 9, cols = 1)
|
| 1357 |
+
|
| 1358 |
+
config_df <- data.frame(
|
| 1359 |
+
Parameter = c("Weight: Win Quality", "Weight: Efficiency", "Weight: Stability", "Weight: Output", "At-Risk Win % <", "At-Risk Volatility >"),
|
| 1360 |
+
Value = c(
|
| 1361 |
+
input$ceo_w_win %||% 35,
|
| 1362 |
+
input$ceo_w_eff %||% 20,
|
| 1363 |
+
input$ceo_w_stab %||% 20,
|
| 1364 |
+
input$ceo_w_out %||% 25,
|
| 1365 |
+
input$ceo_risk_win %||% 12,
|
| 1366 |
+
input$ceo_risk_cv %||% 1.25
|
| 1367 |
+
),
|
| 1368 |
+
stringsAsFactors = FALSE
|
| 1369 |
+
)
|
| 1370 |
+
writeData(wb, "Top Brief Summary", config_df, startRow = 14, startCol = 1, colNames = TRUE)
|
| 1371 |
+
addStyle(wb, "Top Brief Summary", header_style, rows = 14, cols = 1:2)
|
| 1372 |
+
|
| 1373 |
+
setProgress(0.6)
|
| 1374 |
+
|
| 1375 |
+
addWorksheet(wb, "Scorecard")
|
| 1376 |
+
setColWidths(wb, "Scorecard", cols = 1:12, widths = c(22, 12, 10, 12, 12, 12, 16, 14, 14, 14, 12, 12))
|
| 1377 |
+
score_export <- sc %>%
|
| 1378 |
+
left_join(fc_roll %>% select(primary_name, pred_90d), by = "primary_name") %>%
|
| 1379 |
+
mutate(pred_90d = replace_na(pred_90d, 0)) %>%
|
| 1380 |
+
transmute(
|
| 1381 |
+
Estimator = primary_name,
|
| 1382 |
+
`Score (0-100)` = round(composite_score, 1),
|
| 1383 |
+
Risk = risk_flag,
|
| 1384 |
+
`Win % (Used)` = win_rate_used,
|
| 1385 |
+
`Win % (Same-Mo)` = same_month_win_rate,
|
| 1386 |
+
`Win % (Lag-Adj)` = lag_win_rate,
|
| 1387 |
+
`Booked Forecast 90D` = pred_90d,
|
| 1388 |
+
`Total Booked` = total_booked,
|
| 1389 |
+
`Total Bid` = total_bid,
|
| 1390 |
+
`Avg Job Size` = avg_job_size,
|
| 1391 |
+
`Months Active` = months_active,
|
| 1392 |
+
`Booked CV` = booked_cv
|
| 1393 |
+
) %>%
|
| 1394 |
+
arrange(desc(`Score (0-100)`))
|
| 1395 |
+
writeData(wb, "Scorecard", score_export, startRow = 1, startCol = 1, colNames = TRUE)
|
| 1396 |
+
addStyle(wb, "Scorecard", header_style, rows = 1, cols = 1:ncol(score_export))
|
| 1397 |
+
addStyle(wb, "Scorecard", pct_style, rows = 2:(nrow(score_export) + 1), cols = c(4, 5, 6), gridExpand = TRUE)
|
| 1398 |
+
addStyle(wb, "Scorecard", dollar_style, rows = 2:(nrow(score_export) + 1), cols = c(7, 8, 9, 10), gridExpand = TRUE)
|
| 1399 |
+
|
| 1400 |
+
addWorksheet(wb, "Forecast Detail")
|
| 1401 |
+
setColWidths(wb, "Forecast Detail", cols = 1:6, widths = c(22, 14, 14, 14, 14, 14))
|
| 1402 |
+
fc_export <- fc %>%
|
| 1403 |
+
arrange(primary_name, period) %>%
|
| 1404 |
+
transmute(
|
| 1405 |
+
Estimator = primary_name,
|
| 1406 |
+
Month = format(period, "%b %Y"),
|
| 1407 |
+
`Predicted Booked` = predicted_booked,
|
| 1408 |
+
`Low 80%` = lo80,
|
| 1409 |
+
`High 80%` = hi80,
|
| 1410 |
+
`Model MAE` = model_mae
|
| 1411 |
+
)
|
| 1412 |
+
writeData(wb, "Forecast Detail", fc_export, startRow = 1, startCol = 1, colNames = TRUE)
|
| 1413 |
+
addStyle(wb, "Forecast Detail", header_style, rows = 1, cols = 1:ncol(fc_export))
|
| 1414 |
+
addStyle(wb, "Forecast Detail", dollar_style, rows = 2:(nrow(fc_export) + 1), cols = 3:6, gridExpand = TRUE)
|
| 1415 |
+
|
| 1416 |
+
saveWorkbook(wb, file, overwrite = TRUE)
|
| 1417 |
+
})
|
| 1418 |
+
}
|
| 1419 |
+
)
|
| 1420 |
+
|
| 1421 |
output$db_status_text <- renderText({ paste("Database records loaded:", comma(nrow(get_data()))) })
|
| 1422 |
|
| 1423 |
output$preview_table <- renderDataTable({
|
|
|
|
| 1510 |
})
|
| 1511 |
}
|
| 1512 |
)
|
| 1513 |
+
}
|
tutorial.R
ADDED
|
File without changes
|
ui.R
CHANGED
|
@@ -10,8 +10,10 @@ dashboardPage(
|
|
| 10 |
dashboardSidebar(
|
| 11 |
width = 260,
|
| 12 |
sidebarMenu(
|
| 13 |
-
menuItem("
|
|
|
|
| 14 |
menuItem("Intervention Analysis", tabName = "its", icon = icon("flask")),
|
|
|
|
| 15 |
menuItem("Data Management", tabName = "admin", icon = icon("database")),
|
| 16 |
hr(),
|
| 17 |
div(style = "padding:10px;",
|
|
@@ -81,6 +83,59 @@ dashboardPage(
|
|
| 81 |
|
| 82 |
tabItems(
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# ββ EXECUTIVE DASHBOARD ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 85 |
tabItem(tabName = "dashboard",
|
| 86 |
|
|
@@ -195,7 +250,6 @@ dashboardPage(
|
|
| 195 |
"Grouped bars show Total Bid $ vs. Total Booked $ per estimator. Win % annotated on each booked bar. Sorted by Total Bid descending.")),
|
| 196 |
|
| 197 |
tabPanel("Seasonality Heatmap",
|
| 198 |
-
# ββ NEW: metric toggle ββββββββββββββββββββββββββββββββ
|
| 199 |
fluidRow(
|
| 200 |
column(12,
|
| 201 |
radioGroupButtons(
|
|
@@ -208,7 +262,6 @@ dashboardPage(
|
|
| 208 |
)
|
| 209 |
)
|
| 210 |
),
|
| 211 |
-
# ββ END NEW βββββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
plotlyOutput("seasonality_heatmap", height = "400px"),
|
| 213 |
p(class = "text-muted", style = "font-size:11px;",
|
| 214 |
"Revenue or bid volume by calendar month and year. Darker = higher value. Reveals consistent seasonal peaks and troughs across the full history."))
|
|
@@ -304,6 +357,66 @@ dashboardPage(
|
|
| 304 |
)
|
| 305 |
),
|
| 306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
# ββ DATA MANAGEMENT βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 308 |
tabItem(tabName = "admin",
|
| 309 |
|
|
@@ -335,4 +448,4 @@ dashboardPage(
|
|
| 335 |
)
|
| 336 |
)
|
| 337 |
)
|
| 338 |
-
)
|
|
|
|
| 10 |
dashboardSidebar(
|
| 11 |
width = 260,
|
| 12 |
sidebarMenu(
|
| 13 |
+
menuItem("Top Brief", tabName = "ceo_brief", icon = icon("briefcase")),
|
| 14 |
+
menuItem("Executive Dashboard", tabName = "dashboard", icon = icon("chart-area")),
|
| 15 |
menuItem("Intervention Analysis", tabName = "its", icon = icon("flask")),
|
| 16 |
+
menuItem("Estimator Scatter", tabName = "scatter", icon = icon("circle-dot")),
|
| 17 |
menuItem("Data Management", tabName = "admin", icon = icon("database")),
|
| 18 |
hr(),
|
| 19 |
div(style = "padding:10px;",
|
|
|
|
| 83 |
|
| 84 |
tabItems(
|
| 85 |
|
| 86 |
+
# ββ CEO BRIEF ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 87 |
+
tabItem(tabName = "ceo_brief",
|
| 88 |
+
fluidRow(
|
| 89 |
+
box(title = "Top Brief Calibration & Export",
|
| 90 |
+
status = "primary", solidHeader = TRUE, width = 12,
|
| 91 |
+
fluidRow(
|
| 92 |
+
column(2, sliderInput("ceo_w_win", "Weight: Win Quality", min = 0, max = 100, value = 35, step = 1)),
|
| 93 |
+
column(2, sliderInput("ceo_w_eff", "Weight: Efficiency", min = 0, max = 100, value = 20, step = 1)),
|
| 94 |
+
column(2, sliderInput("ceo_w_stab", "Weight: Stability", min = 0, max = 100, value = 20, step = 1)),
|
| 95 |
+
column(2, sliderInput("ceo_w_out", "Weight: Output", min = 0, max = 100, value = 25, step = 1)),
|
| 96 |
+
column(2, numericInput("ceo_risk_win", "At-Risk Win % <", value = 12, min = 1, max = 100, step = 1)),
|
| 97 |
+
column(2, numericInput("ceo_risk_cv", "At-Risk Volatility >", value = 1.25, min = 0.1, max = 3, step = 0.05))
|
| 98 |
+
),
|
| 99 |
+
fluidRow(
|
| 100 |
+
column(10, p(class = "text-muted", style = "font-size:11px;margin-top:6px;",
|
| 101 |
+
"Weights auto-normalize to 100%. Win threshold is percentage points. Volatility is coefficient of variation for booked revenue.")),
|
| 102 |
+
column(2, div(style = "margin-top:8px;",
|
| 103 |
+
downloadButton("download_top_brief", "Download Top Brief",
|
| 104 |
+
icon = icon("file-excel"),
|
| 105 |
+
style = "width:100%;font-weight:600;")))
|
| 106 |
+
)
|
| 107 |
+
)
|
| 108 |
+
),
|
| 109 |
+
|
| 110 |
+
fluidRow(
|
| 111 |
+
valueBoxOutput("ceo_total_pred_90d", width = 3),
|
| 112 |
+
valueBoxOutput("ceo_top_estimator", width = 3),
|
| 113 |
+
valueBoxOutput("ceo_at_risk_count", width = 3),
|
| 114 |
+
valueBoxOutput("ceo_model_quality", width = 3)
|
| 115 |
+
),
|
| 116 |
+
|
| 117 |
+
fluidRow(
|
| 118 |
+
box(title = "90-Day Booked Revenue Forecast by Estimator",
|
| 119 |
+
status = "primary", solidHeader = TRUE, width = 7,
|
| 120 |
+
plotlyOutput("ceo_forecast_plot", height = "380px"),
|
| 121 |
+
p(class = "text-muted", style = "font-size:11px;",
|
| 122 |
+
"Forecast uses estimator-level monthly history with lag features and seasonality. Error bars show an approximate 80% interval from in-sample model error.")
|
| 123 |
+
),
|
| 124 |
+
box(title = "Executive Action Summary",
|
| 125 |
+
status = "info", solidHeader = TRUE, width = 5,
|
| 126 |
+
uiOutput("ceo_action_summary"),
|
| 127 |
+
p(class = "text-muted", style = "font-size:11px;",
|
| 128 |
+
"Composite score combines win quality, efficiency, stability, and expected near-term revenue.")
|
| 129 |
+
)
|
| 130 |
+
),
|
| 131 |
+
|
| 132 |
+
fluidRow(
|
| 133 |
+
box(title = "Estimator Performance Scorecard",
|
| 134 |
+
status = "warning", solidHeader = TRUE, width = 12,
|
| 135 |
+
dataTableOutput("ceo_scorecard_table"))
|
| 136 |
+
)
|
| 137 |
+
),
|
| 138 |
+
|
| 139 |
# ββ EXECUTIVE DASHBOARD ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
tabItem(tabName = "dashboard",
|
| 141 |
|
|
|
|
| 250 |
"Grouped bars show Total Bid $ vs. Total Booked $ per estimator. Win % annotated on each booked bar. Sorted by Total Bid descending.")),
|
| 251 |
|
| 252 |
tabPanel("Seasonality Heatmap",
|
|
|
|
| 253 |
fluidRow(
|
| 254 |
column(12,
|
| 255 |
radioGroupButtons(
|
|
|
|
| 262 |
)
|
| 263 |
)
|
| 264 |
),
|
|
|
|
| 265 |
plotlyOutput("seasonality_heatmap", height = "400px"),
|
| 266 |
p(class = "text-muted", style = "font-size:11px;",
|
| 267 |
"Revenue or bid volume by calendar month and year. Darker = higher value. Reveals consistent seasonal peaks and troughs across the full history."))
|
|
|
|
| 357 |
)
|
| 358 |
),
|
| 359 |
|
| 360 |
+
# ββ ESTIMATOR SCATTER ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 361 |
+
tabItem(tabName = "scatter",
|
| 362 |
+
|
| 363 |
+
fluidRow(
|
| 364 |
+
box(
|
| 365 |
+
title = "Estimator Scatter \u2014 Bid Behavior vs. Conversion",
|
| 366 |
+
width = 12,
|
| 367 |
+
status = "primary",
|
| 368 |
+
solidHeader = TRUE,
|
| 369 |
+
|
| 370 |
+
# ββ Axis / bubble controls ββββββββββββββββββββββββββββββββ
|
| 371 |
+
fluidRow(
|
| 372 |
+
column(4,
|
| 373 |
+
selectInput(
|
| 374 |
+
inputId = "scatter_x",
|
| 375 |
+
label = "X Axis",
|
| 376 |
+
choices = c(
|
| 377 |
+
"Avg Bid Size ($)" = "avg_bid_size",
|
| 378 |
+
"Total Bid Volume ($)" = "total_amt_bid",
|
| 379 |
+
"Bid Count" = "total_qty_bid"
|
| 380 |
+
),
|
| 381 |
+
selected = "avg_bid_size"
|
| 382 |
+
)
|
| 383 |
+
),
|
| 384 |
+
column(4,
|
| 385 |
+
selectInput(
|
| 386 |
+
inputId = "scatter_y",
|
| 387 |
+
label = "Y Axis",
|
| 388 |
+
choices = c(
|
| 389 |
+
"Win Rate (%)" = "win_rate",
|
| 390 |
+
"Avg Booked Size ($)" = "avg_booked_size",
|
| 391 |
+
"Total Booked Volume ($)" = "total_amt_booked"
|
| 392 |
+
),
|
| 393 |
+
selected = "win_rate"
|
| 394 |
+
)
|
| 395 |
+
),
|
| 396 |
+
column(4,
|
| 397 |
+
selectInput(
|
| 398 |
+
inputId = "scatter_size",
|
| 399 |
+
label = "Bubble Size",
|
| 400 |
+
choices = c(
|
| 401 |
+
"Total Bid Volume ($)" = "total_amt_bid",
|
| 402 |
+
"Bid Count" = "total_qty_bid",
|
| 403 |
+
"Total Booked ($)" = "total_amt_booked"
|
| 404 |
+
),
|
| 405 |
+
selected = "total_amt_bid"
|
| 406 |
+
)
|
| 407 |
+
)
|
| 408 |
+
),
|
| 409 |
+
|
| 410 |
+
# ββ Plot βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 411 |
+
plotlyOutput("scatter_plot", height = "520px"),
|
| 412 |
+
|
| 413 |
+
# ββ Footnote βββββββββββββββββββββββββββββββββββββββββββββ
|
| 414 |
+
p(class = "text-muted", style = "font-size:11px; margin-top:6px;",
|
| 415 |
+
"Bubble area proportional to selected size metric. Win rate is same-month unless Lag > 0 is set in the sidebar, in which case the lag-adjusted rate is used. Estimators with fewer than 3 bids in the selected period are excluded. Hover for detail.")
|
| 416 |
+
)
|
| 417 |
+
)
|
| 418 |
+
),
|
| 419 |
+
|
| 420 |
# ββ DATA MANAGEMENT βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 421 |
tabItem(tabName = "admin",
|
| 422 |
|
|
|
|
| 448 |
)
|
| 449 |
)
|
| 450 |
)
|
| 451 |
+
)
|