-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
343 lines (249 loc) · 11 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
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
#######################
library(shinyMobile)
library(shiny)
library(rtweet)
library(tidytext)
library(dplyr)
library(ggplot2)
library(giphyr)
library(RPostgreSQL)
library(DBI)
library(dbplyr)
library(htmlwidgets)
library(DT)
library(plotly)
library(stringr)
#######################
# Token for twitter API -- stored as object named "twitter_token"
source("www/twitter_token.R")
#twitter_token <- create_token(
# app = "shiny.tweets",
# consumer_key = "xxxxxxxxxxxxxxxxxxxxxx",
# consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
# set_renv = TRUE,
# access_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
# access_secret="xxxxxxxxxxxxxxxxxxxxxxxxxx")
######## USER INTERFACE
ui <- f7Page(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "styling.css")),
options=list(dark=TRUE),
f7TabLayout(
navbar=f7Navbar(
title=p("Gif Money",style="font-size:30px"),
leftPanel = TRUE),
panels=tagList(
f7Panel(side="left",theme="dark",effect="reveal")),
f7Tabs(
swipeable = T,animated = F,
id="tabs",
## Log-In Tab
f7Tab(tabName = "Log-In",
icon=f7Icon("signature"),
active=T,
uiOutput("title_giphy"),
f7Accordion(f7AccordionItem(title = "Sign in",f7Password(inputId = "password",label="Enter password here"),
f7Button(inputId = "passaction",label="Press to Log-in",color = "red")))
),
# Transaction Tab
f7Tab(tabName="Purchases",
tags$head(
tags$script(async = NA, src = "https://platform.twitter.com/widgets.js")),
icon=f7Icon("creditcard"),
f7Accordion(
f7AccordionItem(title="Click to Enter Purchase Details",
f7Text(inputId="Purchase",label = "Purchase"),
f7Text(inputId="Price",label="Price"),
f7Text(inputId="Quantity",label="Quantity"),
f7Select(inputId="Category",label="Category",
choices=c("grocery", "dining-out",
"takeout", "rent", "subscriptions",
"other","travel","loans","recreation",
"beer","pet","sales_tax")),
f7Button(color = "red",inputId ="confirmPurchase",
label="Submit to Ledger")
)
),
br(),
uiOutput("giphy_spending"),
f7Popup(id = "PopConfirmPurchase",
f7Text(inputId = "Confirmation",label = "","Confirmed."),
uiOutput("giphy"),
uiOutput("tweet")
)
),
# Ledger Tab
f7Tab(tabName="Ledger",
icon=f7Icon("book"),
active=T,
actionButton(width='100px',inputId="UpdateDB",
label="Update Ledger",icon=icon("save")
),
actionButton(width='100px',inputId="DELETE",
label="Delete Selected?",icon=icon("trash")
),
DT::dataTableOutput("LEDGER"),
tags$style(HTML(".dataTables_wrapper .dataTables_length {
float: right;}
.dataTables_wrapper .dataTables_filter {
float: left;
text-align: left;}"
)
)
),
# Analysis Tab
f7Tab(tabName = "Analysis",
icon=f7Icon("graph_square"),
active=T,
f7Row(
f7Col(f7DatePicker("DateStart","From",value = "2021-01-01")),
f7Col(f7DatePicker("DateEnd","To",value=Sys.Date()+1),gap=FALSE)
),
plotOutput("Categories"),
f7Text(inputId = "scatterfilter",
label = "Filter by"
),
plotlyOutput("scatterall")
),
# Gift Card Tab
f7Tab(tabName="Gift Cards",
icon = f7Icon("gift"),
active=T,
f7SmartSelect("GC_Select",
"Choose gift card to update",
choices="",
selected=""),
f7Text("GC_used",label = "Credit used"),
f7Button("update_GC_balance","Submit",color="red"),
uiOutput("GIFTCARD")
)
) # closes f7Tabs
)) # closes f7Tab Layout and f7Page
########## SERVER SIDE
server <- function(input,output,session){
# set reactive database tables
ledger <- reactiveValues() # all purchases
gc <- reactiveValues() # all giftcard balances
writeTable <- reactive({dbWriteTable(conn=dbConnect(
PostgreSQL(),
host = "thriftysmackers.ckbxdrgolj49.us-east-2.rds.amazonaws.com",
port=5432,
dbname="",
user = "postgres",
password = input$password),
"thriftysmackers", ledger$df, overwrite = TRUE)})
# Establish SQL connection and initializes database tables
observeEvent(input$passaction,{
database <- dbConnect(
PostgreSQL(),
host = "thriftysmackers.ckbxdrgolj49.us-east-2.rds.amazonaws.com",
port=5432,
dbname="",
user = "postgres",
password = input$password
)
ledger$df <- data.frame(tbl(database,"thriftysmackers") %>%
select(Date,Purchase,Price,Category,Quantity)) %>%
mutate(Price=as.numeric(Price))
gc$df <- data.frame(tbl(database,"gift_cards")%>%
select(card_name,balance)) %>%
mutate(balance=as.numeric(balance))
})
# Giphy images
output$title_giphy <- renderUI({tags$img(
src ="https://media0.giphy.com/media/67ThRZlYBvibtdF9JH/giphy.gif",height="280px")})
output$giphy_spending <- renderUI({
tags$img(src=gif_search(sample(c("cheapskate","stingy","frugal","thrifty"),1)) %>%
select("original") %>% .[sample(1:10,1),] %>% as.character())
})
giphy <- reactive({ gif_search(input$Purchase)})
output$giphy <- renderUI({
req(input$Purchase)
tags$img(src = giphy()$original[sample(1:10,1)], width=200, height=200)
})
# Update Ledger upon confirming purchase
observeEvent(input$confirmPurchase,{
append <- isolate(data.frame(Date=as.character(Sys.time()),
Purchase=input$Purchase,
Category=input$Category,
Quantity=input$Quantity,
Price=as.numeric(input$Price)))
ledger$df <- isolate(rbind(ledger$df,append))
writeTable()
})
# Ledger Tab Table
output$LEDGER <- DT::renderDataTable({
req(input$password)
DT::datatable(ledger$df %>% arrange(desc(Date)),
rownames = FALSE,
editable=TRUE,
options=list(
initComplete=JS("function(settings,json){
","$(this.api().table().header()).css({'color':'#fff'});","
}"))) %>%
formatStyle(backgroundColor = "black",
columns=TRUE,
color = "white")})
# saves latest updates to table
observeEvent(input$UpdateDB,{writeTable()})
# Allows for modification of dt table to be saved
observeEvent(input$LEDGER_cell_edit,{
ledger$df <- ledger$df %>% arrange(desc(Date))
ledger$df[input$LEDGER_cell_edit$row,input$LEDGER_cell_edit$col+1] <- isolate(input$LEDGER_cell_edit$value)
})
# Deletes selected rows from dt table
observeEvent(input$DELETE,{
ledger$df <- ledger$df %>% arrange(desc(Date)) %>% .[-(input$LEDGER_rows_selected),]
})
# Bar plot analysis
graphic <- reactive({ledger$df %>% filter(Date > input$DateStart & Date < input$DateEnd)})
output$Categories <- renderPlot({
req(input$password)
ggplot(graphic(),aes(x=Category,y=Price,fill=Category))+
geom_bar(stat="identity",color="black")+
theme(panel.border = element_rect(colour = "black", fill=NA, size=3),
axis.text.x = element_blank())+
xlab("Categories")+
ylab("Total Spent")
})
## Scatter plot analysis
filtered_plot <- reactive({
if(input$scatterfilter!=""){ledger$df %>%
filter(str_detect(string=Purchase,pattern=regex(input$scatterfilter,ignore_case = T)
))
}else{ledger$df}
})
output$scatterall <- renderPlotly({
ggplotly(ggplot(filtered_plot(),x=Date,y=Price)+
geom_point(aes(x=as.Date(Date),y=Price,color=Category))+
theme_bw())
})
# Pop up confirming new purchase
observeEvent(input$confirmPurchase,{
updateF7Popup(id="PopConfirmPurchase")})
# Tweet accomanying the pop-up confirmation
output$tweet <- renderUI({
req(input$Purchase)
tagList(
# Twitter widget
tags$blockquote(class = "twitter-tweet",
tags$a(href = rtweet::search_tweets(input$Purchase,type = "popular") %>%
select(status_url) %>% .[sample(c(1:10),size = 1),1] %>%
as.character())),
tags$script('twttr.widgets.load(document.getElementById("tweet"));')
)
})
# Password-activated gift card table
output$GIFTCARD <- renderUI({req(input$password)
f7Table(card = T,gc$df,colnames = c("Name of Card","Balance"))})
# Password-activated selections populate
observeEvent(input$passaction,{
updateF7SmartSelect(inputId ="GC_Select",openIn="sheet",
choices=as.character(gc$df[,1]),selected=gc$df[1,1])})
# Subtract balance from selected gift card
observeEvent(input$update_GC_balance,{
gc$df[gc$df["card_name"]==input$GC_Select,2] <- gc$df %>%
filter(card_name==input$GC_Select) %>% .[1,2] - as.numeric(input$GC_used)
})
} # closes server
shiny::shinyApp(ui,server)