-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Comments
I think it is better to make functions with two arguments - 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" |
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:
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 Thanks in advance. |
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() |
Wow, Awesome!!! As always, I appreciate the work and quick your feedback. |
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 ...
Best regards
The text was updated successfully, but these errors were encountered: