changing the labels of options in table

i have created a function which is working fine, now i want to take the labels from global option in rmarkdown to change the labels of Mean , median, percentile

So in global option if someone want to show mean as Avg or averagea then it should change same as percenties25 or 75

i was trying the below update but it was not working

library(expss)
library(dplyr)

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)
  valid_n <- function(x) sum(!is.na(x))
  
  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)
}

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



I was trying like below but it was not working .

t1 <- cross_fun(
  data,
  data[[var1]],
  col_vars = Banner,
  fun = combine_functions(
    !!p25 = perc_25,
    "Mean" = Mean,
    "Median" = Median,
    !!p75 = perc_75,
    "Valid N" = valid_n
  )
)

to inject alternate function names to combine_functions you need to
a) use the walrus operator :=
b) use do.call to run combine_functions on the seperated out list

library(expss)
library(dplyr)

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
  )
  
  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)

this example uses np25 variable hardcoded to "something else", this could come from fun1 arguments of course

i am getting this error sir, and one more thing if i want to add one more parameter like stats

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"))

I tested the script I provided you, it returns

|              |                    |     1 |
 | ------------ | ------------------ | ----- |
 | data[[var1]] | something else.25% |  93.0 |
 |              |               Mean | 104.3 |
 |              |             Median | 110.0 |
 |              |      75th Perc.75% | 110.0 |
 |              |            Valid N |   3.0 |

with no raised errors.
It also looks like you forgot to share an error message.

result <- fun1(df, var = "hp", Banner = banner1,)
Error in fun1(df, var = "hp", Banner = banner1, ) :
object 'combine_functions' not found

I would guess you do not have expss library loaded, as combine_functions comes from that.
You probably need to add library(rlang) also, because list2 that I used relies on that.

one more thing if i want to add one more parameter like stats so if sometime i want o show average only in table or i want to show perc25 or perc7 what i should i update ..???

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.