john-harrold / onbrand

R package for creating templated reporting workflows in Word and PowerPoint
Other
24 stars 0 forks source link

Using custom R code for a Table/Figure legend #11

Closed mattfidler closed 4 months ago

mattfidler commented 2 years ago

Hi John,

I was wondering if you can use custom R code for a Table/Figure legend.

I some officer code that creates the table and figure numbers for internal reports using slip_in_seqfield() to support the required internal format for tables and figures.

I couldn't find the function used in this onbrand package and would like to include it as an option for my reports.

john-harrold commented 2 years ago

At a high level the formatting is handled by the Word template that's being used. If you have one you want to use, you can send it to me. Populate it with a couple dummy tables and figures and I can create a template for onbrand from that. This provides the basic font, weight, linespacing, etc.

For more detailed formatting you can use Markdown or ftext commands to control it. I'm going to add a caption_format option so you can specify which you want ("text", "md" or "ftext"). The ftext option is in a version that I'm going to push later.

john-harrold commented 2 years ago

This example should show each of those. It'll only work with the version here on GitHub.

library(onbrand)
library(ggplot2)
library(magrittr)
library(officer)

obnd = read_template(template = file.path(system.file(package="onbrand"), "templates", "report.docx"),
                      mapping = file.path(system.file(package="onbrand"), "templates", "report.yaml"))

p = ggplot() + annotate("text", x=0, y=0, label = "picture example")
imgfile = tempfile(pattern="image", fileext=".png")
ggsave(filename=imgfile, plot=p, height=5.15, width=9, units="in")

mdtext = "In Table <ref:ex_tab_text> the notes _and_ caption are
specified using *text* only. While in Figure <ref:ex_fig_md> we
can see an example of **Markdown** being used in a caption _and_
figure notes."

obnd = report_add_doc_content(obnd,
           type     = "text",
           content  = list(text   = mdtext,
                           format = "md"))

tmp_note= list(officer::ftext("This figure shows how to use ", prop=NULL),
               officer::ftext("ftext", prop=officer::fp_text(color="green")),
               officer::ftext(" in captions and notes.", prop=NULL))

tmp_cap = list(officer::ftext("Caption using ", prop=NULL),
              officer::ftext("ftext", prop=officer::fp_text(color="green")))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           key             = "ex_fig_ftext",
                           notes_format    = "ftext",
                           notes           = tmp_note,
                           caption_format  = "ftext",
                           caption         = tmp_cap))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           key             = "ex_fig_ftext",
                           notes_format    = "ftext",
                           notes           = tmp_note,
                           caption_format  = "ftext",
                           caption         = tmp_cap))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           notes_format    = "text",
                           key             = "ex_fig_text",
                           notes           = "This figure shows how to use text captions _and_ notes",
                           caption_format  = "text",
                           caption         = "Text caption"))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           notes_format    = "text",
                           key             = "ex_fig_text",
                           notes           = "This figure shows how to use text captions _and_ notes",
                           caption_format  = "text",
                           caption         = "Text caption"))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           notes_format    = "md",
                           key             = "ex_fig_md",
                           notes           = "This figure shows how to use markdown in captions _and_ notes",
                           caption_format  = "md",
                           caption         = "<shade:#33ff33>Markdown in a </shade><shade:#33ff33>*caption*.</shade>"))

obnd = report_add_doc_content(obnd,
           type     = "ggplot",
           content  = list(image           = p,
                           notes_format    = "md",
                           key             = "ex_fig_md",
                           notes           = "This figure shows how to use markdown in captions _and_ notes",
                           caption_format  = "md",
                           caption         = "<shade:#33ff33>Markdown in a </shade><shade:#33ff33>*caption*.</shade>"))

tdf =    data.frame(Parameters = c("Length", "Width", "Height"),
                    Values     = 1:3,
                    Units      = c("m", "m", "m") )
tab_fto = flextable::flextable(tdf) %>%
          flextable::width(width=1.5, unit="in")

obnd = report_add_doc_content(obnd,
           type     = "flextable_object",
           content  = list(ft=tab_fto,
                           notes_format    = "md",
                           key             = "ex_tab_md",
                           notes           = "This table shows how to use markdown in captions _and_ notes",
                           caption_format  = "md",
                           caption         = "<shade:#33ff33>Markdown in a </shade><shade:#33ff33>*caption*.</shade>"))

obnd = report_add_doc_content(obnd,
           type     = "flextable_object",
           content  = list(ft=tab_fto,
                           notes_format    = "md",
                           key             = "ex_tab_md",
                           notes           = "This table shows how to use markdown in captions _and_ notes",
                           caption_format  = "md",
                           caption         = "<shade:#33ff33>Markdown in a </shade><shade:#33ff33>*caption*.</shade>"))

obnd = report_add_doc_content(obnd,
           type     = "flextable_object",
           content  = list(ft=tab_fto,
                           notes_format    = "text",
                           key             = "ex_tab_text",
                           notes           = "This table shows how to use text in captions _and_ notes",
                           caption_format  = "text",
                           caption         = "This is a text caption"))

obnd = report_add_doc_content(obnd,
           type     = "flextable_object",
           content  = list(ft=tab_fto,
                           notes_format    = "text",
                           key             = "ex_tab_text",
                           notes           = "This table shows how to use text in captions _and_ notes",
                           caption_format  = "text",
                           caption         = "This is a text caption"))

save_report(obnd, "test_docx_captions.docx")
mattfidler commented 2 years ago

Ok. I can see some field codes are being exported in the word document. I can also see where you can add some markdown in the output.

I think I am missing something in creating the templates. Perhaps it will become more apparent if I send a blank template.

john-harrold commented 2 years ago

Have you looked through this vignette?

http://onbrand.ubiquity.tools/articles/Custom_Office_Templates.html

mattfidler commented 2 years ago

I did, but I could not figure out how to produce the nvs style yet.

On Mon, Oct 25, 2021, 11:21 PM John Harrold @.***> wrote:

Have you looked through this vignette?

http://onbrand.ubiquity.tools/articles/Custom_Office_Templates.html

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/john-harrold/onbrand/issues/11#issuecomment-951545593, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD5VWSC4Y3VITQC6PUYFNLUIYUELANCNFSM5GWPVOAQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

john-harrold commented 2 years ago

If you can send me a blank word document I'll see what I can do.

john-harrold commented 2 years ago

This push, https://github.com/john-harrold/onbrand/commit/28e94a0aa271fffd77453f087f84b98a7028d813 , should address this. It adds a couple options to the yaml file. First it allows you to define the sequence ID used for tables and figures. Second it allows you to specify R code to define the numbering. I've also updated the documentation to provide an example of how to create table and figure numbers based on the current section.

Lastly, to ensure that table and figure numbers reset within headings, I added an option fig_start_at and tab_start_at to this function:

http://onbrand.ubiquity.tools/reference/report_add_doc_content.html

You'll need to set those values to 1 when adding level 1 headings.

I'll send an example later.

mattfidler commented 2 years ago

Do you also have a post-processing hook?

(Of course I could do it myself, but I am just wondering if it can be contained in the yaml or something similar.

john-harrold commented 2 years ago

I'll reopen this to add the post-processing option as well.

mattfidler commented 2 years ago

Thanks John!

I will look at this soon (probably after ACoP)

john-harrold commented 2 years ago

Ok I added the ability to insert R code for post-processing into the yaml file. If you look through this vignette it should explain it.

http://onbrand.ubiquity.tools/articles/Custom_Office_Templates.html

I'd probably use the fetch and set functions to debug what you're trying to do. Once you have that working, then I'd take that code and stick it into the yaml file.

http://onbrand.ubiquity.tools/reference/fetch_officer_object.html http://onbrand.ubiquity.tools/reference/set_officer_object.html