r - 如何在 shinyapp 中订购因子变量
问题描述
在闪亮的框架内,我正在尝试创建一个交互式表格。如此屏幕截图所示,年龄变量未排序。我怎样才能解决这个问题?
我尝试了以下代码来订购年龄组变量,但没有奏效。
workforce_t <- workforce
output$workforce_table_1 <- renderTable ({
#sort the age_group variable
workforce_t$Age_Group <- factor(
workforce_t$Age_Group,
levels = c(
"16+",
"16 to 24",
"25 to 34",
"35 to 44",
"45 to 54",
"55 to 64",
"65+"
),
labels = c(
"16+",
"16 to 24",
"25 to 34",
"35 to 44",
"45 to 54",
"55 to 64",
"65+"
)
)
info <- reactive ({
out <- workforce_t %>%
filter (County %in% input$county_workforce,
#Year %in% input$years,
Indicator %in% input$Indicator_workforce)
return(out)
})
(info())
})
谢谢,
纳德
解决方案
我们可以arrange
为“County”、“Age_Group”添加一个语句。由于 'Age_Group' 已经是一个factor
withlevels
指定的,它会根据levels
. 此外,分配和return
是可选的。它可以通过删除那些变得紧凑
info <- reactive({
workforce_t %>%
filter (County %in% input$county_workforce,
#Year %in% input$years,
Indicator %in% input$Indicator_workforce) %>%
arrange(County, Age_Group)
})
推荐阅读
- vue.js - 在子组件和父组件 Vue js 之间共享值
- python - 我应该把 Celery 配置文件放在哪里?
- django - Django 测试:如何从 HTTPResponseRedirect 对象中获取 html 字符串
- python - 在 unittest 期间全局禁用 joblib.memory 缓存
- node.js - 在 NodeJS 中更新猫鼬
- django - 用于编辑大型数据模型结构的 django 后端的 react/redux 前端的优雅架构?
- ruby-on-rails - Rails RecordNotFound 没有路由到 404 页面,而路由错误确实
- php - 序列化字符串包含对无法实例化的类的引用
- python - Python - 如何使 pm2 与多处理一起工作?
- angular - 如何从注入的模拟服务中模拟 observable 和 geter