Ошибка, не связанная с функцией grand_summary_rows (): Проблема с «мутировать () «ввод»…». x Ввод » … » не может быть переработан до размера 1

#r #gt

Вопрос:

Функция grand_summary() пакета gt выдает ошибку :

 Error: Problem with `mutate()` input `z`.
x Input `z` can't be recycled to size 1.
ℹ Input `z` is `(function (x) ...`.
ℹ Input `z` must be size 1, not 0.
 

Ошибка исчезает, если вместо NA есть число.

Рассмотрим этот минимальный пример:

 library(gt)
library(tidyverse)
library(data.table)

# this data.table gives the error.
dt <- data.table(x=c(1,2,3),y=c(10,20,30),z=c(5,7,9))

# this data.table runs without error (please uncomment the next line and run again.)
#dt <- data.table(x=c(1,8,3),y=c(10,20,30),z=c(5,7,9))

# weighted mean function that selectively returns the mean. The return value is NA for a specific condition and then the gt grand summary starts to give an error.

wtmean <- function() if(nrow(dt)>0 amp; sum(dt$x) >= 7) sum(dt$y)/sum(dt$x) else NA_real_
    
# the second line of the grandsummary will genrate an error.
        dt %>% 
            gt() %>% 
            grand_summary_rows(columns = c("x","y"),fns = list("Total" = "sum")) %>% 
            grand_summary_rows(columns = "z",fns = list("Weighted Mean" = ~wtmean()))