Skip to content

Formatting small numbers, inside a gt in docx output fails #1378

Closed
@mattansb

Description

@mattansb

Prework

Description

I don't know why, but this super specific combination of conditions results in the error:

Quitting from lines 14-17 [unnamed-chunk-2] (tests.qmd)
Error in `read_xml.raw()`:
! StartTag: invalid element name [68]
Backtrace:
  1. global .main()
 74. xml2:::as_xml_document.character(.)
 75. xml2:::read_xml.raw(...)
  1. Format a column using a specific function in gt() |> fmt(). For example:
  • scales::label_pvalue()
  • insight::format_p
  1. Have a data frame with a small number - how small? Smaller than smallest formatted output by these functions.
    For example, smaller than 0.001 (both of these functions by default show <0.001 for any p-value smaller than 0.001).
  2. Output to docx

Reproducible example

  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.
library(gt)
library(scales)

aov_table <- data.frame(asdqwezxc = c(0.0009))

aov_table |> 
  gt() |> 
  fmt(asdqwezxc, fns = label_pvalue(0.001)) |> 
  gt::as_word()
#> Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, : StartTag: invalid element name [68]

Created on 2023-07-19 with reprex v2.0.2

Session info
sessionInfo()
#> R version 4.3.1 (2023-06-16 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 11 x64 (build 22621)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Israel.utf8  LC_CTYPE=English_Israel.utf8   
#> [3] LC_MONETARY=English_Israel.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=English_Israel.utf8    
#> 
#> time zone: Asia/Jerusalem
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] scales_1.2.1 gt_0.9.0    
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.3       cli_3.6.1         knitr_1.43        rlang_1.1.1      
#>  [5] xfun_0.39         generics_0.1.3    glue_1.6.2        colorspace_2.1-0 
#>  [9] htmltools_0.5.5   fansi_1.0.4       rmarkdown_2.23    munsell_0.5.0    
#> [13] evaluate_0.21     tibble_3.2.1      fastmap_1.1.1     yaml_2.3.7       
#> [17] lifecycle_1.0.3   compiler_4.3.1    dplyr_1.1.2       fs_1.6.2         
#> [21] pkgconfig_2.0.3   rstudioapi_0.15.0 digest_0.6.33     R6_2.5.1         
#> [25] reprex_2.0.2      tidyselect_1.2.0  utf8_1.2.3        pillar_1.9.0     
#> [29] magrittr_2.0.3    tools_4.3.1       withr_2.5.0       xml2_1.3.5

Expected result

A docx with a gt output with a formatted column.


Originally posted quarto-dev/quarto-cli#6274

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions