Skip to contents
library(reportifyr)
#> ── Needed reportifyr options ───────────────────────────────────────────────────
#>  options('venv_dir') is not set. venv will be created in Project root
#>  Please set all options for package to work.
#> ── Optional version options ────────────────────────────────────────────────────
#>  options('uv.version') is not set. Default is 0.5.1
#>  options('python.version') is not set. Default is system version
#>  options('python-docx.version') is not set. Default is 1.1.2
#>  options('pyyaml.version') is not set. Default is 6.0.2

options("venv_dir" = file.path(here::here(), "vignettes"))
initialize_report_project(file.path(here::here(), "vignettes"))
#> Creating python virtual environment with the following settings:
#>      venv_dir: /home/runner/work/reportifyr/reportifyr/vignettes
#>  python-docx.version: 1.1.2
#>  pyyaml.version: 6.0.2
#>  uv.version: 0.5.1
#>  python.version: 3.10.12
#> Copied standard_footnotes.yaml into /home/runner/work/reportifyr/reportifyr/vignettes/report

There are certain situations where footnotes might not be desired on all figures/tables or even out throughout the entire document. reportifyr supports this behavior through arguments to build_report. First, we’ll build a report that lacks footnotes throughout the entire document. To do this we can set the add_footnotes argument to FALSE instead of the default TRUE.

# Specify input and output .docx files
docx_shell <- file.path(here::here(), "vignettes", "report", "shell", "template.docx")
docx_out <- file.path(here::here(), "vignettes", "report", "draft", "draft.docx")

# Specify paths to tables and figures directories and the standard_footnotes.yaml
tables_path <- file.path(here::here(), "vignettes", "OUTPUTS", "tables")
figures_path <- file.path(here::here(), "vignettes", "OUTPUTS", "figures")
footnotes <- file.path(here::here(), "vignettes", "report", "standard_footnotes.yaml")

build_report(docx_in = docx_shell, 
             docx_out = docx_out,
             figures_path = figures_path,
             tables_path = tables_path,
             standard_footnotes_yaml = footnotes,
             add_footnotes = FALSE)
#> 1.581 sec elapsed
#> 0.203 sec elapsed

This produces a report without footnotes in the entire document. fig2 no fnfig3 no fntab1 no fntab2 no fn

If we instead want to include an appendix figure and table without footnotes, but want footnotes within the rest of the report we can use the footnotes_fail_on_missing_metadata argument to achieve this. Because footnotes are stored in the metadata a figure/table with no metadata would usually throw an error, with footnotes_fail_on_missing_metadata = FALSE we instead get a warning in the log messages and the figures/tables footnote addition is skipped. One caveat to this is that table formatting is stored in the metadata - table1_format is an additional argument to the format_flextable function that converts RDS/csv data into flextables for addition to the word document. The table that you don’t want footnotes for won’t have a metadata file. We’ve handled this by using default formatting for csv files and if the table is saved as a flextable in an RDS file we do not alter the formatting. If the RDS file is not a flextable we format_flextable with default arguments.

library(ggplot2)
library(dplyr)
set.seed(123)

n <- 300
DV <- runif(n, min = 5, max = 100)
IPRED <- pmax(0, DV + rnorm(n, mean = 0, sd = 40 / sqrt(DV)))


gof_plot <- ggplot(data = data.frame(IPRED, DV), aes(x = DV, y = IPRED)) +
  geom_point(color = "blue", alpha = 0.6) +
  geom_abline(slope = 1, intercept = 0, color = "red", linetype = "dashed") +
  labs(
    title = "Goodness-of-Fit Plot",
    x = "Observed Data (DV)",
    y = "Individual Predictions (IPRED)"
  ) +
  theme_minimal()

gof_fig_name <- "theoph-pk-gof.png" 
ggsave(filename = file.path(here::here(), "vignettes", "OUTPUTS", "figures", gof_fig_name), 
       plot = gof_plot,
       width = 6,
       height = 4.5)

subjects <- 15
pk_demo <- data.frame(
  ID = sprintf("S%03d", 1:subjects),
  Age = sample(18:80, subjects, replace = TRUE),
  Sex = sample(c("M", "F"), subjects, replace = TRUE),
  Weight = round(rnorm(subjects, mean = 70, sd = 15), 1),      
  Height = round(rnorm(subjects, mean = 170, sd = 10), 1),     
  BMI = round(rnorm(subjects, mean = 25, sd = 4), 1),           
  Smoking_Status = sample(c("Non-smoker", "Smoker", "Ex-smoker"), subjects, replace = TRUE),
  Renal_Function = sample(c("Normal", "Mild impairment", "Moderate impairment", "Severe impairment"), subjects, replace = TRUE),
  Hepatic_Function = sample(c("Normal", "Mild impairment", "Severe impairment"), subjects, replace = TRUE)
)

