# Accessing Figures and Tables

# Introduction

When you create the `report_fit.yaml` file you have defined figures and tables for reporting. However you may want to reuse these report objects outside of Word and PowerPoint. For example you may want to use them inside of Shiny Apps or in RMarkdown document. This vignette outlines how you can build the objects outside of the `nlmixr2rpt` workflow.

# Creating figures and tables

First we need to load the appropriate libraries. We need the `onbrand` library because the tables and figures are generated based on the type of report. Specifically default figure dimensions will change based on the report type. Here we initialize an empty Word report with onbrand. The report type is extracted from this object in the functions below.

    library(nlmixr2rpt)
    library(onbrand)

    obnd = read_template(
      template = system.file(package="nlmixr2rpt", "templates","nlmixr_obnd_template.docx"),
      mapping  = system.file(package="nlmixr2rpt", "templates","nlmixr_obnd_template.yaml"))

You should have an `nlmixr2` fit object from your own analysis. We will load an example from this package.

    fit = fetch_fit_example()

Next we read in the yaml file using `yaml_read_fit()`. Below we are using a stripped down example for testing. The contents of the yaml file are stored in the `rptdetails` list element that is returned.

    rptdetails = yaml_read_fit(
      obnd    = obnd,
      rptyaml = system.file(package="nlmixr2rpt", "examples", "report_fit_test.yaml"),
      fit     = fit)$rptdetails

Now we can use the `rptdetails` object we just created to create the figures and tables using the `build_figures()` and `build_tables()` functions respectively.

    bfres = build_figures(obnd       = obnd,
                          fit        = fit,
                          rptdetails = rptdetails)

    btres = build_tables(obnd        = obnd,
                          fit        = fit,
                          rptdetails = rptdetails)

# Using report objects in RMarkdown

You can look at the help for the two build functions above. Each of the objects they create should contain all of the information about figures and tables defined in the `report_fit.yaml` file. To access a figure you just need to specify the figure ID. For example the path and title of the figure containing the `dv_vs_pred` figure can be found here:

    fig_path  = bfres$rptfigs$dv_vs_pred$figure[[1]]
    fig_title = bfres$rptfigs$dv_vs_pred$title_proc

And you can include them in RMarkdown using the following:

    ![`r fig_title`](`r fig_path`){width=80%}

dv_vs_pred title

Tables are used in a similiar fashion. This code will extract the flextable object and title for the `pest_table`. Then it attaches the title as a caption and displays it below.

    tab_ft    = btres$rpttabs$pest_table$table$ft[[1]]
    tab_title = btres$rpttabs$pest_table$title_proc
    tab_ft_knit = flextable::set_caption(tab_ft, caption = tab_title)

    tab_ft_knit

Parameter Estimates
Parameter | Est. | SE | %RSE | Back-transformed(95%CI) | BSV(CV%) | Shrink(SD)%
--- | --- | --- | --- | --- | --- | ---
Log Ka | 0.476 | 0.203 | 42.7 | 1.61 (1.08, 2.39) | 69.8 | 1.23%<
Log Cl | 1.02 | 0.0754 | 7.42 | 2.76 (2.38, 3.2) | 26.1 | 2.36%<
Log V | 3.46 | 0.0632 | 1.83 | 31.8 (28.1, 36) | 13.8 | 9.71%<
add.sd | 0.696 |  |  | 0.696 |  |