繁体   English   中英

如何在ShinyApp中订购因子变量

[英]How to order a factor variable within 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为“县”,“AGE_GROUP”语句。 由于“ Age_Group”已经是指定levels一个factor ,因此它将根据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)
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM