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

Layer templates don't accept added parameters on left side of = within function #141

Open
haodafa2019 opened this issue Sep 27, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@haodafa2019
Copy link

haodafa2019 commented Sep 27, 2023

Prerequisites

layer template question

Description

I have one question on new_layer_template or question on add_total_row in desc_count.
I tried to develop template for categorical variable summary and continues variable summary to generate the demo tables.
In template boa_demo_count_template, I added paramter {count_label} so I can specify the label. And it works!
I tried to add similar parameter in template boa_demo_desc_template ( "desc_label" = f_str("xx", blank_), ) so I can add label for the vairable. but {des_label} not works. The purpose of this step is too add label above the summary as there is no total_row option in desc_count function. So either in template there is way to add self-defined label in template or add label for continued variable are fine. can you give suggestion?

Below are the code I tried,

# demo_count

new_layer_template(
  "boa_demo_count_template",
  group_count(...) %>%
    add_total_row(f_str(""), count_missings=FALSE, sort_value=-Inf) %>%
    set_total_row_label({count_label}) %>%
    set_format_strings(f_str("xx (xx.x)", n, pct))%>%
    set_missing_count(f_str("xx (xx.x)", n,pct),Missing = NA) 
)
get_layer_templates()

# demo description

new_layer_template(
  "boa_demo_desc_template",
  group_desc(...) %>% 
    set_custom_summaries(
      blank_=as.numeric(NA),
      nmiss=sum(!is.na(.var)),
      nmiss_pct=sum(!is.na(.var))/length(.var)*100,
      missing = sum(is.na(.var)),
      missing_pct= sum(is.na(.var))/length(.var)*100
    ) %>% 
    set_format_strings( 
      **"desc_label"  = f_str("xx", blank_),** 
      "n (%)"         = f_str("xx (xx.x)", nmiss, nmiss_pct),
      "Missing (%)"   = f_str("xx (xx.x)", missing, missing_pct),
      "Mean +/- SD"= f_str("xx.x +/- xx.xx", mean,sd, empty='NA'),
      "Median"    = f_str("xx.x", median),
      "Q1; Q3"    = f_str("xx.x; xx.x", q1, q3, empty=c(.overall='NA')),
      "Min; Max"  = f_str("xx; xx", min, max)
    ) 
)
get_layer_templates()

# derive AGE2 for testing missing (%) in demo table

adsl <- tidyCDISC::adsl %>% 
  mutate(AGE2=if_else(AGE!=71,AGE, NA_real_)) 

# run table by using template

demo <- tplyr_table(adsl,TRT01AN) %>% 
  set_pop_data(adsl) %>% 
  add_total_group(group_name = "Total") %>% 
  add_layer(
    use_template("boa_demo_desc_template", AGE2) 
  ) %>% 
  add_layer(
    use_template("boa_demo_count_template", SEX,add_params = list(count_label="Sex"))
  ) %>% 
  build() %>% 
  arrange(ord_layer_index,ord_layer_1) %>% 
  select(-starts_with('ord'))

And below is are the result:
as you see the first row desc_label is one parameter I expect, by adding paramter like add_params = list(desc_label="Age") so I can obtain the table with summary label for continued variables

# A tibble: 11 × 5

   row_label1  var1_0           var1_54          var1_81          var1_Total      
   <chr>       <chr>            <chr>            <chr>            <chr>           
 1 **_desc_label_**  ""               ""               ""               ""              
 2 n (%)       "84 (97.7)"      "79 (94.0)"      "82 (97.6)"      "245 (96.5)"    
 3 Missing (%) " 2 ( 2.3)"      " 5 ( 6.0)"      " 2 ( 2.4)"      " 9 ( 3.5)"     
 4 Mean +/- SD "75.3 +/-  8.67" "76.0 +/-  8.46" "74.5 +/-  7.96" "75.2 +/-  8.36"
 5 Median      "76.5"           "78.0"           "76.5"           "77.0"          
 6 Q1; Q3      "69.0; 82.0"     "72.0; 82.0"     "70.5; 80.0"     "70.0; 81.0"    
 7 Min; Max    "52; 89"         "51; 88"         "56; 88"         "51; 89"        
 8 Sex         ""               ""               ""               ""              
 9 F           "53 (61.6)"      "50 (59.5)"      "40 (47.6)"      "143 (56.3)"    
