Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple response in function() #96

Closed
robertogilsaura opened this issue May 9, 2022 · 4 comments
Closed

Multiple response in function() #96

robertogilsaura opened this issue May 9, 2022 · 4 comments

Comments

@robertogilsaura
Copy link

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
Copy link
Owner

gdemin commented May 10, 2022

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
Copy link
Author

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
Copy link
Owner

gdemin commented May 12, 2022

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
Copy link
Author

Wow, Awesome!!!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants