简体   繁体   English

R 闪亮的 dplyr GROUP_BY 命令缺少条目

[英]R shiny dplyr GROUP_BY command missing entries

I would like to create a dynamic data frame in shiny using the group_by command.我想使用 group_by 命令创建一个闪亮的动态数据框。

The number of rows of the desired table depends on rv$VAR value.所需表的行数取决于 rv$VAR 值。

As the number of rows is different between CL =="1" and CL =="2", because some districts (010102, 010103,160101) don't have vacant housings, it does not work.由于CL =="1"和CL =="2"的行数不同,因为有些地区(010102、010103、160101)没有空房,所以不起作用。

How can I display those districts with 0 in the table in order to get the same number of rows for each sort of housing ?我怎样才能在表格中显示那些带有 0 的地区,以便为每种住房获得相同的行数?

This is a part of my table :这是我表的一部分:

PC;COUN;DISTRICT;HOUSING;CL
01:0101;  010101;     01; 1
01:0101;  010101;     02; 1
01:0101;  010101;     03; 1
01:0101;  010101;     04; 2
01:0101;  010101;     05; 1
01:0102;  010102;     01; 1
01:0102;  010102;     02; 1
01:0102;  010102;     03; 1
01:0102;  010102;     04; 1
01:0102;  010102;     05; 1
01:0103;  010103;     01; 1
01:0103;  010103;     02; 1
01:0103;  010103;     03; 1
01:0103;  010103;     04; 1
01:0103;  010103;     05; 1
15:1501;  150101;     01; 1
15:1501;  150101;     02; 2
15:1501;  150101;     03; 1
15:1501;  150101;     04; 1
15:1501;  150101;     05; 1
16:1601;  160101;     01; 1
16:1601;  160101;     02; 1
16:1601;  160101;     03; 1
16:1601;  160101;     04; 1
21:2101;  210101;     01; 1
21:2101;  210101;     02; 1
21:2101;  210101;     03; 2
21:2101;  210101;     04; 1
21:2101;  210101;     05; 2
25:2501;  250101;     01; 1
25:2501;  250101;     02; 1
25:2501;  250101;     03; 1

This is a part of the code I wrote :这是我写的代码的一部分:

selectionAcc_View  <- reactive({

if (rv$CHAMP == "DISTRICT") {

      selectionAccomodations <- reactive({
        return(filter(myTable, DISTRICT %in% rv$VAR))})

tmp <- selectionAccomodations()

dfACC <- tmp %>%
    group_by(DISTRICT) %>%
    summarize(Accomodations=n())

dfMA <- filter(tmp, CL == "1" %>%
    group_by(DISTRICT) %>%
    summarize(MA=n())

dfVH <- filter(tmp, CL == "2" %>%
    group_by(DISTRICT) %>%
    summarize(VH=n())

# Create table
df <- data.frame(

    Total_Accomodations = c(dfACC$Accomodations), # Number of Accomodations

    Main_Accomodations = c(dfMA$MA), # Number of Main Accomodations

    Vacant_Housings = c(dfVH$VH) # Number of Vacant Housings

    ) # end of data.frame

  } # end of if

df

}) # End of selectionAcc_View  <- reactive({

# Output the table
output$df <- renderDataTable(selectionAcc_View(),options = list(paging =
FALSE, ordering = FALSE,searching = FALSE,info = FALSE)) 

}) # End of shinyServer(function(input, output, session) {

Please, would you have an idea ?请问,你有什么想法吗?

Thank you very much.非常感谢。

Decided to have a look as I needed some dplyr practice anyway.决定看一看,因为无论如何我都需要一些dplyr练习。 But it turns out this needs to use something like tidyr (that has the functions complete and spread ) to get everything working right.但事实证明,这需要使用tidyr类的tidyr (具有completespread功能)才能使一切正常工作。

The core problem is that some of the entries end up missing due to the fact that there are no records in the original data frame for certain combinations.核心问题是,由于某些组合的原始数据框中没有记录,因此某些条目最终会丢失。 This is like the problem that a "FULL OUTER JOIN" addresses in SQL, as opposed to the normal left and right join behavior that leave out potential entries with no corresponding data records.这就像 SQL 中的“FULL OUTER JOIN”解决的问题,与正常的左右连接行为相反,后者会遗漏没有相应数据记录的潜在条目。

