I’m learning about Shiny and keep running into the following issue.
-
server contain a data set.
-
It is used with reactive to filter on variables that the user may use ( filtered_data_1 )
-
the ui part keeos throwing the following error
Error in unique(MyDT$AsOfDate) : object ‘MyDT’ not found
The error makes sense. I create the reactive piece in server but the ui knows nothing about it.
how can i resolve this?
server <- function(input, output, session) {
MyDT <-
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"))
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))
})
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 = unique(MyDT$AsOfDate)
),
selectInput(inputId = "Metric1",
label = "Which Metric",
choices = unique(MyDT$variable)
)
),
mainPanel(
plotOutput("distPlot1")
)
)
)
)
#===============================================================================
message("app: Import Packages n")
# Run the application
shinyApp(ui = ui, server = server)
#===============================================================================