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

Retaining leading space in as_paragraph_md() #88

Closed
jwe4ec opened this issue Mar 12, 2023 · 3 comments
Closed

Retaining leading space in as_paragraph_md() #88

jwe4ec opened this issue Mar 12, 2023 · 3 comments

Comments

@jwe4ec
Copy link

jwe4ec commented Mar 12, 2023

Thank you for this wonderful package!

It seems that as_paragraph_md() automatically strips leading space. See below for comparison with as_paragraph() from flextable.

> as_paragraph(" text")[[1]]$txt
[1] " text"
> as_paragraph_md(" text")[[1]]$txt
[1] "text"

Is there a way to retain leading space when using as_paragraph_md()? Maybe an argument for this option can be added?

This would be useful when a space is desired between the footnote symbol and the footnote text in the footer of a flextable. I am trying to do this in my code below, but no space is displayed between the footnote symbol and footnote text.

Sample Code

# Define function to format dropout summary table

format_summ_tbl_drp <- function(summ_tbl, gen_note, footnotes, title) {
  target_cols <- c("analysis_sample", "a_contrast", "param", "mean", "emp_sd", 
                   "avg_sd", "pctl_bs_ci", "hpd_ci")
  left_align_body_cols <- c("analysis_sample", "a_contrast", "param")
  merge_v_cols         <- c("analysis_sample", "a_contrast")
  cols_to_bold         <- c("pctl_bs_ci", "hpd_ci")

  summ_tbl_ft <- flextable(data = summ_tbl[, target_cols]) |>
    set_table_properties(align = "left") |>
    
    set_caption(as_paragraph(as_i(title)),
                fp_p = fp_par(padding.left = 0, padding.right = 0),
                align_with_table = FALSE) |>
    
    align(align = "center", part = "header") |>
    align(align = "center", part = "body") |>
    align(j = left_align_body_cols, align = "left", part = "body") |>
    align(align = "left", part = "footer") |>
    
    merge_v(j = merge_v_cols, part = "body") |>
    valign(j = merge_v_cols, valign = "top", part = "body") |>
    fix_border_issues(part = "body") |>
    
    bold(which(summ_tbl$sig == 1), cols_to_bold) |>
    
    set_header_labels(analysis_sample            = "Sample",
                      a_contrast                 = "Contrast",
                      param                      = "Estimand",
                      pctl_bs_ci                 = "95% PB CI",
                      hpd_ci                     = "95% HPD CI") |>
    compose(j = "mean", part = "header",
            value = as_paragraph("Emp. ", as_i("M"))) |>
    compose(j = "emp_sd", part = "header",
            value = as_paragraph("Emp. ", as_i("SD"))) |>
    compose(j = "avg_sd", part = "header",
            value = as_paragraph("Avg. ", as_i("SD"))) |>
    
    labelizor(part = "body",
              labels = c("c1_corr_itt_2000"      = "ITT",
                         "c2_4_class_meas_compl" = "CMC",
                         "a1"                    = "CBM-I vs. Psychoeducation",
                         "a2_1"                  = "CBM-I HR Coaching vs. No Coaching",
                         "a2_2"                  = "CBM-I HR Coaching vs. CBM-I LR",
                         "a2_3"                  = "CBM-I HR No Coaching vs. CBM-I LR",
                         "para[3]"               = "Zero-inflation odds ratio",
                         "para[1]"               = "Count model difference")) |>
    
    add_footer_lines(gen_note) |>
    
    footnote(i = c(1, 3), j = 1,
             value = as_paragraph_md(c(footnotes$ITT_a, footnotes$CMC_b)),
             ref_symbols = c(" a", " b"),
             part = "body") |>
  
    autofit()
  
  return(summ_tbl_ft)
}

# Define notes (note: as_paragraph_md() seems to strip text of leading
# space; thus, the footnote values lack the leading space)

footnotes <- list(ITT_a = " For the ITT model, results were pooled across bootstrap samples in which all parameters converged, and the posterior disribution in each bootstrap sample was based on 10,000 MCMC sampling iterations (after 10,000 burn-in iterations). Emp. *M* = *M* of empirical *M*s across bootstrap samples; Emp. *SD* = *SD* of empirical *M*s across bootstrap samples; Avg. *SD* = *M* of empirical *SD*s across bootstrap samples; PB CI = percentile bootstrap confidence interval.",
                  CMC_b = " For the CMC models, the posterior distribution was based on 500,000 MCMC sampling iterations (after 500,000 burn-in iterations). Emp. *M* = empirical *M*; Emp. *SD* = empirical *SD*; HPD CI = Highest Posterior Density Credible Interval.")

gen_note <- as_paragraph_md("*Note.* Significant differences between contrast levels are in boldface. Separate models were fit for each contrast. The latter level of the contrast is the reference group. Only estimands of interest are shown (for all model parameters and convergence diagnostics, see Supplement B). CBM-I = cognitive bias modification for interpretation; TX = treatment; FU = follow-up; HR = High Risk; LR = Low Risk.")

# Run function

summ_tbl_drp_ft <- format_summ_tbl_drp(summ_tbl_drp, gen_note, footnotes,
  "Stages 1-2 Dropout Results for Intent-To-Treat (ITT) and Classification Measure Completer (CMC) Samples")

Sample Table

The code above yields this table. You can see that there is no space in the footer between "a" and "For the ITT model..." or between "b" and "For the CMC models..."

image

@atusy
Copy link
Owner

atusy commented Mar 13, 2023

Thank you for using ftExtra.
For the next time, would you keep in mind to minimize the example?

Anyway, ftExtra uses Pandoc's Markdown, so you can literally use spaces by escaping it with backslashes.
Read Pandoc's manual for the detail: pandoc.org/MANUAL.html

library(ftExtra)
data.frame(
  package = "ftExtra",
  description = "Extensions for 'Flextable'^[\\ Supports of footnotes]",
  stringsAsFactors = FALSE
) %>%
  as_flextable() %>%
  colformat_md(
    .footnote_options = ftExtra::footnote_options()
  ) %>%
  flextable::autofit(add_w = 0.5)

BTW, your suggestion is absolutely true that providing option is better.
There is .footnote_options argument for that, however, it seems not to be working as expected...
I'll take a look.

image

@jwe4ec
Copy link
Author

jwe4ec commented Mar 13, 2023

Thank you so much, and sorry for the long example!

Escaping the leading space with \\ worked!

footnote(i = c(1, 3), j = 1,
             value = as_paragraph_md(c(footnotes$ITT_a, footnotes$CMC_b)),
             ref_symbols = c(" a", " b"),
             part = "body")

# Revised code escaping leading space

footnotes <- list(ITT_a = "\\ For the ITT model, results were pooled across bootstrap samples in which all parameters converged, and the posterior disribution in each bootstrap sample was based on 10,000 MCMC sampling iterations (after 10,000 burn-in iterations). Emp. *M* = *M* of empirical *M*s across bootstrap samples; Emp. *SD* = *SD* of empirical *M*s across bootstrap samples; Avg. *SD* = *M* of empirical *SD*s across bootstrap samples; PB CI = percentile bootstrap confidence interval.",
                  CMC_b = "\\ For the CMC models, the posterior distribution was based on 500,000 MCMC sampling iterations (after 500,000 burn-in iterations). Emp. *M* = empirical *M*; Emp. *SD* = empirical *SD*; HPD CI = Highest Posterior Density Credible Interval.")

image

@atusy atusy closed this as completed May 30, 2023
@atusy
Copy link
Owner

atusy commented May 30, 2023

Thank you for the response.
Let me close this issue as solved.
If you encounter any problems let me know again!!

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