complete works with factor levels to make your output "complete" when some of the summary records do not show up due to missing data of that nature. complete使用因子水平使您的输出“完整”,因为当某些摘要记录由于缺少该性质的数据而未显示时。 So I had to make DISTRICT and COUN and CL into factors for this to work.因此,我必须将 DISTRICT 和 COUN 和 CL 设为要使其起作用的因素。

spread spread out the values in a single column into multiple columns - so converts "long" data into "wide" data. spread单列中的值分散到多列中 - 因此将“长”数据转换为“宽”数据。

I made a complete(ish) example out of it.我用它做了一个完整的(ish)例子。 Haven't rigorously tested for correctness.没有经过严格的正确性测试。

library(shiny)
library(dplyr)
library(tidyr)
myTable <- read.csv(sep=";",text=
'PC;COUN;DISTRICT;HOUSING;CL
01;0101;  010101;     01; 1
01;0101;  010101;     02; 1
01;0101;  010101;     03; 1
01;0101;  010101;     04; 2
01;0101;  010101;     05; 1
01;0102;  010102;     01; 1
01;0102;  010102;     02; 1
01;0102;  010102;     03; 1
01;0102;  010102;     04; 1
01;0102;  010102;     05; 1
01;0103;  010103;     01; 1
01;0103;  010103;     02; 1
01;0103;  010103;     03; 1
01;0103;  010103;     04; 1
01;0103;  010103;     05; 1
15;1501;  150101;     01; 1
15;1501;  150101;     02; 2
15;1501;  150101;     03; 1
15;1501;  150101;     04; 1
15;1501;  150101;     05; 1
16;1601;  160101;     01; 1
16;1601;  160101;     02; 1
16;1601;  160101;     03; 1
16;1601;  160101;     04; 1
21;2101;  210101;     01; 1
21;2101;  210101;     02; 1
21;2101;  210101;     03; 2
21;2101;  210101;     04; 1
21;2101;  210101;     05; 2
25;2501;  250101;     01; 1
25;2501;  250101;     02; 1
25;2501;  250101;     03; 1')
myTable$DISTRICT <- as.factor(myTable$DISTRICT)
myTable$COUN <- as.factor(myTable$COUN)
myTable$CL <- as.factor(myTable$CL)

u <- shinyUI(fluidPage(
  titlePanel("Housing Statistics"),
  sidebarLayout(position = "left",
           sidebarPanel(h3("sidebar panel"),
                        selectInput("champmode","CHAMP Mode",c("DISTRICT","COUNTY")),
                        uiOutput("uivarselect")
                       ),
           mainPanel(h3("main panel"),
                     dataTableOutput('outdf')
                     )
             )))

s <- shinyServer(function(input,output) {

  rv <- reactiveValues(VAR = NULL,CHAMP = NULL)

  observeEvent(input$champmode,{ rv$CHAMP = input$champmode })
  observeEvent(input$varmode,{ rv$VAR = input$varmode })

  output$uivarselect <- renderUI({
    req(input$champmode)
    if (rv$CHAMP == "DISTRICT") {
        vals <- unique(as.character(myTable$DISTRICT))
     } else {
        vals <- unique(as.character(myTable$COUN))
     }
    selectInput("varmode","VAR Mode",vals)
  })


  selectionAccomodations <- reactive({
      if (rv$CHAMP == "DISTRICT") {
        return(filter(myTable,DISTRICT %in% rv$VAR))
      } else {
        return(filter(myTable,COUN %in% rv$VAR))
      }
    })

  selectionAcc_View <- reactive({
        tmp <- selectionAccomodations()
        if (nrow(tmp)==0) return(tmp) # don't process empty dataframe, just display
        tmp <- group_by(tmp,DISTRICT,COUN,CL) %>% summarize(cn = n()) %>% complete(CL)
        tmp[is.na(tmp)] <- 0 # replace NAs with zero
        df <- spread(tmp,CL,cn)
        names(df) <- c("DISTRICT","COUN","Main_Accomodations","Vacant_Housings")
        df$Total_Accomodations <- df$Main_Accomodations + df$Vacant_Housings;
        return(df)
    })

    # Output the table
  output$outdf <- renderDataTable({
      req(input$varmode) # keep from display before we are set up
      selectionAcc_View()
      },options = list(paging = F,ordering = F,searching = F,info = F))
  }
)
shinyApp(ui=u,server=s)

That yields:这产生:

在此处输入图片说明

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

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