【发布时间】:2021-02-07 01:01:55
【问题描述】:
我有一个data.frame,其中“identificador_matriz_filial”列填充了 1 和 2。
用户可以选择 3 个选项:Matriz (1)、Filial (2) 或 Todos (3) 来绘制图形。如果用户选择 3,图表将同时显示 1 和 2(所有数据)。否则,将显示 1 OR 2。
但我失败了。
我的用户界面
box(plotOutput("regiao_plot"), widht = 8),
box(selectInput("regiao", "Região:",
choices = list("Estado" = "uf", "Região" = "regiao"),
selected = "Estado")),
box(selectInput("estab", "Estabelecimento:",
choices = list("Todos" = 3, "Matriz" = 1, "Filial" = 2),
selected = "Todos"))
我的服务器
output$regiao_plot <- renderPlot({
col <- sym(input$regiao)
mef <- as.numeric(input$estab)
Coop_ativas %>% filter(identificador_matriz_filial %in% ifelse(mef %in% c(1,2), mef, c(1:2))) %>%
select(!! col) %>% group_by(!! col) %>% count() %>%
arrange(desc(n)) %>% head(10) %>%
ggplot(aes(reorder(!! col, n), n), ) +
geom_bar(stat="identity", fill="steelblue") +
geom_text(aes(label=n), vjust=0.5, hjust=-0.5, color="darkgrey", size=3) +
labs(title = "Cooperativas Ativas por Estado",
subtitle = "02/2020",
caption = "Fonte: RFB, tratado por OBSCOOP/USP",
#tag = "Figure 1",
x = "Estado",
y = "Quantidade") +
theme_minimal() + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(0.0)) +
coord_flip()
当用户选择“待办事项”(3)时,图形显示1。当用户选择1或2时,图形向右。
我错过了什么?
【问题讨论】:
-
我也遇到了
ifelse的一些问题,我最终使用了switch,它按我的预期工作。但我也很好奇为什么。 -
您能否提供完整的代码和数据以使这篇文章可重现??
-
问题出在
ifelse(mef %in% c(1,2), mef, c(1:2)),当 mef 为 3 时,它只拾取 1 [c(1:2) 中的第一项]。所以,你需要修复你的过滤器。 -
@OceanSky_U 我将搜索
switch。谢谢!