Skip to contents

Make a flextable with column names from another dataframe

Usage

make_ft(table, header)

Arguments

table

A dataframe with the contents of the desired flextable output.

header

Another dataframe with three columns:

  • First column contains what the top header row should be. In our template, this is the abbreviation of the measurement (i.e. Organic Matter).

  • Second column, called "key", contains the join key. In our template, this is the same as the first column.

  • Third column contains the second header row. In our template, this is the unit (i.e. %).

Value

Formatted flextable object.

Examples

# Read in wrangled table data
headers_path <- soils_example("headers.RDS")
headers <- readRDS(headers_path)

tables_path <- soils_example("tables.RDS")
tables <- readRDS(tables_path)

# Input dataframes
headers$chemical
#>               abbr              key     unit
#> 1               pH               pH         
#> 2               EC               EC mmhos/cm
#> 3              CEC              CEC cmolc/kg
#> 4          Total C          Total C        %
#> 5              TOC              TOC        %
#> 6      Inorganic C      Inorganic C        %
#> 7 Field or Average Field or Average         

tables$chemical
#> # A tibble: 6 × 7
#>   `Field or Average`                pH    EC   CEC `Total C`   TOC `Inorganic C`
#>   <glue>                         <dbl> <dbl> <dbl>     <dbl> <dbl>         <dbl>
#> 1 Field 01                         6.7  0.42   7.8       1.8   1.8         NA   
#> 2 Field 03                         7.6  0.6   10         1.6   1.5          0.12
#> 3 Hay/Silage Average 
#> (14 Fields)   6.1  0.43  15         2.4   2.4         NA   
#> 4 Pasture, Seeded Average 
#> (16 F…   6.2  0.33  14         2.7   2.7          0.11
#> 5 County 9 Average 
#> (5 Fields)      7.1  0.48   8.7       1.7   1.6          0.11
#> 6 Project Average 
#> (100 Fields)     6.1  0.74  15         2.9   2.9          0.19

# Make the flextable
make_ft(
  table = tables$chemical,
  header = headers$chemical
) |>
  # Style the flextable
  style_ft() |>
  # Add the white line under the columns with the same units
  unit_hline(header = headers$chemical)

Field or Average

pH

EC

CEC

Total C

TOC

Inorganic C

mmhos/cm

cmolc/kg

%

Field 01

6.7

0.42

7.8

1.8

1.8

Field 03

7.6

0.60

10.0

1.6

1.5

0.12

Hay/Silage Average
(14 Fields)

6.1

0.43

15.0

2.4

2.4

Pasture, Seeded Average
(16 Fields)

6.2

0.33

14.0

2.7

2.7

0.11

County 9 Average
(5 Fields)

7.1

0.48

8.7

1.7

1.6

0.11

Project Average
(100 Fields)

6.1

0.74

15.0

2.9

2.9

0.19