R闪亮 |如何将月份 <ord> 列为单选按钮列表
R shiny | How to list Months <ord> as a radioButton list
我正在处理摘要 table,如下所示:
这是热图的数据集。在我的 Shiny 应用程序中,我想通过选择年份和月份的选项来可视化热图(table 已经排除了空值)。
我已经为我的选项列表提取了年份和月份,如下所示:
OverageYPool <- Overagelogbypool$Year %>% as.list(Year) %>% unique()
OverageMPool <- Overagelogbypool$Month %>% as.list(Month) %>% unique()
这是我的 OverageMPool 的显示方式:
> 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
>
虽然年份没有任何问题,但当我在应用程序中显示月份列表时,我得到的是月份编号而不是月份名称,并且出现错误(<- 中的错误:替换长度为零) 代替情节。我假设输入格式与 table 格式不匹配,这就是我收到错误的原因。
我不确定如何转换该列表中的值以匹配 table 中的格式(并按月排序),而不是在列表中对它们进行硬编码。有什么建议吗?
为了完整性,我正在添加函数的其余代码:
反应式过滤
filteredPoolbyYandM <- reactive({
dplyr::filter(Overagelogbypool, Year == input$poolyear & Month == input$poolmonth)
})
output$Poolheatmap <- renderPlot(V_Overagelogbypool(filteredPoolbyYandM()), res = 96)
绘图函数
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)")
}
提前致谢!
单选框中显示的数字取决于 radioButtons
与向量相比使用因子列表的方式。如果将它们保留为向量,则不会出现此问题。
OverageMPool <- sort(unique(Overagelogbypool$Month))
或者,在 base R 中有 month.abb
,其中包含按顺序缩短的月份名称。
我正在处理摘要 table,如下所示:
这是热图的数据集。在我的 Shiny 应用程序中,我想通过选择年份和月份的选项来可视化热图(table 已经排除了空值)。
我已经为我的选项列表提取了年份和月份,如下所示:
OverageYPool <- Overagelogbypool$Year %>% as.list(Year) %>% unique()
OverageMPool <- Overagelogbypool$Month %>% as.list(Month) %>% unique()
这是我的 OverageMPool 的显示方式:
> 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
>
虽然年份没有任何问题,但当我在应用程序中显示月份列表时,我得到的是月份编号而不是月份名称,并且出现错误(<- 中的错误:替换长度为零) 代替情节。我假设输入格式与 table 格式不匹配,这就是我收到错误的原因。
我不确定如何转换该列表中的值以匹配 table 中的格式(并按月排序),而不是在列表中对它们进行硬编码。有什么建议吗?
为了完整性,我正在添加函数的其余代码:
反应式过滤
filteredPoolbyYandM <- reactive({
dplyr::filter(Overagelogbypool, Year == input$poolyear & Month == input$poolmonth)
})
output$Poolheatmap <- renderPlot(V_Overagelogbypool(filteredPoolbyYandM()), res = 96)
绘图函数
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)")
}
提前致谢!
单选框中显示的数字取决于 radioButtons
与向量相比使用因子列表的方式。如果将它们保留为向量,则不会出现此问题。
OverageMPool <- sort(unique(Overagelogbypool$Month))
或者,在 base R 中有 month.abb
,其中包含按顺序缩短的月份名称。