sugitora commited on
Commit
6a0fbf7
·
verified ·
1 Parent(s): c19fb06

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +170 -194
app.R CHANGED
@@ -6,29 +6,6 @@ library(corrplot)
6
  library(plotly)
7
  library(gridExtra)
8
 
9
- # データの読み込みと前処理
10
- load_and_prepare_data <- function(file_path) {
11
- # CSVファイルを読み込み
12
- df <- read.csv(file_path, row.names = 1, check.names = FALSE)
13
-
14
- # 行名を列に追加
15
- df <- df %>%
16
- mutate(年月 = rownames(df), .before = 1) %>%
17
- rownames_to_column(var = "index") %>%
18
- select(-index)
19
-
20
- return(df)
21
- }
22
-
23
- # データを準備
24
- data_file <- "SampleData.csv"
25
-
26
- # アプリが実行されるディレクトリを確認して適切なパスを設定
27
- if (!file.exists(data_file)) {
28
- # Shinyサーバーで実行される場合のパス
29
- data_file <- file.path(dirname(rstudioapi::getSourceEditorContext()$path), "SampleData.csv")
30
- }
31
-
32
  ui <- fluidPage(
33
  # タイトルと概要
34
  titlePanel("経営指標分析ダッシュボード"),
@@ -48,150 +25,148 @@ ui <- fluidPage(
48
  tabsetPanel(
49
  # タブ1: 相関分析
50
  tabPanel("相関分析",
51
- fluidRow(
52
- column(12,
53
- h3("指標間の相関係数(ヒートマップ)"),
54
- p("各ビジネス部門の売上、原価、粗利、営業利益、KPIなど全指標間の相関を表示します。")
55
- )
56
- ),
57
- fluidRow(
58
- column(8, plotOutput("correlation_heatmap", height = "600px")),
59
- column(4,
60
- wellPanel(
61
- h4("相関分析の概要"),
62
- verbatimTextOutput("correlation_summary")
63
- )
64
- )
65
- )
66
  ),
67
 
68
  # タブ2: 回帰分析
69
  tabPanel("回帰分析",
70
- fluidRow(
71
- column(12, h3("KPIと売上・利益の関係分析"))
72
- ),
73
- fluidRow(
74
- column(6,
75
- wellPanel(
76
- h4("回帰分析の設定"),
77
- selectInput("x_var", "説明変数(X軸)",
78
- c("整備_車検件数" = "x25",
79
- "整備_整備・板金数" = "x31",
80
- "運送_自社ドライバー数" = "x67",
81
- "運送_他社ドライバー数" = "x73",
82
- "レンタル_レンタル台数" = "x91")),
83
- selectInput("y_var", "被説明変数(Y軸)",
84
- c("整備_売上" = "x2",
85
- "整備_一般車検_売上" = "x3",
86
- "整備_板金_売上" = "x4",
87
- "整備_営業利益" = "x9",
88
- "商品_売上" = "x37",
89
- "商品_営業利益" = "x41",
90
- "運送_売上" = "x52",
91
- "運送_営業利益" = "x56",
92
- "レンタル_売上" = "x76",
93
- "レンタル_営業利益" = "x80")),
94
- actionButton("update_regression", "更新", class = "btn-primary")
95
- )
96
- )
97
- ),
98
- fluidRow(
99
- column(6, plotlyOutput("regression_scatter")),
100
- column(6,
101
- wellPanel(
102
- h4("回帰分析結果"),
103
- verbatimTextOutput("regression_results")
104
- )
105
- )
106
- )
107
  ),
108
 
109
  # タブ3: 時系列分析
110
  tabPanel("時系列分析",
111
- fluidRow(
112
- column(12, h3("各部門の営業利益推移"))
113
- ),
114
- fluidRow(
115
- column(12, plotlyOutput("timeseries_profit", height = "500px"))
116
- ),
117
- fluidRow(
118
- column(12, h3("売上高の推移"))
119
- ),
120
- fluidRow(
121
- column(12, plotlyOutput("timeseries_sales", height = "500px"))
122
- ),
123
- fluidRow(
124
- column(12, h3("KPI推移"))
125
- ),
126
- fluidRow(
127
- column(12, plotlyOutput("timeseries_kpi", height = "500px"))
128
- )
129
  ),
130
 
131
  # タブ4: 統計サマリー
132
  tabPanel("統計サマリー",
133
- fluidRow(
134
- column(12, h3("基本統計量"))
135
- ),
136
- fluidRow(
137
- column(6,
138
- wellPanel(
139
- h4("整備部門"),
140
- verbatimTextOutput("summary_seisetsubi")
141
- )
142
- ),
143
- column(6,
144
- wellPanel(
145
- h4("商品部門"),
146
- verbatimTextOutput("summary_shohin")
147
- )
148
- )
149
- ),
150
- fluidRow(
151
- column(6,
152
- wellPanel(
153
- h4("運送部門"),
154
- verbatimTextOutput("summary_unsou")
155
- )
156
- ),
157
- column(6,
158
- wellPanel(
159
- h4("レンタル部門"),
160
- verbatimTextOutput("summary_rental")
161
- )
162
- )
163
- )
164
  ),
165
 
166
  # タブ5: 詳細な散布図分析
167
  tabPanel("詳細分析",
168
- fluidRow(
169
- column(12, h3("主指標の散布図行列"))
170
- ),
171
- fluidRow(
172
- column(12,
173
- wellPanel(
174
- h4("分析対象指標の選択(複数選択可)"),
175
- checkboxGroupInput("scatter_vars", "変数を選択",
176
- c("整備_売上" = "x2",
177
- "整備_粗利" = "x7",
178
- "整備_営業利益" = "x9",
179
- "商品_売上" = "x37",
180
- "商品_粗利" = "x39",
181
- "商品_営業利益" = "x41",
182
- "運送_売上" = "x52",
183
- "運送_粗利" = "x54",
184
- "運送_営業利益" = "x56",
185
- "レンタル_売上" = "x76",
186
- "レンタル_粗利" = "x78",
187
- "レンタル_営業利益" = "x80"),
188
- selected = c("x2", "x9", "x37", "x52"))
189
- )
190
- )
191
- ),
192
- fluidRow(
193
- column(12, plotOutput("scatter_matrix", height = "800px"))
194
- )
195
  )
196
  )
197
  )
@@ -200,11 +175,12 @@ server <- function(input, output, session) {
200
 
201
  # データの読み込み
202
  df <- reactive({
203
- # ファイルパスを設定(MacOS対応)
204
  possible_paths <- c(
205
- "SampleData.csv",
206
- file.path(getwd(), "SampleData.csv"),
207
- "/mnt/user-data/uploads/SampleData.csv"
 
208
  )
209
 
210
  for (path in possible_paths) {
@@ -227,7 +203,7 @@ server <- function(input, output, session) {
227
  }
228
 
229
  # ファイルが見つからない場合は警告
230
- showNotification("SampleData.csvが見つかりません", type = "error")
231
  return(NULL)
232
  })
233
 
@@ -293,9 +269,9 @@ server <- function(input, output, session) {
293
 
294
  # ヒートマップを作成
295
  corrplot(corr_matrix, method = "color", type = "full",
296
- tl.cex = 0.7, tl.col = "black",
297
- addCoef.col = "white", number.cex = 0.6,
298
- title = "相関係数ヒートマップ", mar = c(0, 0, 2, 0))
299
  }, error = function(e) {
300
  plot(1, 1, type="n", xlab="", ylab="", main="エラー")
301
  text(1, 1, paste("エラー:", e$message), cex=1.2, col="red")
@@ -333,11 +309,11 @@ server <- function(input, output, session) {
333
  cat("相関係数が強い上位10の組み合わせ:\n\n")
334
  for (i in 1:nrow(corr_pairs)) {
335
  cat(sprintf("%s (%s) と %s (%s): %.3f\n",
336
- corr_pairs$var1_label[i],
337
- corr_pairs$var1_code[i],
338
- corr_pairs$var2_label[i],
339
- corr_pairs$var2_code[i],
340
- corr_pairs$correlation[i]))
341
  }
342
  }, error = function(e) {
343
  cat("エラーが発生しました:", e$message)
@@ -376,9 +352,9 @@ server <- function(input, output, session) {
376
  geom_point(size = 3, color = "steelblue", alpha = 0.6) +
377
  geom_smooth(method = "lm", se = TRUE, color = "red", fill = "red", alpha = 0.2) +
378
  labs(title = paste(y_label, "vs", x_label),
379
- subtitle = sprintf("(%s) vs (%s)", input$y_var, input$x_var),
380
- x = x_label,
381
- y = y_label) +
382
  theme_minimal() +
383
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
384
 
@@ -410,9 +386,9 @@ server <- function(input, output, session) {
410
  data <- df() %>%
411
  select(年月, x9, x41, x56, x80) %>%
412
  rename("整備営業利益" = x9,
413
- "商品営業利益" = x41,
414
- "運送営業利益" = x56,
415
- "レンタル営業利益" = x80) %>%
416
  gather(key = "部門", value = "営業利益", -年月)
417
 
418
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
@@ -422,12 +398,12 @@ server <- function(input, output, session) {
422
  geom_line(size = 1) +
423
  geom_point(size = 2) +
424
  labs(title = "各部門の営業利益推移",
425
- x = "年月",
426
- y = "営業利益(円)",
427
- color = "部門") +
428
  theme_minimal() +
429
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
430
- plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
431
 
432
  ggplotly(p)
433
  })
@@ -438,9 +414,9 @@ server <- function(input, output, session) {
438
  data <- df() %>%
439
  select(年月, x2, x37, x52, x76) %>%
440
  rename("整備売上" = x2,
441
- "商品売上" = x37,
442
- "運送売上" = x52,
443
- "レンタル売上" = x76) %>%
444
  gather(key = "部門", value = "売上", -年月)
445
 
446
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
@@ -450,12 +426,12 @@ server <- function(input, output, session) {
450
  geom_line(size = 1) +
451
  geom_point(size = 2) +
452
  labs(title = "各部門の売上高推移",
453
- x = "年月",
454
- y = "売上(円)",
455
- color = "部門") +
456
  theme_minimal() +
457
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
458
- plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
459
 
460
  ggplotly(p)
461
  })
@@ -466,10 +442,10 @@ server <- function(input, output, session) {
466
  data <- df() %>%
467
  select(年月, x25, x31, x67, x73, x91) %>%
468
  rename("車検件数" = x25,
469
- "整備・板金数" = x31,
470
- "自社ドライバー数" = x67,
471
- "他社ドライバー数" = x73,
472
- "レンタル台数" = x91) %>%
473
  gather(key = "KPI", value = "件数", -年月)
474
 
475
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
@@ -479,12 +455,12 @@ server <- function(input, output, session) {
479
  geom_line(size = 1) +
480
  geom_point(size = 2) +
481
  labs(title = "KPI推移",
482
- x = "年月",
483
- y = "件数・台数",
484
- color = "KPI") +
485
  theme_minimal() +
486
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
487
- plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
488
 
489
  ggplotly(p)
490
  })
@@ -582,11 +558,11 @@ server <- function(input, output, session) {
582
 
583
  # 散布図行列を作成
584
  pairs(data_clean,
585
- panel = function(x, y, ...) {
586
- points(x, y, col = "steelblue", cex = 0.7)
587
- abline(lm(y ~ x), col = "red", lty = 2)
588
- },
589
- main = "変数間の散布図行列")
590
  }, error = function(e) {
591
  plot(1, 1, type="n", xlab="", ylab="")
592
  text(1, 1, paste("エラー:", e$message), cex=1.2, col="red")
 
6
  library(plotly)
7
  library(gridExtra)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  ui <- fluidPage(
10
  # タイトルと概要
11
  titlePanel("経営指標分析ダッシュボード"),
 
25
  tabsetPanel(
26
  # タブ1: 相関分析
27
  tabPanel("相関分析",
28
+ fluidRow(
29
+ column(12,
30
+ h3("指標間の相関係数(ヒートマップ)"),
31
+ p("各ビジネス部門の売上、原価、粗利、営業利益、KPIなど全指標間の相関を表示します。")
32
+ )
33
+ ),
34
+ fluidRow(
35
+ column(8, plotOutput("correlation_heatmap", height = "600px")),
36
+ column(4,
37
+ wellPanel(
38
+ h4("相関分析の概要"),
39
+ verbatimTextOutput("correlation_summary")
40
+ )
41
+ )
42
+ )
43
  ),
44
 
45
  # タブ2: 回帰分析
46
  tabPanel("回帰分析",
47
+ fluidRow(
48
+ column(12, h3("KPIと売上・利益の関係分析"))
49
+ ),
50
+ fluidRow(
51
+ column(6,
52
+ wellPanel(
53
+ h4("回帰分析の設定"),
54
+ selectInput("x_var", "説明変数(X軸)",
55
+ c("整備_車検件数" = "x25",
56
+ "整備_整備・板金数" = "x31",
57
+ "運送_自社ドライバー数" = "x67",
58
+ "運送_他社ドライバー数" = "x73",
59
+ "レンタル_レンタル台数" = "x91")),
60
+ selectInput("y_var", "被説明変数(Y軸)",
61
+ c("整備_売上" = "x2",
62
+ "整備_営業利益" = "x9",
63
+ "商品_売上" = "x37",
64
+ "商品_営業利益" = "x41",
65
+ "運送_売上" = "x52",
66
+ "運送_営業利益" = "x56",
67
+ "レンタル_売上" = "x76",
68
+ "レンタル_営業利益" = "x80")),
69
+ actionButton("update_regression", "更新", class = "btn-primary")
70
+ )
71
+ )
72
+ ),
73
+ fluidRow(
74
+ column(6, plotlyOutput("regression_scatter")),
75
+ column(6,
76
+ wellPanel(
77
+ h4("回帰分析結果"),
78
+ verbatimTextOutput("regression_results")
79
+ )
80
+ )
81
+ )
 
 
82
  ),
83
 
84
  # タブ3: 時系列分析
85
  tabPanel("時系列分析",
86
+ fluidRow(
87
+ column(12, h3("各部門の営業利益推移"))
88
+ ),
89
+ fluidRow(
90
+ column(12, plotlyOutput("timeseries_profit", height = "500px"))
91
+ ),
92
+ fluidRow(
93
+ column(12, h3("売上高の推移"))
94
+ ),
95
+ fluidRow(
96
+ column(12, plotlyOutput("timeseries_sales", height = "500px"))
97
+ ),
98
+ fluidRow(
99
+ column(12, h3("KPI推移"))
100
+ ),
101
+ fluidRow(
102
+ column(12, plotlyOutput("timeseries_kpi", height = "500px"))
103
+ )
104
  ),
105
 
106
  # タブ4: 統計サマリー
107
  tabPanel("統計サマリー",
108
+ fluidRow(
109
+ column(12, h3("基本統計量"))
110
+ ),
111
+ fluidRow(
112
+ column(6,
113
+ wellPanel(
114
+ h4("整備部門"),
115
+ verbatimTextOutput("summary_seisetsubi")
116
+ )
117
+ ),
118
+ column(6,
119
+ wellPanel(
120
+ h4("商品部門"),
121
+ verbatimTextOutput("summary_shohin")
122
+ )
123
+ )
124
+ ),
125
+ fluidRow(
126
+ column(6,
127
+ wellPanel(
128
+ h4("運送部門"),
129
+ verbatimTextOutput("summary_unsou")
130
+ )
131
+ ),
132
+ column(6,
133
+ wellPanel(
134
+ h4("レンタル部門"),
135
+ verbatimTextOutput("summary_rental")
136
+ )
137
+ )
138
+ )
139
  ),
140
 
141
  # タブ5: 詳細な散布図分析
142
  tabPanel("詳細分析",
143
+ fluidRow(
144
+ column(12, h3("主���指標の散布図行列"))
145
+ ),
146
+ fluidRow(
147
+ column(12,
148
+ wellPanel(
149
+ h4("分析対象指標の選択(複数選択可)"),
150
+ checkboxGroupInput("scatter_vars", "変数を選択",
151
+ c("整備_売上" = "x2",
152
+ "整備_粗利" = "x7",
153
+ "整備_営業利益" = "x9",
154
+ "商品_売上" = "x37",
155
+ "商品_粗利" = "x39",
156
+ "商品_営業利益" = "x41",
157
+ "運送_売上" = "x52",
158
+ "運送_粗利" = "x54",
159
+ "運送_営業利益" = "x56",
160
+ "レンタル_売上" = "x76",
161
+ "レンタル_粗利" = "x78",
162
+ "レンタル_営業利益" = "x80"),
163
+ selected = c("x2", "x9", "x37", "x52"))
164
+ )
165
+ )
166
+ ),
167
+ fluidRow(
168
+ column(12, plotOutput("scatter_matrix", height = "800px"))
169
+ )
170
  )
171
  )
172
  )
 
175
 
176
  # データの読み込み
177
  df <- reactive({
178
+ # ファイルパスを設定(Docker対応)
179
  possible_paths <- c(
180
+ "DummyData.csv", # Docker環境
181
+ "SampleData.csv", # ローカル環境
182
+ file.path(getwd(), "DummyData.csv"),
183
+ file.path(getwd(), "SampleData.csv")
184
  )
185
 
186
  for (path in possible_paths) {
 
203
  }
204
 
205
  # ファイルが見つからない場合は警告
206
+ showNotification("DummyData.csv または SampleData.csv が見つかりません", type = "error")
207
  return(NULL)
208
  })
209
 
 
269
 
270
  # ヒートマップを作成
271
  corrplot(corr_matrix, method = "color", type = "full",
272
+ tl.cex = 0.7, tl.col = "black",
273
+ addCoef.col = "white", number.cex = 0.6,
274
+ title = "相関係数ヒートマップ", mar = c(0, 0, 2, 0))
275
  }, error = function(e) {
276
  plot(1, 1, type="n", xlab="", ylab="", main="エラー")
277
  text(1, 1, paste("エラー:", e$message), cex=1.2, col="red")
 
309
  cat("相関係数が強い上位10の組み合わせ:\n\n")
310
  for (i in 1:nrow(corr_pairs)) {
311
  cat(sprintf("%s (%s) と %s (%s): %.3f\n",
312
+ corr_pairs$var1_label[i],
313
+ corr_pairs$var1_code[i],
314
+ corr_pairs$var2_label[i],
315
+ corr_pairs$var2_code[i],
316
+ corr_pairs$correlation[i]))
317
  }
318
  }, error = function(e) {
319
  cat("エラーが発生しました:", e$message)
 
352
  geom_point(size = 3, color = "steelblue", alpha = 0.6) +
353
  geom_smooth(method = "lm", se = TRUE, color = "red", fill = "red", alpha = 0.2) +
354
  labs(title = paste(y_label, "vs", x_label),
355
+ subtitle = sprintf("(%s) vs (%s)", input$y_var, input$x_var),
356
+ x = x_label,
357
+ y = y_label) +
358
  theme_minimal() +
359
  theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
360
 
 
386
  data <- df() %>%
387
  select(年月, x9, x41, x56, x80) %>%
388
  rename("整備営業利益" = x9,
389
+ "商品営業利益" = x41,
390
+ "運送営業利益" = x56,
391
+ "レンタル営業利益" = x80) %>%
392
  gather(key = "部門", value = "営業利益", -年月)
393
 
394
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
 
398
  geom_line(size = 1) +
399
  geom_point(size = 2) +
400
  labs(title = "各部門の営業利益推移",
401
+ x = "年月",
402
+ y = "営業利益(円)",
403
+ color = "部門") +
404
  theme_minimal() +
405
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
406
+ plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
407
 
408
  ggplotly(p)
409
  })
 
414
  data <- df() %>%
415
  select(年月, x2, x37, x52, x76) %>%
416
  rename("整備売上" = x2,
417
+ "商品売上" = x37,
418
+ "運送売上" = x52,
419
+ "レンタル売上" = x76) %>%
420
  gather(key = "部門", value = "売上", -年月)
421
 
422
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
 
426
  geom_line(size = 1) +
427
  geom_point(size = 2) +
428
  labs(title = "各部門の売上高推移",
429
+ x = "年月",
430
+ y = "売上(円)",
431
+ color = "部門") +
432
  theme_minimal() +
433
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
434
+ plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
435
 
436
  ggplotly(p)
437
  })
 
442
  data <- df() %>%
443
  select(年月, x25, x31, x67, x73, x91) %>%
444
  rename("車検件数" = x25,
445
+ "整備・板金数" = x31,
446
+ "自社ドライバー数" = x67,
447
+ "他社ドライバー数" = x73,
448
+ "レンタル台数" = x91) %>%
449
  gather(key = "KPI", value = "件数", -年月)
450
 
451
  p <- ggplot(data, aes(x = factor(年月, levels = unique(年月)),
 
455
  geom_line(size = 1) +
456
  geom_point(size = 2) +
457
  labs(title = "KPI推移",
458
+ x = "年月",
459
+ y = "件数・台数",
460
+ color = "KPI") +
461
  theme_minimal() +
462
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
463
+ plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
464
 
465
  ggplotly(p)
466
  })
 
558
 
559
  # 散布図行列を作成
560
  pairs(data_clean,
561
+ panel = function(x, y, ...) {
562
+ points(x, y, col = "steelblue", cex = 0.7)
563
+ abline(lm(y ~ x), col = "red", lty = 2)
564
+ },
565
+ main = "変数間の散布図行列")
566
  }, error = function(e) {
567
  plot(1, 1, type="n", xlab="", ylab="")
568
  text(1, 1, paste("エラー:", e$message), cex=1.2, col="red")