设置条件group_by

我有一组看起来像这样的数据:

+----------+------------+-------+-------+
|  step1   |   step2    | step3 | step4 |
+----------+------------+-------+-------+
| Region 1 | District A | 1A    |   571 |
| Region 1 | District A | 1A    |   356 |
| Region 1 | District A | 1B    |   765 |
| Region 1 | District B | 1B    |   752 |
| Region 2 | District C | 2C    |   885 |
| Region 2 | District C | 2D    |    73 |
| Region 2 | District D | 2D    |   241 |
| Region 2 | District D | 2D    |   823 |
| Region 3 | District E | 3E    |   196 |
| Region 3 | District E | 3E    |   103 |
| Region 3 | District F | 3E    |   443 |
| Region 3 | District F | 3F    |   197 |
+----------+------------+-------+-------+

我已经设置了以下脚本,该脚本的编写方式是使用selectizeGroupServer在步骤1,步骤2和步骤3之间自动设置过滤,以便将它们链接在一起(例如,如果您对区域1进行过滤它将仅返回Step2和Step3中的相关选项。

如果您希望以直接的方式将其返回到group_by_all,下面的脚本将返回我正在寻找的结果。因此,在初次运行时,它将显示所有11个结果的图形输出。如果我按区域1进行过滤,它将在步骤4中返回链接到区域1的所有四个图的图形。

但是我想以某种方式设置它,当我选择一个选项时,它实际上将根据其下面的层次结构选项进行分组。因此,如果我按区域1进行过滤,它将返回两列:A区的总和(1692)和B区的总和(752)。如果同时选择了区域1和区域A,则它将返回两列:1A的总计(927)和与区域A关联的1B的总计(765)。

我该如何设置它以实现此目的?

library(highcharter)
library(shiny)
library(shinyWidgets)
library(dplyr)

step1 <- c('Region 1','Region 1','Region 2','Region 3','Region 3')
step2 <- c('District A','District A','District B','District C','District D','District E','District F','District F')
step3 <- c('1A','1A','1B','2C','2D','3E','3F')
step4 <- c(571,356,765,752,885,73,241,823,196,103,443,197)

ui <- fluidPage(
  fluidRow(
    column(
      width = 5,offset = 1,panel(
        selectizeGroupUI(
          id = "foo",params = list(
            Step1 = list(inputId = "step1",title = "Step1:"),Step2 = list(inputId = "step2",title = "Step2:"),Step3 = list(inputId = "step3",title = "Step3:")
          ))
      ),highchartOutput(outputId = "table")
    )
  )
)

server <- function(input,output,session) {

  abc <- callModule(
    module = selectizeGroupServer,id = "foo",data = df,vars = c("step1","step2","step3")
  )

  output$table <- renderHighchart({

    bar <- abc()

    xyz <- bar %>% filter(is.null(input$step1) | step1 %in% input$step1,is.null(input$step2) | step2 %in% input$step2,is.null(input$step3) | step3 %in% input$step3) %>% group_by_all() %>% summarise(results = sum(step4))


    highchart() %>% hc_add_series(data = xyz,type = "column",hcaes(y = results),showInLegend = TRUE) %>% hc_add_theme(hc_theme_flat())


  })


}

谢谢!

liyazhao 回答:设置条件group_by

首先,我们需要找出要分组的列。在这种情况下,我假设它是具有多个选项的第一列。其余代码非常相似,除了group_by_allgroup_by_at代替之外。

output$table <- renderHighchart({

        bar <- abc()

        # find out which column to group by (first column with more than 1 distinct value)
        summ_column <- bar %>%
            summarise_all(~ length(unique(.))) %>% {colnames(.)[.>1]} %>% first()

        xyz <- bar %>% group_by_at(summ_column) %>% summarise(results = sum(step4))


        highchart() %>% hc_add_series(data = xyz,type = "column",hcaes(y = results),showInLegend = TRUE) %>% hc_add_theme(hc_theme_flat())


    })

如果您为单个选项选择了多个值,则此方法将无效,但是该解决方案应该非常相似。

,

似乎您正在寻找aggregate。请检查以下内容:

library(highcharter)
library(shiny)
library(shinyWidgets)
# library(dplyr)

DF <- data.frame(
  step1 = c('Region 1','Region 1','Region 2','Region 3','Region 3'),step2 = c('District A','District A','District B','District C','District D','District E','District F','District F'),step3 = c('1A','1A','1B','2C','2D','3E','3F'),step4 = c(571,356,765,752,885,73,241,823,196,103,443,197),stringsAsFactors = FALSE)

ui <- fluidPage(
  fluidRow(
    column(
      width = 5,offset = 1,panel(
        selectizeGroupUI(
          id = "foo",params = list(
            Step1 = list(inputId = "step1",title = "Step1:"),Step2 = list(inputId = "step2",title = "Step2:"),Step3 = list(inputId = "step3",title = "Step3:")
          ))
      ),highchartOutput(outputId = "table")
    )
  )
)

server <- function(input,output,session) {

  abc <- callModule(
    module = selectizeGroupServer,id = "foo",data = DF,vars = c("step1","step2","step3")
  )

  output$table <- renderHighchart({
    req(abc())
    bar <- aggregate(step4 ~ step1+step2,abc(),sum)
    highchart() %>% hc_add_series(data = bar,hcaes(y = step4),showInLegend = TRUE) %>% hc_add_theme(hc_theme_flat())
  })

}

shinyApp(ui,server)

Result

本文链接:https://www.f2er.com/2920260.html

大家都在问