【问题标题】:How to order a factor variable within shinyapp如何在 shinyapp 中订购因子变量
【发布时间】:2019-07-28 14:00:05
【问题描述】:

在闪亮的框架内,我正在尝试创建一个交互式表格。如此屏幕截图所示,年龄变量未排序。 我该如何解决这个问题?

我尝试了以下代码来订购年龄组变量,但没有奏效。

 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())
})

谢谢,

纳德

【问题讨论】:

    标签: r shiny dplyr


    【解决方案1】:

    我们可以为“County”、“Age_Group”添加arrange 语句。由于 'Age_Group' 已经是 factor 并指定了 levels,它将根据 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)
      })
    

    【讨论】:

    • 谢谢阿克伦。您的代码会产生错误:无法将“c("reactiveExpr", "reactive")' 类强制转换为 data.frame
    • @NaderMehri。您能否展示一个可重现的小示例,我无法测试您的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多