-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOil&GasDashboard.R
executable file
·489 lines (447 loc) · 16.8 KB
/
Oil&GasDashboard.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# This is a Shiny web application. You can run the application by clicking the 'Run App' button above.
# Install packages using the below commands:
# install.packages("shiny")
# install.packages("plotly")
# install.packages("shinydashboard")
# install.packages("tidyverse")
# install.packages("DT")
# install.packages("shinyjs")
# Required libraries:
library(shiny)
library(plotly)
library(shinydashboard)
library(DT)
library(tidyverse)
library(shinyjs)
# Dashboard UI items:
header <- dashboardHeader(title = "Gas & Oil Profiles")
sidebar <- dashboardSidebar(
sidebarMenu(
img(src = "logo.png", height = 130, width = 230),
selectInput("GasorOilProfile","Profile:",c("Gas Profile", "Oil Profile"), selected = "Gas Profile"),
hr(),
# Selector for file upload (import data in csv format)
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
hr(),
"Display Squeezed CRX plot:",
textInput("crx_value", label = h4("CRX limit:"), value = 600),
actionButton("squeezedplot", "Squeezed Profile"),
tags$style(type="text/css", "#downloadSqueezedCsvButton {background-color:white;color: black;font-family: Courier New}"),
downloadButton("downloadSqueezedCsvButton", 'Download Squeezed CSV', class = "button1"),
actionButton("do", 'save', icon = icon('play-circle')),
hr(),
selectInput("DataPlot","Plot Type:",c("MainPlot", "SingleFieldPlot"), selected = "MainPlot"),
#These column selectors are dynamically created when the file is loaded
uiOutput("fromCol"),
uiOutput("toCol"),
uiOutput("Fieldflag"),
#The conditional panel is triggered by the preceeding checkbox
conditionalPanel(
condition="input.Fieldflag==true",
uiOutput("FieldCol")
),
hr(),
"Delete a trace from the plot:",
uiOutput("DeleteFieldCol"),
actionButton("deleteTrace", "Delete Trace")
)
)
body <- dashboardBody(
tags$head(tags$style(".sidebar-menu li { margin-bottom: 10px; }")),
fluidPage(
# Application title
titlePanel("Oil and Gas Profile Dashboard"),
# tabs UI
tabsetPanel(id="tabs",
tabPanel("Landing Dashboard", value="1", br(), br(), DTOutput(outputId = "table"), br(), br(), textOutput("text0"), br(), plotlyOutput("gasPlotStatic", height = 300)),
#tabPanel("Gas Profile", value="2", br(), textOutput("text0"), br(), plotlyOutput("gasPlotStatic", height = 300)),
tabPanel("Squeezed Profile", value="2", br(), textOutput("text1"), br(), plotlyOutput("gasPlotSqueezed", height = 300))
#tabPanel("Oil Profile", value="4", plotlyOutput("OilPlot"))
) , style='width: 1300px; height: 1250px'
)
)
# Define UI for application dashboard:
ui <- dashboardPage(
header,
sidebar,
body,
useShinyjs() # Set up shinyjs
)
# Define server logic required to draw a plot
server <- function(input, output, session){
# Side Panel Functionalities:
# Data for normal table on landing page:
filedata1 <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
# Preparing a new dataframe
df <- read.csv(infile$datapath);
click("do")
return(df)
})
# Data for squeezed CRX plot:
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
# Preparing a new dataframe
df <- read.csv(infile$datapath);
df1 <- df;
df <- df %>% select(-Fields) %>% select(-Rank)
# Logic for Squeezing below CRX value:
# first finding the sum across rows:
total_col = apply(df[,-1], 1, sum);
# fetch the order:
colOrderFinal <- NA
colOrder <- df1[!(is.na(df1$Rank) | df1$Rank==""),ncol(df1)];
colNames1 <- colnames(df1 %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
# Arrange ColNames in order:
count = 1
for (i in colOrder) {
colOrderFinal[i] <- colNames1[count];
count = count + 1;
}
count_reset <- count;
col_num <- NA
# recursive function to check iteratively if all values are below CRX value:
squeezed_df <- function(df,total_col, count, j){
validate(need(!is.na(as.numeric(input$crx_value)),"Please enter proper CRX value!"));
crx <- input$crx_value;
for (i in total_col) {
cnt <- count;
if( j <= 4){
crx <- '300';
#browser();
}
else{
crx <- input$crx_value;
#browser();
}
total_col_for_yr_j <- i;
if(i >= as.numeric(crx)){
extra_val = total_col_for_yr_j - as.numeric(crx);
while(total_col_for_yr_j > as.numeric(crx)){
# deduction from lowest priority first
col_num <- which(colNames1 == colOrderFinal[cnt-1])
#print(col_num)
count <- count - 1;
cnt <- cnt-1;
# total_col_for_yr_j = 700
# CRX 600
# Case 1: df[j,col_num+1] = 150, extra_val 100
# Case 2: df[j,col_num+1] = 40, extra_val 100, remain_val = 60
# Case 3: df[j,col_num+1] = 100, extra_val 100
# Case 1:
validate(need(!is.na(df[j,col_num+1] - extra_val),"Please enter proper CRX value!"));
if(df[j,col_num+1] - extra_val > 0){
df[j, col_num+1] <- df[j,col_num+1] - extra_val;
total_col_for_yr_j <- apply(df[j,-1], 1, sum);
if(cnt == 1){
cnt <- count_reset;
count <- count_reset;
}
next;
}
# Case 2:
else if(df[j,col_num+1] - extra_val < 0){
extra_val = extra_val - df[j,col_num+1];
df[j,col_num+1] = 0;
}
else{
df[j, col_num+1] <- df[j,col_num+1] - extra_val;
}
total_col_for_yr_j <- apply(df[j,-1], 1, sum);
if(cnt == 1){
cnt <- count_reset;
count <- count_reset;
}
}
}
else {
cnt <- count_reset;
count <- count_reset;
}
cnt <- count_reset;
count <- count_reset;
j <- j + 1;
}
# Again find the total after x% deduction
total_col <- apply(df[,-1], 1, sum);
click("do");
if(length(which(total_col[1:4] > 300)) == 0 & length(which(total_col[5:count_reset-1] > as.numeric(input$crx_value))) == 0) {
return(df)
} else {
return(squeezed_df(df,total_col, count, j))
}
}
# Calling the squeezing function:
j <- 1;
df <- squeezed_df(df,total_col, count, j);
return(df)
})
####### Main Gas Plot functionalities: #####
# Realtime plot for gas profile as per user's selection:
data <- reactive({
x <- filedata1() %>%
filter(between(as.integer(Year), as.integer(input$from), as.integer(input$to)));
x <- x[, c("Year", input$field)]
return(x)
})
output$text0 <- renderText({
"Main CRX Plot"
})
###### Functions for plots: ##########
#Realtime gas plot:
rtgasplot <- function(df){
#browser();
p <- plot_ly(df, x = ~Year, y = ~get(input$field), name = as.character(input$field), type = 'scatter', mode = 'markers+lines', stackgroup = 'one', fillcolor = '#4C74C9', showlegend = TRUE) %>%
layout(shapes=list(
list(type='line', x0= min(df$Year), x1= 2022, y0=300, y1=300, line=list(dash='dot', width=2)),
list(type='line', x0= 2022, x1= 2023, y0=300, y1=input$crx_value, line=list(dash='dot', width=2)),
list(type='line', x0= 2023, x1= max(df$Year), y0=input$crx_value, y1=input$crx_value, line=list(dash='dot', width=2))
),
title = 'CRX Supply',
xaxis = list(title = "Year", showgrid = TRUE, showline = TRUE),
yaxis = list(title = "Fields", showgrid = TRUE, showline = TRUE))
return(p)
}
#Main Gas plot:
maingasplot <- function(df, trace1, trace1_name, colNames){
p <- plot_ly(df, x = ~Year, y = trace1, name = trace1_name, type = 'scatter', mode = 'markers+lines', stackgroup = 'one', fillcolor = '#00fbff')
color_list = c( '#eb8705' , '#fcc203', '#fc0314', '#eb34e2', '#522c18', '#022beb', '#fcba03', '#50CB86', '#4f23a1', '#42f587', '#d12c7c','#3434eb', '#e60206', '#edb707');
i = 1;
for(trace in colNames){
p <- p %>% add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace, fillcolor = color_list[i])
i = i+1;
}
p <- p %>% layout(shapes=list(
list(type='line', x0= min(df$Year), x1= 2022, y0=300, y1=300, line=list(dash='dot', width=2)),
list(type='line', x0= 2022, x1= 2023, y0=300, y1=input$crx_value, line=list(dash='dot', width=2)),
list(type='line', x0= 2023, x1= max(df$Year), y0=input$crx_value, y1=input$crx_value, line=list(dash='dot', width=2))
),
title = 'CRX Supply',
xaxis = list(title = "Year", showgrid = TRUE, showline = TRUE),
yaxis = list(title = "Fields", showgrid = TRUE, showline = TRUE
))
return(p)
}
# Output Static Plot for Gas or Oil profile (Both MainPlot and user selections based RealTime Plot):
output$gasPlotStatic <- renderPlotly({
if(input$DataPlot == "SingleFieldPlot"){
# Single Field Plot
df <- data()
validate(
need( nrow(df) > 0, "Data insufficient for plot, please select the year range and a field from the sidepanel")
)
p <- rtgasplot(df);
}
else{
# Main Plot (This plot can be changed by user editing in the landing tab table and clicking on save button.)
df <- filedata1();
validate(
need( nrow(df) > 0, "Please upload the CSV to get the plot")
);
# fetch the order:
colNames <- NA
colOrder <- df[!(is.na(df$Rank) | df$Rank==""),ncol(df)];
colNames1 <- colnames(df %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
# Arrange ColNames in order:
count = 1
for (i in colOrder) {
colNames[i] <- colNames1[count];
count = count + 1;
}
trace1_name <- colNames[1]
# we place first trace in plotly first line
colNames <- colNames[-1];
trace1 = as.formula(paste0("~`", trace1_name, "`"))
p <- maingasplot(df, trace1, trace1_name, colNames);
}
})
######## Squeezed Plot functionalities: #########
output$text1 <- renderText({
"Squeezed CRX Plot"
})
observeEvent(input$squeezedplot, {
# Squeezed Static Plot:
output$gasPlotSqueezed <- renderPlotly({
if(input$DataPlot == "SingleFieldPlot"){
# Real time squeezed Plot for single user selected field:
df <- data()
validate(
need( nrow(df) > 0, "Data insufficient for plot, please select the year range and a field from the sidepanel")
)
p <- rtgasplot(df);
}
else{
# squeezed Plot for all fields:
df <- filedata();
df1 <- filedata1();
validate(
need( nrow(df) > 0, "Please upload the CSV and click 'Squeezed Gas Profile' button to get the plot")
);
# fetch the order:
colNames <- NA
colOrder <- df1[!(is.na(df1$Rank) | df1$Rank==""),ncol(df1)];
colNames1 <- colnames(df %>% select(-Year))
# Arrange ColNames in order:
count = 1
for (i in colOrder) {
colNames[i] <- colNames1[count];
count = count + 1;
}
trace1_name <- colNames[1]
# we place first trace in plotly first line
colNames <- colNames[-1];
trace1 = as.formula(paste0("~`", trace1_name, "`"))
p <- maingasplot(df, trace1, trace1_name, colNames);
}
})
})
### Download Squeezed Data ###
output$downloadSqueezedCsvButton <- downloadHandler(
filename = function() {
paste(Sys.time(), '_data_squeezed.csv', sep='')
},
content = function(file) {
write.csv(filedata(), file, row.names = FALSE)
}
)
######### Landing Tab functionalities: #############################
output$table <- renderDataTable(server = FALSE, {
df <- filedata1();
validate(
need( nrow(df) > 0, "Please upload the CSV to view the Data Table")
);
DT::datatable(df, editable = "all", rownames = FALSE, filter = "top", # location of column filters,
extensions = "Buttons", options = list(fixedColumns = TRUE, autoWidth = TRUE, ordering = FALSE,
scrollX = TRUE, # allow user to scroll wide tables horizontally
dom = 'Blfrtip', buttons = c('copy', 'csv', 'excel', 'pdf'), pageLength = 10,lengthMenu = list(c(10,30, 50, 100, -1), list('10', '30', '50','100', 'All')), paging = T)
)
})
# hide save button:
observe({ toggle(id="do", condition=!is.null(input$location))})
###Tracking Changes###
rvs <- reactiveValues(
data = NULL #dynamic data object
)
observe({
rvs$data <- filedata1()
})
#proxy = dataTableProxy('table')
observe({
tryCatch({
replaceData(rvs$data, rownames = FALSE, resetPaging = FALSE)
}, error = function(e) {
}
)
})
observeEvent(input$table_cell_edit, {
tryCatch({
rvs$data <<- editData(rvs$data, input$table_cell_edit, rownames = FALSE)
click("do")
}, error = function(e) {
print("Please provide all different ranks.");
}
)
})
####### DeleteTrace functionality: #######
output$DeleteFieldCol <- renderUI({
df <-filedata1()
if (is.null(df)) return(NULL)
# Let's only show field names
items= colnames(df %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
selectInput("delfield", "Field to Delete:",choices=items)
})
# Track delete trace button click:
observeEvent(
input$deleteTrace,{
output$gasPlotStatic <- renderPlotly({
df <- filedata1()
# fetch the order:
colNames <- NA
colOrder <- df[!(is.na(df$Rank) | df$Rank==""),ncol(df)];
colNames1 <- colnames(df %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
# Arrange ColNames in order:
count = 1
for (i in colOrder) {
colNames[i] <- colNames1[count];
count = count + 1;
}
colNames <- colNames[-which(colNames == input$delfield)]
trace1_name <- colNames[1]
colNames <- colNames[-1]
trace1 = as.formula(paste0("~`", trace1_name, "`"))
p <- maingasplot(df, trace1, trace1_name, colNames);
})
})
#### RealTime Plot functionality options ########
# The following set of functions populate the column selectors
output$fromCol <- renderUI({
df <-filedata1()
if (is.null(df)) return(NULL)
items= df[,1]
selectInput("from", "From Year:",choices= items)
})
output$toCol <- renderUI({
df <-filedata1()
if (is.null(df)) return(NULL)
items= df[,1]
selectInput("to", "To Year:",items)
})
#The checkbox selector is used to determine whether we want an optional column Field
output$Fieldflag <- renderUI({
checkboxInput("Fieldflag", "Do you want to select specific field?", FALSE)
})
#If we do want the optional column, this is where it gets created
output$FieldCol <- renderUI({
df <-filedata1()
if (is.null(df)) return(NULL)
#Let's only show field names
items= colnames(df %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
selectInput("field", "Field:",choices=items)
})
#### Save editable table values and populate plots: #########
observeEvent(
input$do,{
reacdata <- rvs$data
# Output Static Plot for Gas profile (Both MainPLot and user selections based RealTime Plot):
output$gasPlotStatic <- renderPlotly({
if(input$DataPlot == "SingleFieldPlot"){
# Real time plot
df <- data()
validate(
need( nrow(df) > 0, "Data insufficient for plot, please select the year range and a field from the sidepanel")
)
p <- rtgasplot(df);
}
else{
# Main Plot (This plot can be changed by user editing in the landing tab table and clicking on save button.)
df <- reacdata
# fetch the order:
colNames <- NA
colOrder <- df[!(is.na(df$Rank) | df$Rank==""),ncol(df)];
colNames1 <- colnames(df %>% select(-Year) %>% select(-Fields) %>% select(-Rank))
# Arrange ColNames in order:
count = 1
for (i in colOrder) {
colNames[i] <- colNames1[count];
count = count + 1;
}
trace1_name <- colNames[1]
# we place first trace in plotly first line
colNames <- colNames[-1];
trace1 = as.formula(paste0("~`", trace1_name, "`"))
p <- maingasplot(df, trace1, trace1_name, colNames);
}
})
})
}
# Run the application:
shinyApp(ui = ui, server = server)