-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
159 lines (131 loc) · 4.17 KB
/
app.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
library(shiny)
library(DT)
library(tidyverse)
library(odbc)
library(ggthemes)
library(lubridate)
library(zoo)
library(pool)
#library(dbplyr)
library(glue)
library(config)
library(shinycssloaders)
library(httr2)
#library(bs4Dash)
source('utils.R')
conn_args <- config::get("dataconnection")
con <- dbPool(RMariaDB::MariaDB(),
host = conn_args$host,
port = conn_args$port,
username = conn_args$username,
password = conn_args$password,
db = conn_args$db,
minSize = conn_args$minSize,
idleTimeout = conn_args$idleTimeout
)
AOdates <- #dbGetQuery
dbGetQuery(con, 'SELECT distinct AOdate
FROM scratchoff.game_info order by aodate desc
limit 25')
# the dashboard -----------------------------------------------------------
shinyApp(
ui = fluidPage(
tabsetPanel(
tabPanel("Games Overview",
fluidRow(
mainPanel(
withSpinner(DTOutput('game_overview_table')))
)
),
tabPanel("Game Information",
fluidRow(
column(3,
selectInput('aodates',
'Date: ',
AOdates),
# selectInput(
# 'game_names',
# 'Game: ',
# avaliable_games)
uiOutput('avaliable_games'),
numericInput('lag', 'Time span (months): ', value = 3)
),
mainPanel(
withSpinner(plotOutput('prizes_left_graph')),
withSpinner(plotOutput('ev_change_graph')),
textOutput('test1')
)
)
)
)
),
server = function(input, output) {
aodate <- reactive({input$aodates})
selected_game_number <- reactive({ input$game_names %>% str_extract('.+(?= -)') })
# selected_game_name <- reactive({ input$game_names %>% str_extract('(?<=- ).+') })
selected_lag <- reactive({input$lag})
game_info <- reactive({
dbGetQuery(
con,
paste0(
"SELECT distinct
a.game_number, b.game_name, a.expected_value_current
FROM
scratchoff.game_summaries a
LEFT JOIN
scratchoff.game_info b ON a.game_number = b.game_number
WHERE
cast(a.AOdate as date) = CAST('",
aodate(),
"' AS DATE)
ORDER BY expected_value_current DESC"
)
)
})
prize_data <- reactive({ #dbGetQuery
dbGetQuery(con,
paste0("SELECT * FROM scratchoff.game_prizes where game_number = '",
selected_game_number(),
"' and aodate = '", aodate(), "'"))
})
# outputs -----------------------------------------------------------------
output$game_info_table <-
renderDT(
DT::datatable({
game_info()
})
)
output$test1 <- renderText({paste0('aodate = ', aodate(), '\nsgnumber = ', selected_game_number())})
output$avaliable_games <- renderUI({
selectInput(
'game_names',
'Game: ',
paste0(game_info()$game_number, ' - ', game_info()$game_name))
})
output$prizes_left_graph <- renderPlot({
plot_prizes_left(prize_data())
})
output$ev_change_graph <- renderPlot({
asofdate <- aodate()
game_number <- selected_game_number()[1]
lag <- selected_lag()
# browser
tryCatch({
plot_ev_change(asofdate = asofdate,
game_number = game_number,
con,
lag = lag)
}, error = function(e) {""} # Leave this line as-is.
)
})
output$game_overview_table = renderDT(
DT::datatable(
game_overview_table(aodate = aodate(), con = con, positive_ev = F),
escape = FALSE,
options = list(lengthChange = FALSE,
paging = FALSE)
) %>% formatRound(columns=c('expected_value_current'), digits=2) %>%
formatPercentage(columns =c('percent_ev'), digits = 0)
)
}
)