Less ugly tables

Tables tend to look fine in html, but when rendering to word they completely fall down. Below, I test a bunch of the standard packages. *The one that seems to work best is [flextable::flecxtable()], which is interesting, since [huxtable::huxtable()] claims to build on it but doesn’t work well at all.

NOTE: this doc probably looks fine on the website. The issue is word rendering.

I’ll test two tables- iris, which has narrow columns, and a table with lots of text, which tends to cause the most problems.

texttab <- readr::read_csv(file.path('data/component_table.csv'), 
                           show_col_types = FALSE) |> 
  dplyr::select(-tidyselect::last_col())

The solution

Maybe. This exact code works great in another project, and not at all here. I have no idea what’s going on.

library(flextable)
Warning: package 'flextable' was built under R version 4.4.2
set_flextable_defaults(font.family = 'Calibri',
                       font.size = 10,
                       table.layout = 'autofit')

Use flextable with set_table_properties(layout = 'autofit') .

head(iris) |> 
  flextable()
Table 1: This is a table with flextable

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

4.7

3.2

1.3

0.2

setosa

4.6

3.1

1.5

0.2

setosa

5.0

3.6

1.4

0.2

setosa

5.4

3.9

1.7

0.4

setosa

texttab |> 
  flextable() #|> 
  # set_table_properties(layout = "autofit")
Table 2: This is a table with lots of text in flextable

General HydroBOT components

Type

General component definitions

Specific components used in our example

Input data

Not part of HydroBOT

Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models. 

Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)

Controller

Workflow

Interface between input data, response model, and other toolkit components. Sets up run(s).

Sets up links to data and parameters for EWR tool and aggregations.

Response models

External, integrated

A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers.

EWR tool

Aggregator

Workflow

Aggregates response model results to scales across the dimensions of time, space, and theme. 

Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).

Comparer

Workflow

Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables.

Comparison of environmental values at various theme scales for the example climate and adaptation scenarios

Causal networks

External, integrated

Describe causal relationships between values.

Long Term Water Plan (LTWP)

Spatial data

External, integrated

Describe spatial relationships

Gauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin

Note that there is an issue still if the table prints into a div. I think because divs render in word as tables, and so flextable struggles with what it controls. I’ll show that below.

The others

These all look fine in html, most things do. But the word doc is where things seem to fall down. Especially if they have lots of text.

head(iris) |> 
  knitr::kable()
Table 3: This is a table with kable
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
Table 4: This is a table with kableExtra. It does not render to word. It used to with prefer-html: true, but that workarounds seems not to work anymore.
# head(iris) |> 
#   knitr::kable() |> 
#   kableExtra::kable_styling()
head(iris) |> 
  huxtable::huxtable() |> 
  huxtable::theme_article()
Table 5: This is a table with huxtable
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93  1.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5  3.61.40.2setosa
5.43.91.70.4setosa
head(iris) |> 
 gt::gt()
Table 6: This is a table with gt
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Long text

This table is giving me fits elsewhere, let’s try it here. It has a common issue of wrapping words.

Table 7: This is just printed
texttab
# A tibble: 7 × 4
  General HydroBOT compone…¹ Type  General component de…² Specific components …³
  <chr>                      <chr> <chr>                  <chr>                 
1 Input data                 Not … Hydrologic data (time… Modified historical h…
2 Controller                 Work… Interface between inp… Sets up links to data…
3 Response models            Exte… A model of the respon… EWR tool              
4 Aggregator                 Work… Aggregates response m… Response model sets t…
5 Comparer                   Work… Compares scenarios (t… Comparison of environ…
6 Causal networks            Exte… Describe causal relat… Long Term Water Plan …
7 Spatial data               Exte… Describe spatial rela… Gauge locations, Sust…
# ℹ abbreviated names: ¹​`General HydroBOT components`,
#   ²​`General component definitions`,
#   ³​`Specific components used in our example`
texttab |> 
  knitr::kable()
Table 8: This is a table with kable
General HydroBOT components General component definitions Specific components used in our example Details of Specific components
Input data Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models.  Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios) Daily flow rates for 45 for 15 scenarios
Controller Interface between input data, response model, and other toolkit components. Sets up run(s). Sets up links to data and parameters for EWR tool and aggregations. NA
Response models A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers. EWR tool The EWR tool holds databases of the EWRs required to meet the environmental objectives of the basin, which protect or enhance environmental assets that are valued based on ecological significance.
Aggregator Aggregates response model results to scales across the dimensions of time, space, and theme.  Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time). The spatial dimension consists of gauges nested within planning units within the basin. The time dimension assessess EWR performance averaged over the yearly data returned by the EWR tool. The value dimension consists of multiple EWRs (hydrologic indicators) that apply to environmental values (many-to-many), at multiple levels in the causal network (e.g. life-cycle components, species, groups of species, or long-term planning targets).
Comparer Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables. Comparison of environmental values at various theme scales for the example climate and adaptation scenarios Explored in detail in figures and tables
Causal networks Describe causal relationships between values. Long Term Water Plan (LTWP) Provided by HydroBOT. LTWP required of Basin States by the Murray-Darling Basin Plan and give effect to the Basin-wide Environmental Watering Strategy. 
texttab |> 
  huxtable::huxtable() |> 
  huxtable::theme_article()
