Your Name commited on
Commit
91944c8
·
1 Parent(s): 0480ef8

Stable pre-release version — dashboard complete before go-live hardening

Browse files
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base image: A version of Linux with R and Shiny pre-installed
2
+ FROM --platform=linux/amd64 rocker/shiny-verse:latest
3
+
4
+ # Install Linux system libraries required for R packages
5
+ # (RPostgres needs libpq, Tidyverse needs xml2/ssl, etc.)
6
+ RUN apt-get update && apt-get install -y \
7
+ libpq-dev \
8
+ libssl-dev \
9
+ libxml2-dev \
10
+ zlib1g-dev \
11
+ && rm -rf /var/lib/apt/lists/*
12
+
13
+
14
+ RUN R -e "install.packages(c('shinydashboard', 'tidyverse', 'lubridate', 'plotly', 'DBI', 'RPostgres', 'scales', 'DT', 'shinyWidgets'), repos='https://cloud.r-project.org/')"
15
+
16
+ # Copy app code into the container
17
+ COPY app.R /srv/shiny-server/
18
+
19
+ # Expose the port used by Shiny
20
+ EXPOSE 3838
21
+
22
+ # Run the app
23
+ CMD ["R", "-e", "shiny::runApp('/srv/shiny-server/app.R', host = '0.0.0.0', port = 3838)"]
Estimator_Intelligence_2026-03-24.xlsx ADDED
Binary file (18.6 kB). View file
 
app.R CHANGED
The diff for this file is too large to render. See raw diff
 
docker-compose.yml ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ # Container 1: The Database
5
+ db:
6
+ image: postgres:latest
7
+ environment:
8
+ POSTGRES_USER: shiny_user
9
+ POSTGRES_PASSWORD: shiny_password
10
+ POSTGRES_DB: sales_db
11
+ volumes:
12
+ - postgres_data:/var/lib/postgresql/data
13
+ ports:
14
+ - "5432:5432"
15
+
16
+ # Container 2: R Shiny App
17
+ shiny_app:
18
+ build: .
19
+ ports:
20
+ - "3838:3838"
21
+ depends_on:
22
+ - db
23
+ environment:
24
+ # These variables override the settings in your R code
25
+ DB_HOST: db
26
+ DB_PORT: 5432
27
+ DB_USER: shiny_user
28
+ DB_PASS: shiny_password
29
+ DB_NAME: sales_db
30
+
31
+ volumes:
32
+ postgres_data:
fore_load.R DELETED
@@ -1,86 +0,0 @@
1
- library(DBI)
2
- library(RPostgres)
3
- library(tidyverse)
4
- library(lubridate)
5
- library(readr)
6
-
7
- # --- 1. SETTINGS ---
8
- CSV_FILENAME <- "Bid_SalesAnalysisByEstimatorByMonth_121125.csv"
9
- DB_PORT <- 5433
10
-
11
- print("--- DIAGNOSTIC START ---")
12
-
13
- # --- 2. CHECK FILE EXISTENCE ---
14
- # This checks if RStudio is looking in the right folder
15
- if (file.exists(CSV_FILENAME)) {
16
- print(paste("✅ SUCCESS: Found file:", CSV_FILENAME))
17
- } else {
18
- print(paste("❌ ERROR: Could not find file:", CSV_FILENAME))
19
- print(paste(" R is currently looking in:", getwd()))
20
- stop("Stoppping script. Please move the CSV to the folder listed above.")
21
- }
22
-
23
- # --- 3. CONNECT TO DATABASE ---
24
- tryCatch({
25
- con <- dbConnect(RPostgres::Postgres(),
26
- dbname = "sales_db",
27
- host = "localhost",
28
- port = DB_PORT,
29
- user = Sys.info()[["user"]],
30
- password = "")
31
- print("✅ SUCCESS: Connected to Database.")
32
- }, error = function(e) {
33
- print("❌ ERROR: Could not connect to Database. Make sure Postgres.app is running.")
34
- stop(e)
35
- })
36
-
37
- # --- 4. LOAD DATA ---
38
- print("--- Attempting Data Load ---")
39
-
40
- raw_data <- read_csv(CSV_FILENAME, show_col_types = FALSE)
41
-
42
- clean_data <- raw_data %>%
43
- mutate(report_date = ym(MoYear)) %>%
44
- mutate(across(c(AmtBid, AmtBooked), ~ as.numeric(gsub("[\\$,]", "", .)))) %>%
45
- mutate(clean_name = case_when(
46
- Estimator == "Scott W. Hutchings" ~ "Scott Hutchings",
47
- Estimator == "SW" ~ "Scott Hutchings",
48
- Estimator == "SH" ~ "Scott Hutchings",
49
- TRUE ~ Estimator
50
- ))
51
-
52
- # Insert Estimator Names
53
- unique_estimators <- unique(clean_data$clean_name)
54
- count_new_names <- 0
55
-
56
- for(name in unique_estimators){
57
- exists <- dbGetQuery(con, "SELECT id FROM estimators WHERE primary_name = $1", params = list(name))
58
- if(nrow(exists) == 0){
59
- dbExecute(con, "INSERT INTO estimators (primary_name) VALUES ($1)", params = list(name))
60
- count_new_names <- count_new_names + 1
61
- }
62
- }
63
- print(paste(" Processed Estimators. Added", count_new_names, "new names."))
64
-
65
- # Insert Sales Data
66
- est_map <- dbGetQuery(con, "SELECT id, primary_name FROM estimators")
67
-
68
- upload_ready <- clean_data %>%
69
- left_join(est_map, by = c("clean_name" = "primary_name")) %>%
70
- select(estimator_id = id, report_date, amt_bid = AmtBid, qty_bid = QtyBid,
71
- amt_booked = AmtBooked, qty_booked = QtyBooked)
72
-
73
- # Write to DB
74
- tryCatch({
75
- dbWriteTable(con, "monthly_sales", upload_ready, append = TRUE, row.names = FALSE)
76
- print(paste("✅ SUCCESS: Uploaded", nrow(upload_ready), "sales records."))
77
- }, error = function(e) {
78
- print("⚠️ NOTE: Data write skipped (likely duplicate data).")
79
- })
80
-
81
- # --- 5. FINAL VERIFICATION ---
82
- row_count <- dbGetQuery(con, "SELECT count(*) FROM monthly_sales")
83
- print(paste("--- FINAL DATABASE STATUS: The table now has", row_count, "rows. ---"))
84
-
85
- dbDisconnect(con)
86
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ml_pipeline.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sqlalchemy import create_engine
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.ensemble import RandomForestRegressor
5
+ from sklearn.metrics import mean_absolute_error
6
+
7
+ # 1. CONNECT TO YOUR EXISTING DOCKER DATABASE
8
+ # Format: postgresql://username:password@host:port/database_name
9
+ # (Using the port 5432 mapped in your docker-compose.yml)
10
+ db_url = "postgresql://shiny_user:shiny_password@localhost:5432/sales_db"
11
+ engine = create_engine(db_url)
12
+
13
+ # 2. EXTRACT THE DATA
14
+ query = """
15
+ SELECT e.primary_name, s.report_date, s.amt_bid, s.qty_bid, s.amt_booked
16
+ FROM monthly_sales s
17
+ JOIN estimators e ON s.estimator_id = e.id
18
+ """
19
+ df = pd.read_sql(query, engine)
20
+
21
+ print(f"Successfully loaded {len(df)} records from PostgreSQL.")
22
+
23
+ # 3. FEATURE ENGINEERING (Preparing data for the ML model)
24
+ # Extract the month (1-12) to capture Seasonality
25
+ df['month'] = pd.to_datetime(df['report_date']).dt.month
26
+
27
+ # Define our Features (X) and Target (y)
28
+ # We want to predict Booked Revenue based on Bid Amount, Bid Qty, and Seasonality
29
+ X = df[['amt_bid', 'qty_bid', 'month']]
30
+ y = df['amt_booked']
31
+
32
+ # Split data into Training and Testing sets (A core CS229 concept)
33
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
34
+
35
+ # 4. TRAIN THE MACHINE LEARNING MODEL
36
+ print("Training Random Forest Regressor...")
37
+ model = RandomForestRegressor(n_estimators=100, random_state=42)
38
+ model.fit(X_train, y_train)
39
+
40
+ # 5. EVALUATE THE MODEL
41
+ predictions = model.predict(X_test)
42
+ error = mean_absolute_error(y_test, predictions)
43
+ print(f"Model Mean Absolute Error: ${error:,.2f}")
44
+
45
+ # 6. MAKE A NEW PREDICTION FOR THE BOSS
46
+ # Scenario: Larry Howard just bid $5,000,000 across 8 jobs in Month 4 (April).
47
+ # What will he actually book?
48
+ new_data = pd.DataFrame({'amt_bid': [5000000], 'qty_bid': [8], 'month': [4]})
49
+ predicted_revenue = model.predict(new_data)[0]
50
+
51
+ print(f"\n--- EXECUTIVE ML PREDICTION ---")
52
+ print(f"Based on a $5M pipeline of 8 jobs in April,")
53
+ print(f"the ML Model predicts we will book: ${predicted_revenue:,.2f}")
report.Rmd ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: "Estimator Intelligence Report"
3
+ subtitle: "Preconstruction Performance Analysis"
4
+ date: "`r params$report_date`"
5
+ output:
6
+ pdf_document:
7
+ latex_engine: xelatex
8
+ toc: true
9
+ toc_depth: 2
10
+ number_sections: false
11
+ fig_caption: true
12
+ fig_height: 4
13
+ fig_width: 7
14
+ keep_tex: false
15
+ header-includes:
16
+ - \usepackage{booktabs}
17
+ - \usepackage{longtable}
18
+ - \usepackage{array}
19
+ - \usepackage{multirow}
20
+ - \usepackage{xcolor}
21
+ - \usepackage{geometry}
22
+ - \usepackage{fancyhdr}
23
+ - \usepackage{titling}
24
+ - \usepackage{float}
25
+ - \geometry{margin=1in, top=0.9in, bottom=0.9in}
26
+ - \definecolor{primary}{HTML}{2c3e50}
27
+ - \definecolor{success}{HTML}{27ae60}
28
+ - \definecolor{info}{HTML}{3c8dbc}
29
+ - \definecolor{muted}{HTML}{7f8c8d}
30
+ - \definecolor{graybg}{HTML}{f4f6f9}
31
+ - \pagestyle{fancy}
32
+ - \fancyhf{}
33
+ - \fancyhead[L]{\textcolor{primary}{\textbf{\small Estimator Intelligence}}}
34
+ - \fancyhead[R]{\textcolor{info}{\small \thepage}}
35
+ - \fancyfoot[C]{\textcolor{muted}{\footnotesize Confidential — Internal Use Only}}
36
+ - \renewcommand{\headrulewidth}{0.4pt}
37
+ - \renewcommand{\headrule}{\hbox to\headwidth{\color{primary}\leaders\hrule height \headrulewidth\hfill}}
38
+ params:
39
+ data_path: NULL
40
+ date_start: "N/A"
41
+ date_end: "N/A"
42
+ estimators: "All Estimators"
43
+ win_lag: 3
44
+ report_date: "N/A"
45
+ ---
46
+
47
+ ```{r setup, include=FALSE}
48
+ knitr::opts_chunk$set(
49
+ echo = FALSE,
50
+ warning = FALSE,
51
+ message = FALSE,
52
+ fig.align = "center",
53
+ fig.pos = "H"
54
+ )
55
+
56
+ library(tidyverse)
57
+ library(lubridate)
58
+ library(scales)
59
+ library(knitr)
60
+ library(kableExtra)
61
+
62
+ # ── Load data ────────────────────────────────────────────────────────────────
63
+ df <- readRDS(params$data_path)
64
+
65
+ # ── Color palette (Dashboard Matching) ───────────────────────────────────────
66
+ col_primary <- "#2c3e50" # Dark Navy/Slate
67
+ col_success <- "#27ae60" # Green (Booked)
68
+ col_info <- "#3c8dbc" # Light Blue
69
+ col_danger <- "#c0392b" # Red (UCL/LCL)
70
+ col_anomaly <- "#e74c3c" # Bright Red
71
+ col_bid <- "#95a5a6" # Grey (Bid Volume)
72
+ col_mean <- "#2980b9" # Blue (SPC Mean)
73
+ col_bg <- "#f4f6f9" # Dashboard content wrapper background
74
+
75
+ # ── ggplot theme ─────────────────────────────────────────────────────────────
76
+ theme_report <- function() {
77
+ theme_minimal(base_size = 10) %+replace%
78
+ theme(
79
+ plot.background = element_rect(fill = "white", color = NA),
80
+ panel.background = element_rect(fill = "white", color = NA),
81
+ panel.grid.major = element_line(color = "#ecf0f5", linewidth = 0.5),
82
+ panel.grid.minor = element_blank(),
83
+ axis.text = element_text(color = "#333333", size = 8),
84
+ axis.title = element_text(color = col_primary, size = 9, face = "bold"),
85
+ plot.title = element_text(color = col_primary, size = 11, face = "bold", hjust = 0),
86
+ plot.subtitle = element_text(color = "#7f8c8d", size = 8, hjust = 0),
87
+ legend.background = element_rect(fill = "white", color = NA),
88
+ legend.text = element_text(color = "#333333", size = 8),
89
+ legend.title = element_text(color = col_primary, size = 8, face = "bold"),
90
+ strip.background = element_rect(fill = col_bg, color = NA),
91
+ strip.text = element_text(color = col_primary, size = 8, face = "bold")
92
+ )
93
+ }
94
+
95
+ # ── KPI helpers ───────────────────────────────────────────────────────────────
96
+ total_bid <- sum(df$amt_bid, na.rm = TRUE)
97
+ total_booked <- sum(df$amt_booked, na.rm = TRUE)
98
+ win_rate_pct <- if(total_bid > 0) total_booked / total_bid else 0
99
+ job_win_rate <- if(sum(df$qty_bid, na.rm = TRUE) > 0) sum(df$qty_booked, na.rm = TRUE) / sum(df$qty_bid, na.rm = TRUE) else 0
100
+ n_estimators <- length(unique(df$primary_name))
101
+
102
+ fmt_dollar <- function(x) dollar(x, accuracy = 1, scale = 1e-3, suffix = "K")
103
+
104
+
105
+ \newpage
106
+
107
+ \textcolor{primary}{\Large\textbf{Report Summary}}
108
+
109
+ \vspace{0.3cm}
110
+
111
+ \begin{tabular}{ll}
112
+ \textbf{Reporting Period:} & r params$date_start — r params$date_end \
113
+ \textbf{Estimator Scope:} & r params$estimators \
114
+ \textbf{Win Rate Lag:} & r params$win_lag month(s) \
115
+ \textbf{Generated:} & r params$report_date \
116
+ \end{tabular}
117
+
118
+ \vspace{0.5cm}
119
+ \hrule
120
+ \vspace{0.5cm}
121
+
122
+ Executive KPIs
123
+
124
+ kpi_df <- tibble(
125
+ Metric = c(
126
+ "Total Bid Volume",
127
+ "Total Booked Revenue",
128
+ "Dollar Win Rate (same-month)",
129
+ "Job Win Rate (same-month)",
130
+ "Active Estimators in Period"
131
+ ),
132
+ Value = c(
133
+ dollar(total_bid, accuracy = 1),
134
+ dollar(total_booked, accuracy = 1),
135
+ percent(win_rate_pct, accuracy = 0.1),
136
+ percent(job_win_rate, accuracy = 0.1),
137
+ as.character(n_estimators)
138
+ )
139
+ )
140
+
141
+ kable(kpi_df, booktabs = TRUE, col.names = c("Metric", "Value"),
142
+ caption = "Key Performance Indicators for the Selected Period") %>%
143
+ kable_styling(latex_options = c("striped", "HOLD_position"),
144
+ stripe_color = "#f4f6f9",
145
+ full_width = FALSE,
146
+ position = "left") %>%
147
+ column_spec(1, bold = TRUE, width = "8cm") %>%
148
+ column_spec(2, width = "4cm") %>%
149
+ row_spec(0, background = "#2c3e50", color = "white", bold = TRUE)
150
+
151
+ \newpage
152
+
153
+ annual <- df %>%
154
+ mutate(Year = year(report_date)) %>%
155
+ group_by(Year) %>%
156
+ summarise(
157
+ Bid = sum(amt_bid, na.rm = TRUE),
158
+ Booked = sum(amt_booked, na.rm = TRUE),
159
+ .groups = "drop"
160
+ ) %>%
161
+ pivot_longer(c(Bid, Booked), names_to = "Type", values_to = "Amount")
162
+
163
+ ggplot(annual, aes(factor(Year), Amount, fill = Type)) +
164
+ geom_col(position = "dodge", width = 0.7) +
165
+ scale_fill_manual(values = c("Bid" = col_bid, "Booked" = col_success)) +
166
+ scale_y_continuous(labels = function(x) dollar(x, accuracy = 1, scale = 1e-3, suffix = "K")) +
167
+ labs(x = "Year", y = "Amount ($)", fill = NULL,
168
+ title = "Annual Bid vs. Booked Revenue",
169
+ subtitle = paste("Period:", params$date_start, "—", params$date_end)) +
170
+ theme_report() +
171
+ theme(axis.text.x = element_text(angle = 45, hjust = 1))
172
+
173
+ \newpage
174
+
175
+ lb <- df %>%
176
+ group_by(primary_name) %>%
177
+ summarise(
178
+ `Total Bids` = sum(amt_bid, na.rm = TRUE),
179
+ `Total Revenue` = sum(amt_booked, na.rm = TRUE),
180
+ `Jobs Bid` = sum(qty_bid, na.rm = TRUE),
181
+ `Jobs Won` = sum(qty_booked, na.rm = TRUE),
182
+ .groups = "drop"
183
+ ) %>%
184
+ mutate(
185
+ `Win Rate` = if_else(`Total Bids` > 0, `Total Revenue` / `Total Bids`, 0),
186
+ `Avg Job Size` = if_else(`Jobs Bid` > 0, `Total Bids` / `Jobs Bid`, 0)
187
+ ) %>%
188
+ arrange(desc(`Total Revenue`)) %>%
189
+ mutate(
190
+ `Total Bids` = dollar(`Total Bids`, accuracy = 1),
191
+ `Total Revenue` = dollar(`Total Revenue`, accuracy = 1),
192
+ `Win Rate` = percent(`Win Rate`, accuracy = 0.1),
193
+ `Avg Job Size` = dollar(`Avg Job Size`, accuracy = 1)
194
+ ) %>%
195
+ rename(Estimator = primary_name)
196
+
197
+ kable(lb, booktabs = TRUE, longtable = TRUE,
198
+ caption = "Full Estimator Performance Summary — Selected Period") %>%
199
+ kable_styling(latex_options = c("striped", "HOLD_position", "repeat_header"),
200
+ stripe_color = "#f4f6f9",
201
+ font_size = 9,
202
+ full_width = FALSE) %>%
203
+ row_spec(0, background = "#2c3e50", color = "white", bold = TRUE) %>%
204
+ column_spec(1, bold = TRUE)
205
+
206
+ \newpage
207
+
208
+ seasonal <- df %>%
209
+ mutate(
210
+ Month_Num = month(report_date),
211
+ Month_Name = month(report_date, label = TRUE, abbr = FALSE),
212
+ Year = year(report_date)
213
+ ) %>%
214
+ group_by(Month_Num, Month_Name, Year) %>%
215
+ summarise(Booked = sum(amt_booked, na.rm = TRUE), .groups = "drop") %>%
216
+ group_by(Month_Num, Month_Name) %>%
217
+ summarise(Avg_Booked = mean(Booked, na.rm = TRUE), .groups = "drop") %>%
218
+ arrange(Month_Num)
219
+
220
+ ggplot(seasonal, aes(x = fct_reorder(Month_Name, Month_Num), y = Avg_Booked)) +
221
+ geom_col(fill = col_info, width = 0.7) +
222
+ geom_text(aes(label = dollar(Avg_Booked, accuracy = 1, scale = 1e-3, suffix = "K")),
223
+ vjust = -0.4, size = 2.8, color = col_primary) +
224
+ scale_y_continuous(
225
+ labels = function(x) dollar(x, accuracy = 1, scale = 1e-3, suffix = "K"),
226
+ expand = expansion(mult = c(0, 0.15))
227
+ ) +
228
+ labs(x = NULL, y = "Avg Booked Revenue ($K)",
229
+ title = "Average Booked Revenue by Calendar Month",
230
+ subtitle = "Historical average across all years in dataset") +
231
+ theme_report() +
232
+ theme(axis.text.x = element_text(angle = 35, hjust = 1),
233
+ panel.grid.major.x = element_blank())
234
+
235
+ \vspace{1cm}
236
+ \begin{center}
237
+ \textcolor{primary}{\rule{0.4\linewidth}{0.6pt}} \quad \textcolor{muted}{\footnotesize Generated by Estimator Intelligence Dashboard} \quad \textcolor{primary}{\rule{0.4\linewidth}{0.6pt}}
238
+ \end{center}
sales_db.R CHANGED
@@ -2,7 +2,7 @@ library(DBI)
2
  library(RPostgres)
3
 
4
  # 1. Connect to the system database to create your App DB
5
- # Note: We use Sys.info()[["user"]] to get your Mac username automatically
6
  sys_con <- dbConnect(RPostgres::Postgres(),
7
  dbname = "postgres",
8
  host = "localhost",
@@ -29,7 +29,7 @@ con <- dbConnect(RPostgres::Postgres(),
29
  password = "")
30
 
31
  # 4. Create Tables (Embedded SQL to ensure it works)
32
- # Create Estimators Table
33
  dbExecute(con, "
34
  CREATE TABLE IF NOT EXISTS estimators (
35
  id SERIAL PRIMARY KEY,
 
2
  library(RPostgres)
3
 
4
  # 1. Connect to the system database to create your App DB
5
+
6
  sys_con <- dbConnect(RPostgres::Postgres(),
7
  dbname = "postgres",
8
  host = "localhost",
 
29
  password = "")
30
 
31
  # 4. Create Tables (Embedded SQL to ensure it works)
32
+
33
  dbExecute(con, "
34
  CREATE TABLE IF NOT EXISTS estimators (
35
  id SERIAL PRIMARY KEY,
setup_database.sql CHANGED
@@ -2,16 +2,14 @@ library(DBI)
2
  library(RPostgres)
3
  library(readr) # To read your .sql file
4
 
5
- # 1. Connect to the 'default' postgres database to create your specific App DB
6
- # (You usually can't create a database while connected to it, so we connect to 'postgres' first)
7
  sys_con <- dbConnect(RPostgres::Postgres(),
8
  dbname = "postgres", # Default system DB
9
  host = "localhost",
10
  user = "postgres",
11
  password = "your_password_here") # <--- UPDATE THIS
12
 
13
- # 2. Create the new database (if it doesn't exist)
14
- # We wrap in tryCatch because if it exists, it throws an error.
15
  tryCatch({
16
  dbExecute(sys_con, "CREATE DATABASE sales_db;")
17
  print("Database 'sales_db' created successfully.")
@@ -21,19 +19,17 @@ tryCatch({
21
 
22
  dbDisconnect(sys_con) # Disconnect from system DB
23
 
24
- # 3. Connect to YOUR new 'sales_db'
25
  con <- dbConnect(RPostgres::Postgres(),
26
  dbname = "sales_db",
27
  host = "localhost",
28
  user = "postgres",
29
- password = "ggdata2014") # <--- UPDATE THIS
 
30
 
31
- # 4. Read your SQL file and execute it
32
- # This assumes 'database.sql' is in your R Project root folder
33
  sql_commands <- read_file("database.sql")
34
 
35
- # RPostgres sometimes struggles with multiple SQL commands in one string.
36
- # We split them by the semicolon to run them one by one.
37
  statements <- unlist(strsplit(sql_commands, ";"))
38
 
39
  # Execute each statement
 
2
  library(RPostgres)
3
  library(readr) # To read your .sql file
4
 
5
+
 
6
  sys_con <- dbConnect(RPostgres::Postgres(),
7
  dbname = "postgres", # Default system DB
8
  host = "localhost",
9
  user = "postgres",
10
  password = "your_password_here") # <--- UPDATE THIS
11
 
12
+
 
13
  tryCatch({
14
  dbExecute(sys_con, "CREATE DATABASE sales_db;")
15
  print("Database 'sales_db' created successfully.")
 
19
 
20
  dbDisconnect(sys_con) # Disconnect from system DB
21
 
22
+
23
  con <- dbConnect(RPostgres::Postgres(),
24
  dbname = "sales_db",
25
  host = "localhost",
26
  user = "postgres",
27
+ password = "ggdata2014") #
28
+
29
 
 
 
30
  sql_commands <- read_file("database.sql")
31
 
32
+
 
33
  statements <- unlist(strsplit(sql_commands, ";"))
34
 
35
  # Execute each statement