I have a Shiny app twhere the user selects a date and metric, and shows a line graph.
But if I select a metric, it will pop0ulate that values and then quickly rever to the original plot.
After some research, I found info on these update event functions but have had no luck implementing it.
Any ideas what is going wrong?
server <- function(input, output, session) {
#===============================================================================
message("app: Create Query n")
AbrQuery <- "
SELECT
*
FROM SQ_ML_MODEL.dbo.DataSci_ABR_Preds_Vs_Actuals_updated
"
#===============================================================================
message("app: Set Secret Values n")
SecretValuesLst <- GetSecret(Debug = TRUE)
print(SecretValuesLst)
print(SecretValuesLst$SERVER)
#===============================================================================
ConnectionString <- CreateConnectionString(SecretValuesLst, Debug = TRUE)
print(ConnectionString)
#===============================================================================
message("app: Connect to Database n")
# DbCon <- RODBC::odbcDriverConnect(connectionString)
#
# print(DbCon)
Conn <- GetSQLServerConnection(ConnectionString, Debug = TRUE)
print(Conn)
#===============================================================================
message("app: Pull Data From Database n")
DataOutput <- RODBC::sqlQuery(Conn[["DbCon"]], query = AbrQuery)
setDT(DataOutput)
DataOutput
#===============================================================================
###
message("app: Prepare Data n")
# RawAbrData
# str(RawAbrData)
VizAbrData <- melt.data.table(DataOutput[, .(MinuteOfDay,
AgentCategory,
Queue,
Login,
Tier,
Abr_Actuals,
Abr_Preds,
Abr_Diff)],
id.vars = c("MinuteOfDay",
"AgentCategory"))
VizAbrData
# VizAbrData[, .N, by = variable]
# VizAbrData[variable == "Login"]
VizAbrData[, AsOfDate := as_date(MinuteOfDay)]
# VizAbrData[, .N, by = AsOfDate]
# WhichDates <- VizAbrData[order(-AsOfDate)][, unique(AsOfDate)][1:14]
# WhichDates
# VizAbrData <- VizAbrData[AsOfDate %in% WhichDates]
# VizAbrData
VizAbrData$value[is.na(VizAbrData$value)] <- 0
# VizAbrData
library(hms)
VizAbrData[, MinuteOfDay_TIME := as_hms(MinuteOfDay)]
MyDT <- VizAbrData
MyDT[, AsOfDate := as.Date(AsOfDate)]
MyDT[, variable := as.character(variable)]
#WhichDates <- mydat[, .N, by = AsOfDate][order(-AsOfDate)][1:10, AsOfDate]
#WhichVariables <- mydat[, .N, by = variable][order(-variable)][1:10, variable]
#===============================================================================
output$distPlot1 <- renderPlot({
filtered_data_1 <- reactive({
subset(MyDT,
as.Date(MyDT$AsOfDate) %in% as.Date(input$Date1) &
as.character(MyDT$variable) %in% as.character(input$Metric1))
})
observeEvent(filtered_data_1(), {
updateSelectizeInput(session, 'filtered_data_1',
choices = names(filtered_data_1()))
})
updateSelectInput(session, "Date1", choices = MyDT$AsOfDate)
updateSelectInput(session, "Metric1", choices = MyDT$variable)
ggplot(data = filtered_data_1(),
aes(x = MinuteOfDay,
y = value)) +
geom_point(stat = "identity", col = "black") +
geom_line(stat = "identity", linewidth = 1.6, col = "red") +
labs(x = "MinuteOfDay", y = input$Metric1) +
ggtitle(paste0("Time Series: ", input$Metric1, sep = "")) +
theme(
plot.title = element_text(size=16, face= "bold", colour= "black", hjust = 0.5, margin = margin(t=10,b=-20)),
axis.title.x = element_text(size=16, face="bold", colour = "black"),
axis.title.y = element_text(size=14, face="bold", colour = "black"),
axis.text.x = element_text(size=14, face="bold", colour = "black"),
axis.text.y = element_text(size=14, face="bold", colour = "black"), # bold
strip.text.x = element_text(size = 14, face="bold", colour = "black" ),
strip.text.y = element_text(size = 14, face="bold", colour = "black"),
axis.line.x = element_line(color="black", size = 0.3),
axis.line.y = element_line(color="black", size = 0.3),
panel.border = element_rect(colour = "black", fill=NA)
) +
theme_classic()
})
}
#===============================================================================
# x <- seq(("2010/01/01"), as.Date("2016/12/11"), by = "day")
message("app: Create UI n")
ui <- fluidPage(
titlePanel("ABR Dashboard"),
tabsetPanel(
tabPanel("Time Series",
sidebarLayout(
selectInput(inputId = "Date1",
label = "Select Date",
choices = ""
),
selectInput(inputId = "Metric1",
label = "Which Metric",
choices = ""
)
),
mainPanel(
plotOutput("distPlot1")
)
)
)
#===============================================================================
message("app: Create App n")
# Run the application
shinyApp(ui = ui, server = server)
#===============================================================================