sugitora commited on
Commit
8361b0a
·
verified ·
1 Parent(s): 71e62b6

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +148 -48
app.R CHANGED
@@ -1,58 +1,158 @@
1
  library(shiny)
2
- library(bslib)
3
- library(dplyr)
4
  library(ggplot2)
 
 
 
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
 
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
 
 
 
 
 
 
 
 
18
  ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
 
 
 
 
 
 
25
  )
26
 
27
- server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  })
32
-
33
- output$scatter <- renderPlot(
34
- {
35
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
36
- theme_light() +
37
- list(
38
- theme(legend.position = "bottom"),
39
- if (input$by_species) aes(color = Species),
40
- geom_point(),
41
- if (input$smooth) geom_smooth()
42
- )
43
-
44
- if (input$show_margins) {
45
- margin_type <- if (input$by_species) "density" else "histogram"
46
- p <- p |> ggExtra::ggMarginal(
47
- type = margin_type, margins = "both",
48
- size = 8, groupColour = input$by_species, groupFill = input$by_species
49
- )
50
- }
51
-
52
- p
53
- },
54
- res = 100
55
- )
56
  }
57
 
58
- shinyApp(ui, server)
 
1
  library(shiny)
 
 
2
  library(ggplot2)
3
+ library(dplyr)
4
+ library(tidyr)
5
+ library(showtext)
6
 
7
+ # 日本語フォント設定
8
+ font_add("Hiragino", "/System/Library/Fonts/ヒラギノ角ゴシック W3.ttc")
9
+ showtext_auto()
10
 
11
+ ui <- fluidPage(
12
+ titlePanel("クライアント型電子計算機の分析可視化"),
13
+ sidebarLayout(
14
+ sidebarPanel(
15
+ sliderInput("alpha", "残存関数のパラメータ alpha:", min = 1, max = 4, value = 2.58, step = 0.01),
16
+ sliderInput("lambda", "残存関数のパラメータ lambda:", min = 5, max = 15, value = 8.0, step = 0.1),
17
+ # TEC分布インタラクティブ追加
18
+ conditionalPanel(
19
+ condition = "$('li.active a').text() === 'TEC分布(ノートPC)'",
20
+ sliderInput("screen_min", "画面サイズ(最小)", min = 8, max = 14.9, value = 8, step = 0.1),
21
+ sliderInput("screen_max", "画面サイズ(最大)", min = 15.1, max = 18, value = 18, step = 0.1),
22
+ numericInput("mean_tec", "TEC平均値", value = 20, min = 0),
23
+ numericInput("sd_tec", "TEC標準偏差", value = 5, min = 0.1),
24
+ checkboxInput("show_medians", "中央値ラベルを表示", value = TRUE),
25
+ checkboxInput("show_vline", "15インチ区切り線を表示", value = TRUE)
26
+ )
27
  ),
28
+ mainPanel(
29
+ tabsetPanel(
30
+ tabPanel("残存関数", plotOutput("residualPlot")),
31
+ tabPanel("ストック推計", plotOutput("stockPlot")),
32
+ tabPanel("TEC分布(ノートPC)", plotOutput("tecNotePlot")),
33
+ tabPanel("TEC分布(Pスコア)", plotOutput("tecPscorePlot")),
34
+ tabPanel("TEC分布(デスクトップ)", plotOutput("tecDesktopPlot")),
35
+ tabPanel("TEC分布(分離型PC)", plotOutput("tecSplitPlot")),
36
+ tabPanel("エネルギー消費量", plotOutput("energyPlot"))
37
+ )
38
+ )
39
+ )
40
  )
41
 
42
+ server <- function(input, output) {
43
+
44
+ output$residualPlot <- renderPlot({
45
+ n <- 0:30
46
+ residual_rate <- exp(- (n / input$lambda)^input$alpha)
47
+ df <- data.frame(Year = n, ResidualRate = residual_rate)
48
+
49
+ ggplot(df, aes(x = Year, y = ResidualRate)) +
50
+ geom_line(color = "darkorange", size = 1.2) +
51
+ labs(title = "残存関数", subtitle = paste("alpha=", input$alpha, ", lambda=", input$lambda),
52
+ x = "年数 (n)", y = "残存率") +
53
+ theme_minimal() + ylim(0, 1.2)
54
+ })
55
+
56
+ output$stockPlot <- renderPlot({
57
+ df <- data.frame(
58
+ 年度 = 2000:2030,
59
+ 実績 = c(55000000, 61000000, 67000000, 73000000, 80000000, 86000000, 91000000, 96000000, 101000000,
60
+ 106000000, 111000000, 115000000, 118000000, 120000000, 121000000, 120000000, 116000000, 113000000,
61
+ 112000000, 111000000, 108000000, 107000000, 106000000, rep(NA, 8)),
62
+ 推計 = c(rep(NA, 23), 104000000, 102000000, 101000000, 99000000, 98000000, 97000000, 96000000, 95000000)
63
+ )
64
+ df_long <- df %>% mutate(年度 = as.character(年度)) %>%
65
+ pivot_longer(cols = c(実績, 推計), names_to = "区分", values_to = "ストック数") %>%
66
+ filter(!is.na(ストック数))
67
+ ggplot(df_long, aes(x = 年度, y = ストック数, fill = 区分)) +
68
+ geom_col(position = "dodge") +
69
+ scale_fill_manual(values = c("実績" = "steelblue", "推計" = "darkorange")) +
70
+ theme_minimal() + theme(axis.text.x = element_text(angle = 90)) +
71
+ labs(title = "ストック量の推計", x = "年度", y = "ストック数(台)")
72
+ })
73
+
74
+ output$tecNotePlot <- renderPlot({
75
+ set.seed(123)
76
+ screen_size <- c(runif(25, input$screen_min, min(14.9, input$screen_max)),
77
+ runif(15, max(15.1, input$screen_min), input$screen_max))
78
+ tec_value <- c(rnorm(25, input$mean_tec - 5, input$sd_tec),
79
+ rnorm(15, input$mean_tec + 5, input$sd_tec))
80
+ df1 <- data.frame(ScreenSize = screen_size, TEC = tec_value)
81
+ medians1 <- data.frame(
82
+ ScreenSize = c(mean(c(input$screen_min, 14.9)), mean(c(15.1, input$screen_max))),
83
+ TEC = c(input$mean_tec - 5, input$mean_tec + 5),
84
+ Label = sprintf("%.2f", c(input$mean_tec - 5, input$mean_tec + 5))
85
+ )
86
+
87
+ p <- ggplot(df1, aes(x = ScreenSize, y = TEC)) +
88
+ geom_point(color = "blue") +
89
+ labs(title = "TEC分布:ノートPC(区分J・K)", x = "画面サイズ", y = "TEC[kWh]") +
90
+ theme_minimal(base_family = "Hiragino")
91
+
92
+ if (input$show_vline) {
93
+ p <- p + geom_vline(xintercept = 15, linetype = "dashed")
94
+ }
95
+ if (input$show_medians) {
96
+ p <- p + geom_text(data = medians1, aes(label = Label), vjust = -1, color = "red", size = 4)
97
+ }
98
+ p
99
+ })
100
+
101
+ output$tecPscorePlot <- renderPlot({
102
+ set.seed(124)
103
+ df2 <- data.frame(P_score = runif(30, 8, 13), TEC = rnorm(30, 20, 5))
104
+ ggplot(df2, aes(x = P_score, y = TEC)) + geom_point(color = "darkgreen") +
105
+ geom_segment(aes(x = 10.5, xend = 12.5, y = 11.34, yend = 11.34), color = "red") +
106
+ annotate("text", x = 12.7, y = 11.34, label = "11.34") +
107
+ labs(title = "TEC分布:Pスコア8以上(区分L)", x = "Pスコア", y = "TEC[kWh]") +
108
+ theme_minimal(base_family = "Hiragino")
109
+ })
110
+
111
+ output$tecDesktopPlot <- renderPlot({
112
+ set.seed(125)
113
+ df3 <- data.frame(P_score = runif(40, 2, 15), TEC = runif(40, 30, 180))
114
+ ggplot(df3, aes(x = P_score, y = TEC)) + geom_point() +
115
+ geom_vline(xintercept = 8, linetype = "dashed") +
116
+ annotate("text", x = 5, y = 180, label = "区分M") +
117
+ annotate("text", x = 12, y = 180, label = "区分N") +
118
+ geom_segment(aes(x = 3, xend = 7, y = 39.87, yend = 39.87), color = "red") +
119
+ geom_segment(aes(x = 10, xend = 14, y = 53.32, yend = 53.32), color = "red") +
120
+ annotate("text", x = 7.1, y = 39.87, label = "39.87") +
121
+ annotate("text", x = 14.1, y = 53.32, label = "53.32") +
122
+ labs(title = "TEC分布:一体型デスクトップ(M/N)", x = "Pスコア", y = "TEC[kWh]") +
123
+ theme_minimal(base_family = "Hiragino")
124
+ })
125
+
126
+ output$tecSplitPlot <- renderPlot({
127
+ set.seed(126)
128
+ df4 <- data.frame(Volume = runif(40, 0, 20), TEC = runif(40, 20, 140))
129
+ ggplot(df4, aes(x = Volume, y = TEC)) + geom_point() +
130
+ geom_vline(xintercept = 10, linetype = "dashed") +
131
+ annotate("text", x = 4, y = 140, label = "区分O") +
132
+ annotate("text", x = 14, y = 140, label = "区分P") +
133
+ geom_segment(aes(x = 3, xend = 8, y = 29.55, yend = 29.55), color = "red") +
134
+ geom_segment(aes(x = 11, xend = 15, y = 31.33, yend = 31.33), color = "red") +
135
+ annotate("text", x = 8.1, y = 29.55, label = "29.55") +
136
+ annotate("text", x = 15.1, y = 31.33, label = "31.33") +
137
+ labs(title = "TEC分布:分離型PC(O/P)", x = "容量[L]", y = "TEC[kWh]") +
138
+ theme_minimal(base_family = "Hiragino")
139
+ })
140
+
141
+ output$energyPlot <- renderPlot({
142
+ data <- tibble(
143
+ 年度 = 2006:2030,
144
+ 実績 = c(10000, 10300, 10400, 10200, 10100, 10000, 9800, 9400, 9200, 8900,
145
+ 8500, 8100, 7700, 7200, 6800, 6300, 6000, rep(NA, 8)),
146
+ 推計 = c(rep(NA, 17), 5800, 5600, 5400, 5200, 5000, 4800, 4700, 4600)
147
+ )
148
+ data_long <- data %>% pivot_longer(cols = c("実績", "推計"), names_to = "区分", values_to = "消費量")
149
+ ggplot(data_long, aes(x = 年度, y = 消費量, fill = 区分)) +
150
+ geom_col(position = "dodge") +
151
+ scale_fill_manual(values = c("実績" = "steelblue", "推計" = "darkorange")) +
152
+ scale_y_continuous(labels = scales::comma) +
153
+ labs(title = "エネルギー消費量の推移", x = "年度", y = "GWh/年") +
154
+ theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
155
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  }
157
 
158
+ shinyApp(ui = ui, server = server)