hide values with special character in table not working properly

I am trying to hide values with "--" within my function but its not working properly.

so the condition is if Valid N (total cases) is less than 3 then replace the values in column of mean and median with "--"
same as for 25perc and 75 perc for valid_N is less than 4

@ nirgrahamuk as you have given some solution , may be you can help more.



library(expss)
library(dplyr)


df <- data.frame(c1= c(12,13,NA,0,0,19,NA),
                 c2= c(0,14,0,NA,0,13,0),
                 c3= c(101,0,19,22,16,45,12),
                 c4= c(0,65,0,NA,0,45,0),
                 c5= c(1,0,NA,1,1,1,1),
                 c6= c(1,1,1,NA,0,NA,0),
                 c7= c(NA,NA,NA,1,0,1,0),
                 c8= c(1,NA,NA,1,0,1,0))


var <- "c3"
val_lab(df$c5)<-c("Dept 1"=1)
val_lab(df$c6)<-c("Dept 2"=1)
val_lab(df$c7)<-c("Dept 3"=1)
val_lab(df$c8)<-c("Dept 4"=1)

banner1 <- list(total(),df$c5,df$c6,df$c7,df$c8)

Mask_v <- function(fun,masking,Mask_val){
  function(x) {val <- fun(x)
  hval <- val
  if(masking==TRUE)
    hval <- "--"
  ifelse(val <= Mask_val,hval,val)}
}

fun1 <- function(data, var, Banner, masking = FALSE,Mask_val=105) {
  var1 <- rlang::parse_expr(var)
  
  
  perc_25_raw <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
  perc_75_raw <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
  Mean_raw <- function(x) mean_col(x, na.rm = TRUE)
  Median_raw <-  function(x) median_col(x, na.rm = TRUE)
  
  
  perc_25 <- perc_25_raw
  perc_75 <- perc_75_raw
  Mean <- Mean_raw
  Median <- Median_raw
  
  
  valid_n <- function(x) sum(!is.na(x))
  
  N <- valid_n(data[[var1]])
  
  print(N)
  if(N <= 4){
    perc_25 <- Mask_v(perc_25_raw,TRUE,Mask_val)
    perc_75 <- Mask_v(perc_75_raw,TRUE,Mask_val)
  }
  if(N <= 3){
    Mean <- Mask_v(Mean_raw,TRUE,Mask_val)}
  if(N<=4){
    Median <-  Mask_v(Median_raw,TRUE,Mask_val)}
  
  t1 <- cross_fun(
    data,
    data[[var1]],
    col_vars = Banner,
    fun = combine_functions(
      "25th Perc" = perc_25,
      "Mean" = Mean,
      "Median" = Median,
      "75th Perc" = perc_75,
      "Valid N" = valid_n
    )
  )
  
  
  return(t1)
}

debugonce(fun1)
result <- fun1(df, var = "c3", Banner = banner1, masking = TRUE)
print(result)

the output should be look like

row_labels #Total Dept 1 Dept 2 Dept 3 Dept 4
data[[var1]] 25th Perc.25% 12 14 -- --
data[[var1]] Mean 30.71429 39.2 40 --
data[[var1]] Median 19 22 19 --
data[[var1]] 75th Perc.75% 45 73 -- --
data[[var1]] Valid N 7 5 3 2