Table 9: This is a table with huxtable
General HydroBOT componentsGeneral component definitionsSpecific components used in our exampleDetails of Specific components
Input dataHydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models. Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)Daily flow rates for 45 for 15 scenarios
ControllerInterface between input data, response model, and other toolkit components. Sets up run(s).Sets up links to data and parameters for EWR tool and aggregations.
Response modelsA model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers.EWR toolThe EWR tool holds databases of the EWRs required to meet the environmental objectives of the basin, which protect or enhance environmental assets that are valued based on ecological significance.
AggregatorAggregates response model results to scales across the dimensions of time, space, and theme. Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).The spatial dimension consists of gauges nested within planning units within the basin. The time dimension assessess EWR performance averaged over the yearly data returned by the EWR tool. The value dimension consists of multiple EWRs (hydrologic indicators) that apply to environmental values (many-to-many), at multiple levels in the causal network (e.g. life-cycle components, species, groups of species, or long-term planning targets).
ComparerCompares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables.Comparison of environmental values at various theme scales for the example climate and adaptation scenariosExplored in detail in figures and tables
Causal networksDescribe causal relationships between values.Long Term Water Plan (LTWP)Provided by HydroBOT. LTWP required of Basin States by the Murray-Darling Basin Plan and give effect to the Basin-wide Environmental Watering Strategy. 
texttab |> 
 gt::gt()
Table 10: This is a table with gt
General HydroBOT components General component definitions Specific components used in our example Details of Specific components
Input data Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models.  Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios) Daily flow rates for 45 for 15 scenarios
Controller Interface between input data, response model, and other toolkit components. Sets up run(s). Sets up links to data and parameters for EWR tool and aggregations. NA
Response models A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers. EWR tool The EWR tool holds databases of the EWRs required to meet the environmental objectives of the basin, which protect or enhance environmental assets that are valued based on ecological significance.
Aggregator Aggregates response model results to scales across the dimensions of time, space, and theme.  Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time). The spatial dimension consists of gauges nested within planning units within the basin. The time dimension assessess EWR performance averaged over the yearly data returned by the EWR tool. The value dimension consists of multiple EWRs (hydrologic indicators) that apply to environmental values (many-to-many), at multiple levels in the causal network (e.g. life-cycle components, species, groups of species, or long-term planning targets).
Comparer Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables. Comparison of environmental values at various theme scales for the example climate and adaptation scenarios Explored in detail in figures and tables
Causal networks Describe causal relationships between values. Long Term Water Plan (LTWP) Provided by HydroBOT. LTWP required of Basin States by the Murray-Darling Basin Plan and give effect to the Basin-wide Environmental Watering Strategy. 

gt seems to really have a lot of issues here. What if I use divs?

Table 11: This is a table with gt in a div.
texttab |> 
 gt::gt()
General HydroBOT components Type General component definitions Specific components used in our example
Input data Not part of HydroBOT Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models.  Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)
Controller Workflow Interface between input data, response model, and other toolkit components. Sets up run(s). Sets up links to data and parameters for EWR tool and aggregations.
Response models External, integrated A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers. EWR tool
Aggregator Workflow Aggregates response model results to scales across the dimensions of time, space, and theme.  Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).
Comparer Workflow Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables. Comparison of environmental values at various theme scales for the example climate and adaptation scenarios
Causal networks External, integrated Describe causal relationships between values. Long Term Water Plan (LTWP)
Spatial data External, integrated Describe spatial relationships Gauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin

