Open haodafa2019 opened 11 months ago
@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!
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))
}
@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)"
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" = fstr("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
# demo description
# derive AGE2 for testing missing (%) in demo table
# run table by using template
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
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: