selecting the stats i want to show

I want to add one more parameter in my function which could check and get the stats i want to show in table.

for example if i want to show all stats or average only or i want to show percentile25 or percentile75

result <- fun1(df, var = "hp", Banner = banner1,stats ="all")
result <- fun1(df, var = "hp", Banner = banner1,stats ="Percentile25")
result <- fun1(df, var = "hp", Banner = banner1,stats =c("median","Percentile25"))


library(expss)
library(dplyr)
library(rlang)

df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)


fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
                 perc_75 = getOption("percentile75", default = "75th percentile")) {
  var1 <- rlang::parse_expr(var)
  
  perc_25 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
  perc_75 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
  Mean <- function(x) mean(x, na.rm = TRUE)
  Median <- function(x) median(x, na.rm = TRUE)
  valid_n <- function(x) sum(!is.na(x))
  
  np25 <- "something else"
  flist <- list2(
    !!np25 := perc_25,
    "Mean" = Mean,
    "Median" = Median,
    "75th Perc" = perc_75,
    "Valid N" = valid_n
  )
  

if (stats != "all") {
    stats <- tolower(stats)
    flist <- flist [names(flist) %in% stats]
  } else {
    flist <- flist 
  }

cmb_fun <- do.call(combine_functions,flist)
  t1 <- cross_fun(
    data,
    data[[var1]],
    col_vars = Banner,
    fun = cmb_fun
  )
  
  
  return(t1)
}

# Example usage with masking set to TRUE
result <- fun1(df, var = "hp", Banner = banner1,)
print(result)

also tried with map2, but not working any different idea..????

I forsee that you have decisions to make around how you want ot allow users to both rename/relabel the functions uses, and include or exclude funcstions (by name) because you have the issue that these would presumably need to align...
but anyway, I did some minimal clean up of your function
removed np25 which should have always been your p25....
actually added a stats argument to your fun1
added a print out that explains what didnt work at the point where your example failed.


library(expss)
library(dplyr)
library(rlang)

df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)


fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
                 perc_75 = getOption("percentile75", default = "75th percentile"),
                 stats="all") {
  var1 <- rlang::parse_expr(var)
  
  perc_25 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
  perc_75 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
  Mean <- function(x) mean(x, na.rm = TRUE)
  Median <- function(x) median(x, na.rm = TRUE)
  valid_n <- function(x) sum(!is.na(x))
  
  flist <- list2(
    !!p25 := perc_25,
    "Mean" = Mean,
    "Median" = Median,
    "75th Perc" = perc_75,
    "Valid N" = valid_n
  )
  
  stat_names <- names(flist)
  
  if (stats != "all") {
    stats <- tolower(stats)
    flist <- flist [stat_names %in% stats]
  } else {
    flist <- flist 
  }
  
  if(length(flist)>0)
  cmb_fun <- do.call(combine_functions,flist) else 
    stop(paste0("No stats chosen as possible stat names are ",paste0(
      stat_names,collapse=", "
    ),"\n and selection was : ",paste0(stats,collapse=", ")))
  
  t1 <- cross_fun(
    data,
    data[[var1]],
    col_vars = Banner,
    fun = cmb_fun
  )
  
  
  return(t1)
}

result <- fun1(df, var = "hp", Banner = banner1,stats="Percentile25")
print(result)

so once i filter the required stats from the flist like below
flist <- list2(
25th percentile= perc_25,
Mean = Mean,
Median = Median,
75th Perc = perc_75,
"Valid N" = valid_n
)

stat_names <- names(flist)

if (stats != "all") {
stats <- tolower(stats)
flist <- flist [stat_names %in% stats]
} else {
flist <- flist
}

so lets say i will have Mean and median in flist, now how can i change the names from global options is there any option sir ..???

like i am doing below
df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)
options(percentile25 = "Perc_25")

fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
perc_75 = getOption("percentile75", default = "75th percentile"),
stats="all") {
var1 <- rlang::parse_expr(var)

perc_25 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
perc_75 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
Mean <- function(x) mean(x, na.rm = TRUE)
Median <- function(x) median(x, na.rm = TRUE)
valid_n <- function(x) sum(!is.na(x))

flist <- list2(
"Percentile 25"= perc_25,
"Mean" = Mean,
"Median" = Median,
"75th Perc" = perc_75,
"Valid N" = valid_n
)

stat_names <- names(flist) %>% tolower()

if (stats != "all") {
stats <- tolower(stats)
flist <- flist [stat_names %in% stats]
} else {
flist <- flist
}

Here can i change the names from global option but also sometimes flist can be "all" then names needed to be change for all or sometimes for percentile25 or percentile75

if(length(flist)>0)
cmb_fun <- do.call(combine_functions,flist) else
stop(paste0("No stats chosen as possible stat names are ",paste0(
stat_names,collapse=", "
),"\n and selection was : ",paste0(stats,collapse=", ")))

t1 <- cross_fun(
data,
data[[var1]],
col_vars = Banner,
fun = cmb_fun
)

return(t1)
}

debugonce(fun1)
result <- fun1(df, var = "hp", Banner = banner1,stats="percentile 25")
print(result)

please format your posts, its very hard to read, and not clear what you are telling or asking.

This the the below example i am trying and before creating cmb_fun , i want to change the label of percentile 25 from global option dynamically.

how can i change it ..??

library(expss)
library(dplyr)
library(rlang)

df <- mtcars
df <- head(df,3)
var <- "hp"
df$vs <- 1
banner1 <- list(df$vs)
options(percentile25 = "Perc_25")



fun1 <- function(data, var, Banner,p25 = getOption("percentile25", default = "25th percentile"),
                 perc_75 = getOption("percentile75", default = "75th percentile"),
                 stats="all") {
  var1 <- rlang::parse_expr(var)
  
  perc_25 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2]
  perc_75 <- function(x) quantile(x, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[4]
  Mean <- function(x) mean(x, na.rm = TRUE)
  Median <- function(x) median(x, na.rm = TRUE)
  valid_n <- function(x) sum(!is.na(x))
  
  flist <- list2(
    "Percentile 25"= perc_25,
    "Mean" = Mean,
    "Median" = Median,
    "75th Perc" = perc_75,
    "Valid N" = valid_n
  )
  
  stat_names <- names(flist) %>% tolower()
  
  if (stats != "all") {
    stats <- tolower(stats)
    flist <- flist [stat_names %in% stats]
  } else {
    flist <- flist 
  }
  
  
  if(length(flist)>0)
    cmb_fun <- do.call(combine_functions,flist) else 
      stop(paste0("No stats chosen as possible stat names are ",paste0(
        stat_names,collapse=", "
      ),"\n and selection was : ",paste0(stats,collapse=", ")))
  
  t1 <- cross_fun(
    data,
    data[[var1]],
    col_vars = Banner,
    fun = cmb_fun
  )
  
  
  return(t1)
}

debugonce(fun1)
result <- fun1(df, var = "hp", Banner = banner1,stats="percentile 25")
print(result)

after the line flist <- flist
i want to change the percentile25 = "Perc_25" dynamically
i want to make it dynamic like whatever i will select in stats , it should apply for all selected stats like

result <- fun1(df, var = "hp", Banner = banner1,stats=c("percentile 25"))
result <- fun1(df, var = "hp", Banner = banner1,stats="all")
result <- fun1(df, var = "hp", Banner = banner1,stats=c("percentile 25","percentile 75")

I have created a new question

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.