10 M           "33 (38.4)"      "34 (40.5)"      "44 (52.4)"      "111 (43.7)"    
11 Missing     " 0 ( 0.0)"      " 0 ( 0.0)"      " 0 ( 0.0)"      " 0 ( 0.0)"     
> 

Versions

1.1.0

You can get this information from executing sessionInfo().

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:

[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Tplyr_1.1.0     tidyCDISC_0.2.1 dplyr_1.1.3    

loaded via a namespace (and not attached):
 [1] insight_0.19.5     RColorBrewer_1.1-3 httr_1.4.4         tools_4.2.1        tippy_0.1.0        bslib_0.4.1       
 [7] utf8_1.2.3         R6_2.5.1           sjlabelled_1.2.0   lazyeval_0.2.2     colorspace_2.0-3   withr_2.5.0       
[13] tidyselect_1.2.0   GGally_2.1.2       compiler_4.2.1     cli_3.4.1          gt_0.9.0           xml2_1.3.3        
[19] shinyjs_2.1.0      plotly_4.10.1      sass_0.4.6         scales_1.2.1       stringr_1.4.1      digest_0.6.30     
[25] rmarkdown_2.18     pkgconfig_2.0.3    htmltools_0.5.5    attempt_0.3.1      fastmap_1.1.0      htmlwidgets_1.6.2 
[31] rlang_1.1.1        rstudioapi_0.15.0  shiny_1.7.4        jquerylib_0.1.4    generics_0.1.3     jsonlite_1.8.7    
[37] config_0.3.2       magrittr_2.0.3     Matrix_1.5-3       Rcpp_1.0.9         munsell_0.5.0      fansi_1.0.4       
[43] lifecycle_1.0.3    stringi_1.7.8      IDEAFilter_0.1.2   yaml_2.3.6         plyr_1.8.8         shinyTime_1.0.3   
[49] grid_4.2.1         promises_1.2.0.1   forcats_1.0.0      crayon_1.5.2       lattice_0.20-45    haven_2.5.1       
[55] splines_4.2.1      hms_1.1.2          knitr_1.41         pillar_1.9.0       glue_1.6.2         cicerone_1.0.4    
[61] evaluate_0.18      golem_0.4.1        data.table_1.14.6  vctrs_0.6.3        httpuv_1.6.11      gtable_0.3.1      
[67] purrr_0.3.5        tidyr_1.2.1        reshape_0.8.9      assertthat_0.2.1   cachem_1.0.6       ggplot2_3.4.0     
[73] xfun_0.39          mime_0.12          xtable_1.8-4       later_1.3.0        ggcorrplot_0.1.4.1 survival_3.3-1    
[79] viridisLite_0.4.1  tibble_3.2.1       shinyWidgets_0.8.0 ellipsis_0.3.2     timevis_2.1.0   
@mstackhouse
Copy link
Contributor

@haodafa2019 I'm impressed by the nuance of the issues that you've been finding here.

Ok - for starters, in your first layer template the layer is formed correctly with the pass through parameters. In the second template ("boa_demo_desc_template"), the template would need to be build like this:

new_layer_template(
  "boa_demo_desc_template",
  group_desc(...) %>%
    set_custom_summaries(
      blank_=as.numeric(NA),
      nmiss=sum(!is.na(.var)),
      nmiss_pct=sum(!is.na(.var))/length(.var)*100,
      missing = sum(is.na(.var)),
      missing_pct= sum(is.na(.var))/length(.var)*100
    ) %>%
    set_format_strings(
      {desc_label} = f_str("xx", blank_),
      "n (%)" = f_str("xx (xx.x)", nmiss, nmiss_pct),
      "Missing (%)" = f_str("xx (xx.x)", missing, missing_pct),
      "Mean +/- SD"= f_str("xx.x +/- xx.xx", mean,sd, empty='NA'),
      "Median" = f_str("xx.x", median),
      "Q1; Q3" = f_str("xx.x; xx.x", q1, q3, empty=c(.overall='NA')),
      "Min; Max" = f_str("xx; xx", min, max)
    )
)

But - I see how you ended up where you did, because R doesn't like this syntax, and you'll get this error:

Error: unexpected '=' in:
"    set_format_strings(
      {desc_label} ="

So, basically layer templates can't do what you're trying to do here (right now), so I'll label this as a bug.

That said, I see what you're trying to do here - and honestly clever way to do it! Something we haven't supported very well are tables where you nest your row labels down into one column, and that's basically what you're trying to do with the "Age" label here. "Age" should just be submitted in your by variables like this:

use_template("boa_demo_desc_template", AGE2, by = "AGE")

But we don't offer an elegant way to do what you're trying to do. #129 shows the feature request issue of how I'd like to support this. Basically, I'd like to introduce a function that creates the blank row labelling "Age" like you want to do here, and that way you'd avoid trying to add the template parameter and custom function for blank_ that you're declaring in the layer template.

Thanks for the continued interaction and it's cool to see people trying to use some of the more advanced features of Tplyr!

@mstackhouse mstackhouse added the bug Something isn't working label Sep 29, 2023
@mstackhouse mstackhouse changed the title new_layer_template question Layer templates don't accept added parameters on left side of = within function Sep 29, 2023
@mstackhouse
Copy link
Contributor

mstackhouse commented Sep 29, 2023

This registers as a syntax error, so reprex::reprex() can't even be run on it. It looks like the parameter parsing will need to be updated figure out the left side of equals using syntax like `{var}` instead:

new_layer_template("test", 
  group_desc(...) %>%
    set_format_strings(
      `{my_param}` = f_str("xx", n)
    )
)


> get_layer_templates()
$test
Template name: test
Template parameters: None
Template code:
{
group_desc(...) %>% set_format_strings(`{my_param}` = f_str("xx", n))
} 

@haodafa2019
Copy link
Author

haodafa2019 commented Sep 30, 2023

@mstackhouse , thank you so much for the quick response.
Actually I did try using use_template("boa_demo_desc_template", AGE2,by="Age") , however in my table I expected the label nested in the summary column instead of 2 columns( row_label1 and row_label2).
So either label nested function or '{my_param}' in group_desc(...) fixed can be fine.
Look forward your help :)

