I have the following shiny app. When I select a metric in the ui, it switches for a second and then resets.
The comments show some of the things I tried.
#===============================================================================
server <- function(input, output, session) {
MyDT1 <-
structure(list(MinuteOfDay = structure(c(1716379200, 1716379500,
1716379800, 1716380100, 1716380400, 1716380700, 1716381000, 1716381300,
1716381600, 1716381900), class = c("POSIXct", "POSIXt"), tzone = ""),
AgentCategory = c("Core", "Core", "Core", "Core", "Core",
"Core", "Core", "Core", "Core", "Core"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Queue",
"Login", "Tier", "Abr_Actuals", "Abr_Preds", "Abr_Diff"), class = "factor"),
value = c(2, 0, 1, 4, 4, 4, 2, 1, 0, 4), AsOfDate = structure(c(19865,
19865, 19865, 19865, 19865, 19865, 19865, 19865, 19865, 19865
), class = "Date"), MinuteOfDay_TIME = structure(c(25200,
25500, 25800, 26100, 26400, 26700, 27000, 27300, 27600, 27900
), units = "secs", class = c("hms", "difftime"))), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
MyDT2 <-
structure(list(MinuteOfDay = structure(c(1716379200, 1716379500,
1716379800, 1716380100, 1716380400, 1716380700, 1716381000, 1716381300,
1716381600, 1716381900), class = c("POSIXct", "POSIXt"), tzone = ""),
AgentCategory = c("Core", "Core", "Core", "Core", "Core",
"Core", "Core", "Core", "Core", "Core"), variable = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Queue",
"Login", "Tier", "Abr_Actuals", "Abr_Preds", "Abr_Diff"), class = "factor"),
value = c(20, 30, 10, 24, 24, 24, 20, 10, 50, 40), AsOfDate = structure(c(19865,
19865, 19865, 19865, 19865, 19865, 19865, 19865, 19865, 19865
), class = "Date"), MinuteOfDay_TIME = structure(c(25200,
25500, 25800, 26100, 26400, 26700, 27000, 27300, 27600, 27900
), units = "secs", class = c("hms", "difftime"))), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
MyDT <- rbind(MyDT1, MyDT2)
output$distPlot1 <- renderPlot({
filtered_data_1 <- reactive({
subset(MyDT,
as.character(MyDT$variable) %in% as.character(input$Metric1) &
as.Date(MyDT$AsOfDate) %in% as.Date(input$Date1))
})
# observeEvent(filtered_data_1(), {
# updateSelectInput(session,
# "Metric1",
# label = "Metric1",
# choices = filtered_data_1()$variable,
# selected = NULL
# )
# })
#
# current_Metric1 <- reactiveVal()
#
# observe({
# current_Metric1(input$Metric1)
# })
#
# # as normal except notice that
# # `current_in2()` sets the pre-selected choices
# observeEvent(eventExpr = input$current_Metric1,
# handlerExpr = {
# updateSelectInput(
# session = session,
# inputId = "current_Metric1",
# label = "current_Metric1",
# # keep what we had before
# selected = current_Metric1(),
# choices = MyDT[MyDT$Metric1 %in% input$Metric1, "variable"]
# )
# })
#
# observe({
# updateSelectInput(session,
# "Metric1",
# choices = filtered_data_1()$variable,
# selected = isolate(input$Metric1)
# )
# })
#
# observe({
# updateSelectInput(session,
# "Date1",
# choices = filtered_data_1()$AsOfDate,
# selected = isolate(input$Date1)
# )
# })
# observeEvent(filtered_data_1(), {
# updateSelectInput(session,
# "Metric1",
# choices = unique(filtered_data_1()$variable),
# selected = NULL
# )
# })
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()
})
}
#===============================================================================
message("app: Create UI n")
ui <- fluidPage(
titlePanel("ABR Dashboard"),
tabsetPanel(
tabPanel("Time Series",
sidebarLayout(
selectInput(inputId = "Date1",
label = "Select Date",
choices = c("")
),
selectInput(inputId = "Metric1",
label = "Which Metric",
choices = c("")
)
),
mainPanel(
plotOutput("distPlot1")
)
)
)
)
#===============================================================================
message("app: Import Packages n")
# Run the application
shinyApp(ui = ui, server = server)
#===============================================================================