Column widths.

None of those handle column widths with long text. kable smashes the first col, huxtable and gt smash them all.

Based on some github issues, does it work if I just don’t use a label at all?

Gt no label

texttab |> 
 gt::gt()
General HydroBOT components Type General component definitions Specific components used in our example
Input data Not part of HydroBOT Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models.  Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)
Controller Workflow Interface between input data, response model, and other toolkit components. Sets up run(s). Sets up links to data and parameters for EWR tool and aggregations.
Response models External, integrated A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers. EWR tool
Aggregator Workflow Aggregates response model results to scales across the dimensions of time, space, and theme.  Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).
Comparer Workflow Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables. Comparison of environmental values at various theme scales for the example climate and adaptation scenarios
Causal networks External, integrated Describe causal relationships between values. Long Term Water Plan (LTWP)
Spatial data External, integrated Describe spatial relationships Gauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin

Huxtable no label

texttab |> 
  huxtable::huxtable() |> 
  huxtable::theme_article()
General HydroBOT componentsTypeGeneral component definitionsSpecific components used in our example
Input dataNot part of HydroBOTHydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models. Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)
ControllerWorkflowInterface between input data, response model, and other toolkit components. Sets up run(s).Sets up links to data and parameters for EWR tool and aggregations.
Response modelsExternal, integratedA model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers.EWR tool
AggregatorWorkflowAggregates response model results to scales across the dimensions of time, space, and theme. Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).
ComparerWorkflowCompares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables.Comparison of environmental values at various theme scales for the example climate and adaptation scenarios
Causal networksExternal, integratedDescribe causal relationships between values.Long Term Water Plan (LTWP)
Spatial dataExternal, integratedDescribe spatial relationshipsGauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin

Divs

Flextable struggles in divs. The best we can do is to set width = 1 to at least make all the cols the same width and use the whole space. Though usually what i end up doing is rendering those tables outside a div and pasting in.

texttab |> 
  flextable::flextable() |> 
  flextable::set_table_properties(layout = "autofit", width = 1)
Table 12: This is a flextable in a div with width = 1

General HydroBOT components

Type

General component definitions

Specific components used in our example

Input data

Not part of HydroBOT

Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models. 

Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)

Controller

Workflow

Interface between input data, response model, and other toolkit components. Sets up run(s).

Sets up links to data and parameters for EWR tool and aggregations.

Response models

External, integrated

A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers.

EWR tool

Aggregator

Workflow

Aggregates response model results to scales across the dimensions of time, space, and theme. 

Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).

Comparer

Workflow

Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables.

Comparison of environmental values at various theme scales for the example climate and adaptation scenarios

Causal networks

External, integrated

Describe causal relationships between values.

Long Term Water Plan (LTWP)

Spatial data

External, integrated

Describe spatial relationships

Gauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin

If we don’t set widths, it’s smooshed, even with autofit.

texttab |> 
  flextable::flextable() |> 
  flextable::set_table_properties(layout = "autofit")
Table 13: This is a flextable in a div with width = 1

General HydroBOT components

Type

General component definitions

Specific components used in our example

Input data

Not part of HydroBOT

Hydrologic data (timeseries). Typically representing multiple scenarios, e.g. climate and climate adaptations. May include other inputs as needed by response models. 

Modified historical hydrographs to represent hypothetical climate change and adaptations (45 gauges, 15 scenarios)

Controller

Workflow

Interface between input data, response model, and other toolkit components. Sets up run(s).

Sets up links to data and parameters for EWR tool and aggregations.

Response models

External, integrated

A model of the response of values, e.g. social, cultural, environmental, or economic values in response to hydrologic drivers.

EWR tool

Aggregator

Workflow

Aggregates response model results to scales across the dimensions of time, space, and theme. 

Response model sets the base scale for aggregation. EWR tool assesses hydrologic indicators (value) at gauges (space) and year (time).

Comparer

Workflow

Compares scenarios (typically) or other groupings. Provides standard outputs including comparison methods, plots, and tables.

Comparison of environmental values at various theme scales for the example climate and adaptation scenarios

Causal networks

External, integrated

Describe causal relationships between values.

Long Term Water Plan (LTWP)

Spatial data

External, integrated

Describe spatial relationships

Gauge locations, Sustainable Diversion Limits (SDL) units, Murray-Darling Basin