FredHutch / VISCtemplates

Tools for writing reproducible reports at VISC
Other
6 stars 2 forks source link

Should we save tables in reports as separate files (similar to what is done for figures)? #122

Closed kelliemac closed 1 month ago

kelliemac commented 10 months ago

Should we save tables in reports as separate files (similar to what is done for figures)? Maybe in csv/xlsx format? This can be helpful for sharing results with collaborators.

I am not sure if this is a good idea or not in general, so let's discuss. Does anyone else get asked to share individual tables like this, so that collaborators can annotate them as they want to?

mayerbry commented 7 months ago

I didn't see this before. I had to do this for a lot of tables in one project, so I developed this ad hoc procedure:

# top-level functions
save_table = function(df, fname){
  write_csv(df, paste0("local-dir-tables/", fname, ".csv"))
}
save_caption = function(cap, fname){
  cat(cap, file = paste0("local-dir-tables/", fname, "-cap.txt"))
}
# later in report, in actual table chunk

caption <- "caption for my table."
save_caption(caption, "name-of-table")

tbl_data %>%
  save_table("name-of-table") %>%
  table(
    format = output_type,
    digits = 2,
    booktabs = TRUE,
    caption = caption,
   ...
)
kelliemac commented 7 months ago

Thanks Bryan, this is great! I wasn't sure how useful this feature would be, but you've provided one good use case. Do we think other people need to do this regularly enough that it's worth including a version of this function in VISCtemplates?

I also wonder if there's a way to save the caption in the same file as the table. Maybe a hacky way to do it would be to write the caption as the first or last line in the csv.

mayerbry commented 6 months ago

The complete use case was compiling word doc ready tables for collaborators for a manuscript. This was the full workflow, but I would love a more efficient way to do this:

  1. save tables + captions within each report subdirectory (as per last comment)
  2. within a manuscript subdirectory had an Rmd that made one html document with every saved table compiled together
    • The Rmd re-used code from the reports to generate publication ready looking tables
  3. copy and pasted the full html into a word doc
    • There is a quick manual step within the word doc to make tables look rendered correctly

This was a bit cumbersome in some ways, (and there is an obvious violation of DRY with code re-use to actually re-generate the tables), but at least I was sure the final tables shared the same source data as the reports.

Note: Rmd->doc couldn't re-generate the tables as they needed to be for the publication. I.e., these word tables looked terrible and required a lot of manual formatting in word to fix them. If there is a good kable equivalent for making word tables, that might be simpler and resolve your question re. saving tables and captions together.

source: https://github.com/FredHutch/ID50ZavalaAnalysis/tree/main/manuscript

kelliemac commented 6 months ago

It looks like the flextable package might help with saving nicely formatted tables directly to a Word doc (source: https://community.rstudio.com/t/nice-tables-when-knitting-to-word/3840). We could consider writing a VISCtemplates function to render tables that uses kable + kableExtra for pdf/html and flextable for word outputs. Maybe this is more useful than saving tables to csv.

The issue of code re-use and table/figure re-generation for manuscripts is I think a slightly separate, and important, one. I don't have a good solution to this, and I don't have a good sense of how universal the manuscript preparation process is across different projects, since I haven't been involved in very many.

kelliemac commented 3 months ago

Current thinking on this issue: we should just improve the table formatting in the docx report output, and then it's easy for people to pull desired tables from there. See https://github.com/FredHutch/VISCtemplates/issues/120

kelliemac commented 1 month ago

closing this issue as it is covered by others regarding improving table formatting in word docs