Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grand_summary_rows errors on all NA columns #471

Closed
smingerson opened this issue Jan 17, 2020 · 3 comments · Fixed by #887
Closed

grand_summary_rows errors on all NA columns #471

smingerson opened this issue Jan 17, 2020 · 3 comments · Fixed by #887

Comments

@smingerson
Copy link

smingerson commented Jan 17, 2020

latest from github (GithubSHA1: 883df9a)

My use case is an empty table showing what information will be displayed. The end user will enter info in a Shiny app and then the table will update. My workaround will be to populate the table with 0s. Edit: this also happens with 0. The error happened in the print method, when it is applying the functions to the columns.

library(gt)
grand_summary_rows(gt(data.frame(a = c(1,2), b = NA_real_)), columns = vars(b),
                   fns = list(max = ~max(.)))
#> Error: Column `b` must be length 1 (the number of rows), not 0

Created on 2020-01-17 by the reprex package (v0.3.0)

@rich-iannone
Copy link
Member

Thanks for reporting this and sorry for waiting until now to respond. I'm able to reproduce the error in my environment and I do believe this requires fixing.

@jkgrain
Copy link

jkgrain commented Jul 9, 2020

I am running into the same issue. The following reprex might help trace the problem. I think it has to do with the mean of a vector that is all NA returning NaN even with na.rm=TRUE.

library(dplyr)
library(gt)

x <- c(1:6)
y <- c(6:2, NA)
z <- rep(NA, 6)

df <- tibble(x,y,z)

df <- mutate_if(df, is.logical, as.numeric)

df %>%
  gt() %>%
  grand_summary_rows(
    columns = c(1:3),
    fns = list(
      Avg = ~ mean(., na.rm = T)
    )
  ) 

mean(df$x, na.rm = TRUE)
mean(df$y, na.rm = TRUE)
mean(df$z, na.rm = TRUE)

@jkgrain
Copy link

jkgrain commented Jul 9, 2020

I was able to trap for the problematic column with this variation on the code:

library(dplyr)
library(gt)

x <- c(1:6)
y <- c(6:2, NA)
z <- rep(NA, 6)

df <- tibble(x,y,z)

df %>%
  gt() %>%
  grand_summary_rows(
    columns = c(1:3),
    fns = list(
      Avg = ~ mean(., na.rm = T)
    )
  ) 

df %>%
  gt() %>%
  grand_summary_rows(
    columns = which(sapply(df, class) != "logical"),
    fns = list(
      Avg = ~ mean(., na.rm = T)
    )
  ) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment