-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiny_try.R
125 lines (94 loc) · 3.71 KB
/
shiny_try.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
library(shiny)
library(shinydashboard)
my_db <- tryCatch(dbr::db_query(paste('select *
from "DATA_SCIENCE"."CONCOURSE"."CONCOURSE_SELL_SIDE_HAZEN"
where "DATE" >to_date(\'', Sys.Date()-30,'\')',
sep = ""), db = 'snowflake'), error = function(e)e)
names(my_db) <- tolower(names(my_db))
# Pull old table
df_sf<- tbl(my_db, "CONCOURSE_SELL_SIDE_HAZEN")%>%
data.frame()%>%
mutate(date = as.Date(date), timestamp=as.POSIXct(timestamp))%>%
arrange(desc(date))%>%
mutate(resid_f10 = avg - rps)%>%
mutate(resid_me = hpred - rps)%>%
filter(date > Sys.Date()-20)
df_round <- df_sf%>%
mutate(rps = round(rps,2), avg = round(avg,2), hpred = round(hpred,2))%>%
mutate(resid_f10 = rps - avg)%>%
mutate(resid_me = rps - hpred)
df_amt2 <- df_round%>%
group_by(landingContentGroup2, country, deviceCategory, operatingSystem)%>%
summarise(ses = sum(ses, na.rm = TRUE), count = n(), rev = sum(rev, na.rm = TRUE))%>%
arrange(desc(ses),desc(count))
mase1 <- function(df_tester){
df_mase <- df_tester%>%
filter(!is.na(resid_f10)&!is.na(resid_me))
return(sum(abs(df_mase$resid_me))/sum(abs(df_mase$resid_f10)))
}
mase2 <- function(df_tester){
df_mase <- df_tester%>%
filter(!is.na(resid_f10)&!is.na(resid_me))%>%
mutate(resid_f10 = resid_f10*ses, resid_me = resid_me*ses)
num <- sum(abs(df_mase$resid_me))/sum(df_mase$ses)
den <- sum(abs(df_mase$resid_f10))/sum(df_mase$ses)
return(num/den)
}
# num<-1
#
# name1 <- as.character(df_amt2[num,])
#
# df_tester<-df_round%>%
# filter(as.character(landingContentGroup2) == name1[1] & as.character(country) == name1[2] &
# as.character(deviceCategory) == name1[3], as.character(operatingSystem) == name1[4])%>%
# arrange(timestamp)
ui <- dashboardPage(
dashboardHeader(),
## Sidebar content
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("DATA", tabName = "rawData", icon = icon("th"))
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
fluidRow(
box(plotOutput("plot1", height = 450, width = "100%"), width = 10 ),
box(
title = "Controls",
numericInput("campNumber", "Campaign number:", 1, 1, nrow(df_amt2), 1)
)
)
),
# Second tab content
tabItem(tabName = "rawData",
h2("Raw Data")
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
name1 <- as.character(df_amt2[input$campNumber,])
df_tester<-df_round%>%
filter(as.character(landingContentGroup2) == name1[1] & as.character(country) == name1[2] &
as.character(deviceCategory) == name1[3], as.character(operatingSystem) == name1[4])%>%
arrange(timestamp)
ggplot(df_tester, aes(x = timestamp))+
geom_line(aes(y = rps, color = "rps"), size = 1.1)+
geom_point(aes(y = rps, color = "rps"), size = .6)+
geom_line(aes(y = avg, color = "3day"))+
geom_line(aes(y = hpred, color = "pred"))+
scale_x_datetime(minor_breaks = "1 day")+
scale_colour_manual("",
breaks = c("rps", "3day", "pred"),
values = c("blue", "red", "black")) +
ggtitle(paste("campaign: ", substring(name1[1], first = 1,last = 50),
"\n",name1[2],name1[3],name1[4], "\n", "mase comparison: ", round(mase2(df_tester),4), sep = " " ))
})
}
shinyApp(ui, server)