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

Calling colformat_md() on header rows removes labels? #71

Closed
ddsjoberg opened this issue Apr 16, 2022 · 12 comments · Fixed by #72
Closed

Calling colformat_md() on header rows removes labels? #71

ddsjoberg opened this issue Apr 16, 2022 · 12 comments · Fixed by #72

Comments

@ddsjoberg
Copy link

ddsjoberg commented Apr 16, 2022

Hello! I recently came across your package, and it's awesome! Thank you for your dev work.

I maintain a package called gtsummary that creates summary tables that can be exported as a flextable. I use markdown syntax in the headers that is supported by other output types gt and huxtable. I would like to utilize your package to add support for the markdown syntax in the headers of the flextables too.

I've played around with the ftExtra::colformat_md(part = "header") function to try and use markdown in the header.

Is it possible to utilize the function in this way (aka on column header labels) to use markdown bold syntax in the header, or do I need to rename the underlying columns to use this function on the headers?

Thank you!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

tbl1 <-
  trial %>%
  select(age, trt) %>%
  tbl_summary(by = trt) %>%
  as_flex_table(strip_md_bold = FALSE) 

image

tbl2 <- ftExtra::colformat_md(tbl1, part = "header")

image

Created on 2022-04-16 by the reprex package (v2.0.1)

@atusy
Copy link
Owner

atusy commented Apr 17, 2022

Thank you for opening the issue.
This is indeed the limitation from the current implementation related to #61.

colformat_md needs be called before the content of data frame is modified by flextable because ftExtra::colformat_md considers tbl1$header$dataset as the content of header.
ftExtra::colformat_md should rather look at tbl1$header$content$content$data.

I will give a try to improve, but am not sure when.

@ddsjoberg
Copy link
Author

Thank you so much for the response!

In the meantime, I don't think it would be too much work on my end to use formatted column names instead of using flextable column header labels.

@ddsjoberg
Copy link
Author

Quick update: I began to make the update above (using the column names instead of using the column labels defined in felxtable). BUT, I realized this won't work with the spanning headers. Is there a way to make the additional headers use markdown syntax?

@atusy
Copy link
Owner

atusy commented Apr 17, 2022

Thanks for the update. The fix on #61 will also support the spanning headers.

I also have started working on this issue and #61 this morning, and I think I guess I can make things happen in this week.
Except for a few edge cases, my initial improvements already work (not yet pushed).

tibble::tibble(
  `x_**b**` = c("**bold**", "*italic*"),
  y = c("^superscript^", "~subscript~"),
  z = c("***~ft~^Extra^** is*", "*Cool*")
) %>%
  as_flextable() %>%
  span_header(sep="_") %>%
  colformat_md(part = "all")

image

@ddsjoberg
Copy link
Author

That is fantastic! Thank you!

@atusy
Copy link
Owner

atusy commented Apr 18, 2022

gtsummary also looks great!!

image

@ddsjoberg
Copy link
Author

Wow, it's beautiful! You're so good! 😍

@atusy
Copy link
Owner

atusy commented Apr 18, 2022

@ddsjoberg Would you try remotes::install_github("atusy/ftExtra#72")? This should solve the issue.

@ddsjoberg
Copy link
Author

@atusy ! This is amazing! I did some testing, and everything seems to work perfectly.

I think I'll implement in gtsummary with the following lines. If you have any suggestions, please do let me know!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'
packageVersion("ftExtra")
#> [1] '0.3.0.9999'

tbl <- 
  trial %>%
  select(marker, trt) %>%
  tbl_summary(by = trt) %>%
  modify_spanning_header(all_stat_cols() ~ "***Treatment***\nsecond line") %>%
  modify_footnote(label = "Testing *italic* and **bold**") %>%
  as_flex_table(strip_md_bold = FALSE) %>%
  ftExtra::colformat_md(part = "header", md_extensions = "+hard_line_breaks")

image

Created on 2022-04-18 by the reprex package (v2.0.1)

Thank you for the wonderful package! I am planning a gtsummary release in the next couple of weeks. Do you have an idea of when you'll push these updates to CRAN? I think it'll be great to include them!

@atusy
Copy link
Owner

atusy commented Apr 18, 2022

@ddsjoberg

Thank you for the quick reaction! I am happy to hear the nice result!

About the CRAN release, I will plan in this week.
Let me wait a couple of days for the response from @joelnitta on #61.
If he is silent, I will release anyway.

@joelnitta
Copy link

Thanks for the fix! I will try this tomorrow.

@atusy
Copy link
Owner

atusy commented Apr 18, 2022

Thanks!

@atusy atusy closed this as completed in #72 Apr 19, 2022
atusy added a commit that referenced this issue Apr 19, 2022
Solves #61, #71

* apply colformat_md on content rather than dataset

* update NEWS

* update documents

* build documents
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

Successfully merging a pull request may close this issue.

3 participants