gdemin / expss

expss: Tables and Labels in R
https://cran.r-project.org/web/packages/expss/
84 stars 16 forks source link

Multiple response in function() #96

Closed robertogilsaura closed 2 years ago

robertogilsaura commented 2 years ago

Hello @gdemin

I am starting to create a report using functions() for easier understanding or better code, but I have some doubts about using mrset() in a function.

In this code, I get an error on R3 (multiple response) but I don't know how I can pass the argument. I've tried with R3, 'R3_', 'R3_1, R3_2', 'R3_1 %to% R3_2', c(R3_1, R3_2), with data$... but I always get a different error between options. I've also tried mrset(..f()) and that doesn't work either.

How can multiple response be implemented in this function? I would like to use _f, because I don't know the maximum number of multiple options...

Thanks in advance ...

library(DT)
library(dplyr)
library(expss)
library(highcharter)

data <- data.frame(
        R1 = c(1, 2, 3, 4, 5, 6),
        R2 = c(2, 4, 6, 8, 10, 12),
        R3_1 = c(1, 1,3, 2, 1, 1),
        R3_2 = c(3, 2, 1, 1, 1, 2)
    )
attach(data)

f1 <- function(variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(variable)) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

f1m <- function(variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(mrset_f(variable))) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

listR1 <- f1(data$R1)
listR2 <- f1(data$R2)
listR3 <- f1m(data$R3_)

Best regards

gdemin commented 2 years ago

I think it is better to make functions with two arguments - data and character variable name. And for selection use mrset_p - it selects by perl-style regular expression:

library(DT)
library(dplyr)
library(expss)
library(highcharter)

data <- data.frame(
    R1 = c(1, 2, 3, 4, 5, 6),
    R2 = c(2, 4, 6, 8, 10, 12),
    R3_1 = c(1, 1,3, 2, 1, 1),
    R3_2 = c(3, 2, 1, 1, 1, 2)
)

f1 <- function(data, variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(data[[variable]])) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

f1m <- function(data, variable){
    tab <- data %>% 
        tab_cols(total()) %>% 
        tab_cells('|'=unvr(mrset_p(variable))) %>% 
        tab_stat_cpct(total_row_position = 'none') %>% 
        tab_pivot() 
    colnames(tab) <- c('lab', 'pct')
    dt <- as.datatable_widget(tab)
    grf <- hchart(tab,'bar', hcaes(lab, pct))
    return(list(tab, dt, grf))
}

listR1 <- f1(data, "R1")
listR2 <- f1(data, "R2")
listR3 <- f1m(data, "^R3_") # "^" means "starts with"
robertogilsaura commented 2 years ago

Thanks @gdemin. It runs ok. I save your recommendation.

Another question arose in this project. I have to create a header with new variables created from a condition of the type:

df=data.frame(A4_1=c(1,2,3,1,6,2,1), A4_2=c(6,6,8,3,4,5,6))
df %>%
    tab_cells(total()
        ,'Pregnancy1'=mrset(A4_1 %to% A4_2) %has% 6 
        ,'Pregnancy2'=mrset(A4_1 %to% A4_2) %has% 8
         ) %>%
    tab_stat_cases() %>% 
    tab_pivot()

The result for each line is TRUE/FALSE columns. Can we change label (TRUE/FALSE) of the columns in the table request? I tried using the label= parameter of mrset() but it had no effect on the output. I know I can edit the final etable object or create the variables initially, but in case there is a trick utility to request on the same table.

Thanks in advance. Best regards.

gdemin commented 2 years ago

You can set value labels on the result:

library(expss)
df=data.frame(A4_1=c(1,2,3,1,6,2,1), A4_2=c(6,6,8,3,4,5,6))
df %>%
    tab_cells(total()
              ,'Pregnancy1'=mrset(A4_1 %to% A4_2) %has% 6 %>% set_val_lab(c("A" = TRUE, "B" = FALSE))
              ,'Pregnancy2'=mrset(A4_1 %to% A4_2) %has% 8 %>% set_val_lab(c("C" = TRUE, "D" = FALSE))
    ) %>%
    tab_stat_cases() %>% 
    tab_pivot()
robertogilsaura commented 2 years ago

Wow, Awesome!!!

As always, I appreciate the work and quick your feedback.