I'm working with a summary table that looks like this:
This is the dataset for a heat map. In my Shiny app, I would like to visualize the heatmap with the option of selecting the year and month (the table is already excluding null values).
I have extracted the year and the month for my lists of options as follows:
OverageYPool <- Overagelogbypool$Year %>% as.list(Year) %>% unique()
OverageMPool <- Overagelogbypool$Month %>% as.list(Month) %>% unique()
This is how my OverageMPool is displayed:
> OverageMPool
[[1]]
[1] Jul
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[2]]
[1] Aug
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[3]]
[1] Sep
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[4]]
[1] Oct
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[5]]
[1] Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[6]]
[1] Dec
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[7]]
[1] Jan
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[8]]
[1] Feb
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
[[9]]
[1] Mar
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
>
While the year doesn't present any issues, when I display the month list in the app I get the month number rather than the month name, and I get an error (Error in <-: replacement has length zero) in place of the plot. I assume that there is no match between the input format and the table format and that's why I'm getting the error.
I'm not sure how to convert the values in that list to match the format in the table (and to order them by month) other than hardcode them in the list. Any suggestions?
I'm adding the rest of the code for the function for completeness:
reactive filtering
filteredPoolbyYandM <- reactive({
dplyr::filter(Overagelogbypool, Year == input$poolyear & Month == input$poolmonth)
})
output$Poolheatmap <- renderPlot(V_Overagelogbypool(filteredPoolbyYandM()), res = 96)
plot function
filteredPoolbyYandM <- Overagelogbypool
V_Overagelogbypool <- function(filteredPoolbyYandM) {
filteredPoolbyYandM %>%
ggplot(aes(x = Context, y = Pool))
geom_tile(mapping = aes(fill = Exceeding), alpha = 0.5)
scale_x_discrete(expand = c(0.3, 0))
labs(x = "Destination call context", y = "Pool", color = "Exceeding (m)")
}
Thanks in advance!
CodePudding user response:
The numbers appearing in the radio boxes is down to the way the radioButtons
works with a list of factors compared to a vector. If you keep them as a vector you won't have this issue.
OverageMPool <- sort(unique(Overagelogbypool$Month))
Alternatively, in base R there is month.abb
which contains the shortened month names in order.