# A tibble: 11 × 6
   row_label1 row_label2  var1_0           var1_54          var1_81          var1_Total      
   <chr>      <chr>       <chr>            <chr>            <chr>            <chr>           
 1 Age        desc_label  ""               ""               ""               ""              
 2 Age        n (%)       "84 (97.7)"      "79 (94.0)"      "82 (97.6)"      "245 (96.5)"    
 3 Age        Missing (%) " 2 ( 2.3)"      " 5 ( 6.0)"      " 2 ( 2.4)"      " 9 ( 3.5)"     
 4 Age        Mean +/- SD "75.3 +/-  8.67" "76.0 +/-  8.46" "74.5 +/-  7.96" "75.2 +/-  8.36"
 5 Age        Median      "76.5"           "78.0"           "76.5"           "77.0"          
 6 Age        Q1; Q3      "69.0; 82.0"     "72.0; 82.0"     "70.5; 80.0"     "70.0; 81.0"    
 7 Age        Min; Max    "52; 89"         "51; 88"         "56; 88"         "51; 89"        
 8 Sex        NA          ""               ""               ""               ""              
 9 F          NA          "53 (61.6)"      "50 (59.5)"      "40 (47.6)"      "143 (56.3)"    
10 M          NA          "33 (38.4)"      "34 (40.5)"      "44 (52.4)"      "111 (43.7)"    
11 Missing    NA          " 0 ( 0.0)"      " 0 ( 0.0)"      " 0 ( 0.0)"      " 0 ( 0.0)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants