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

Discussion: a small utility function for printing well formatted data #727

Closed
Melkiades opened this issue Aug 30, 2023 · 2 comments
Closed
Labels
discussion exploration explores ways that might be source of errors or features sme

Comments

@Melkiades
Copy link
Contributor

I was wondering if it would be nice to transform the following afun into a utility function. It would be relatively easy. I wanted to ask @edelarua @ayogasekaram if you think it would be nice to have and if you have seen someone requesting this kind of feature (I think it is done like this in a couple of cases in tern).

library(dplyr)

# Making a fake data similar to question
cf <-function(x) {
    x <- as.character(x)
    sapply(x, function(xx) switch(xx,
                                  "n" = round(runif(1, 10, 30)),
                                  "Mean" = rnorm(1, 2, 1),
                                  NA))
}
df <- expand.grid(
    Group = c("m1", "p"),
    Analysis = c("An 1", "An 2"),
    Metric = c("n", "Mean")
) %>% 
    mutate(Value = cf(Metric)) %>% 
    print()

library(rtables)

# Dummy analysis function to represent the data in "Value"
# -> To understand the current solution please read these in this order:
#    * ?analyze
#    * ?additional_fun_params
#    * ?spl_context
special_afun <- function(x, .spl_context, .df_row){
    
    # Extract the labels for each "analysis"
    labs <- as.character(x)
    
    # Extract the column subset of data
    col_sbset <- .spl_context$cur_col_subset
    res_df <- .df_row[col_sbset[[length(col_sbset)]], ]
    
    # Extract the values following the right labels ("Metric" here)
    metric_order <- sapply(labs, function(x) which(x == res_df$Metric))
    res_lst <- as.list(res_df$Value[metric_order])
    names(res_lst) <- labs # to be safe: add the labels
    
    # Set custom formats
    res_fmt <- setNames(list("xx", "xx.x"), labs)
    
    # Returning the list of results for one column at a time
    in_rows(.list = res_lst, .formats = res_fmt)
}

basic_table() %>% 
    split_rows_by("Analysis") %>% 
    split_cols_by("Group") %>% 
    analyze("Metric", afun = special_afun) %>% 
    build_table(df)
@Melkiades Melkiades added discussion exploration explores ways that might be source of errors or features sme labels Aug 30, 2023
@edelarua
Copy link
Contributor

I think it's a good idea! I've gotten requests for direct printing of data into tables before.

@Melkiades
Copy link
Contributor Author

I think it's a good idea! I've gotten requests for direct printing of data into tables before.

Ok so let's gather names for this analysis function! Do you think it should reside in tern instead of in rtables?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion exploration explores ways that might be source of errors or features sme
Projects
None yet
Development

No branches or pull requests

2 participants