table_file <- "theoph-pk-demographics.csv"
utils::write.csv(pk_demo, 
                 file = file.path(here::here(), "vignettes", "OUTPUTS", "tables", table_file),
                 row.names = FALSE)

We will now use the template-ef.docx file which contains a figure and table in the appendicies that we wish to not have footnotes. These files were generated without metadata and could be non reportifyr generated content.

result <- file.copy(from = system.file("extdata/template-ef.docx", package = "reportifyr"),
                    to = file.path(here::here(), "vignettes", "report", "shell", "template-ef.docx"))

fig4 no fn templatetab4 no fn template

# Specify input and output .docx files
docx_shell <- file.path(here::here(), "vignettes", "report", "shell", "template-ef.docx")
docx_out <- file.path(here::here(), "vignettes", "report", "draft", "draft-ef.docx")

# Specify paths to tables and figures directories and the standard_footnotes.yaml
tables_path <- file.path(here::here(), "vignettes", "OUTPUTS", "tables")
figures_path <- file.path(here::here(), "vignettes", "OUTPUTS", "figures")
footnotes <- file.path(here::here(), "vignettes", "report", "standard_footnotes.yaml")

build_report(docx_in = docx_shell, 
             docx_out = docx_out,
             figures_path = figures_path,
             tables_path = tables_path,
             standard_footnotes_yaml = footnotes,
             add_footnotes = TRUE,
             footnotes_fail_on_missing_metadata = FALSE
             )
#> 2024-11-19 16:08:17 [WARN] Metadata file missing for table: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics.csv
#> 2024-11-19 16:08:17 [WARN] Default formatting will be applied for /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics.csv.
#> 1.92 sec elapsed
#> 0.22 sec elapsed
#> 2024-11-19 16:08:19 [WARN] Figure footnotes script stderr: Metadata file not found: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/figures/theoph-pk-gof_png_metadata.json
#> 
#> 2024-11-19 16:08:19 [WARN] Table footnotes script stderr: Metadata file not found: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics_csv_metadata.json
#> 
#> 0.517 sec elapsed

We can see in the logs that theoph-pk-demographics.csv was missing metadata and that default formatting was applied. We additionally see warnings about missing metadata files.

Additionally, the source path in the footnote points to the R script or quarto document used to create that table or figure. If you would like the path to the object itself you can set the include_object_path argument to TRUE to get an additional footnote with object path.

# Specify input and output .docx files
docx_shell <- file.path(here::here(), "vignettes", "report", "shell", "template-ef.docx")
docx_out <- file.path(here::here(), "vignettes", "report", "draft", "draft-ef-op.docx")

# Specify paths to tables and figures directories and the standard_footnotes.yaml
tables_path <- file.path(here::here(), "vignettes", "OUTPUTS", "tables")
figures_path <- file.path(here::here(), "vignettes", "OUTPUTS", "figures")
footnotes <- file.path(here::here(), "vignettes", "report", "standard_footnotes.yaml")

build_report(docx_in = docx_shell, 
             docx_out = docx_out,
             figures_path = figures_path,
             tables_path = tables_path,
             standard_footnotes_yaml = footnotes,
             add_footnotes = TRUE,
             footnotes_fail_on_missing_metadata = FALSE,
             include_object_path = TRUE
             )
#> 2024-11-19 16:08:21 [WARN] Metadata file missing for table: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics.csv
#> 2024-11-19 16:08:21 [WARN] Default formatting will be applied for /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics.csv.
#> 1.939 sec elapsed
#> 0.221 sec elapsed
#> 2024-11-19 16:08:22 [WARN] Figure footnotes script stderr: Metadata file not found: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/figures/theoph-pk-gof_png_metadata.json
#> 
#> 2024-11-19 16:08:22 [WARN] Table footnotes script stderr: Metadata file not found: /home/runner/work/reportifyr/reportifyr/vignettes/OUTPUTS/tables/theoph-pk-demographics_csv_metadata.json
#> 
#> 0.496 sec elapsed