insightsengineering / rlistings

Value formatting and ASCII rendering infrastructure for tables and listings.
https://insightsengineering.github.io/rlistings/
24 stars 5 forks source link

Export_as_txt is failing for pagination, counting wrong for number of lines. #191

Closed shajoezhu closed 9 months ago

shajoezhu commented 10 months ago

Export_as_txt is failing for pagination, counting wrong for number of lines.

Summary

Your brief description of the problem


# your reproducible example here

library(rlistings)
out <- pharmaverseadam::adae %>%
  select(AESOC, AEDECOD, AETERM) %>%
  unique()

var_labels(out) <- c(
  AESOC = "MedDRA System Organ Class",
  AEDECOD = "MedDRA Preferred Term",
  AETERM = "Investigator-Specified\nAdverse Event Term"
)

lsting <- as_listing(
  out,
  key_cols = c("AESOC", "AEDECOD"),
  disp_cols = names(out),
  main_title = "Glossary of Adverse Event Preferred Terms and Investigator-Specified Adverse Event Terms",
  subtitles = "Protocal xxx",
  main_footer = "Investigator text for AEs is coded using MedDRA version 26.1.",
  prov_footer = "blah blah blah"
)
export_as_txt(lsting, 
              file = "l_ae_with_new_line.txt", 
              colwidths = c(48, 38, 70), cpp = 180, lpp = 35, tf_wrap = T, page_break = "\f")

var_labels(out) <- c(
  AESOC = "MedDRA System Organ Class",
  AEDECOD = "MedDRA Preferred Term",
  AETERM = "Investigator-Specified Adverse Event Term"
)

lsting <- as_listing(
  out,
  key_cols = c("AESOC", "AEDECOD"),
  disp_cols = names(out),
  main_title = "Glossary of Adverse Event Preferred Terms and Investigator-Specified Adverse Event Terms",
  subtitles = "Protocal xxx",
  main_footer = "Investigator text for AEs is coded using MedDRA version 26.1.",
  prov_footer = "blah blah blah"
)
export_as_txt(lsting, 
              file = "l_ae_no_new_line.txt", 
              colwidths = c(48, 38, 70), cpp = 180, lpp = 35, tf_wrap = T, page_break = "\f")

One would expect "blah blah blah" appears at lines 35, 70, 105, 140 and so on.

But "blah blah blah" appears at lines 35, 70, 105, 141 and so on.

R session info


# R -e "utils::sessionInfo()" output goes here

> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pharmaverseadam_0.1.1 citril_0.1.9          rlistings_0.2.7       tibble_3.2.1          tidyr_1.3.0           dplyr_1.1.2           scda_0.1.6.9012       tern_0.9.3.9001      
 [9] rtables_0.6.6         magrittr_2.0.3        formatters_0.5.5.9001

loaded via a namespace (and not attached):
 [1] utf8_1.2.3            generics_0.1.3        citril.metadata_0.1.6 stringi_1.7.12        lattice_0.21-8        dunlin_0.1.6          digest_0.6.31         evaluate_0.21        
 [9] grid_4.3.1            fastmap_1.1.1         jsonlite_1.8.7        R.oo_1.25.0           Matrix_1.5-4.1        R.utils_2.12.2        backports_1.4.1       survival_3.5-5       
[17] purrr_1.0.1           fansi_1.0.4           scales_1.2.1          Rdpack_2.4            cli_3.6.1             nestcolor_0.1.1.9001  rlang_1.1.1           rbibutils_2.2.13     
[25] R.methodsS3_1.8.2     munsell_0.5.0         splines_4.3.1         withr_2.5.0           yaml_2.3.7            tools_4.3.1           checkmate_2.2.0       colorspace_2.1-0     
[33] ggplot2_3.4.0         forcats_1.0.0         broom_1.0.5           vctrs_0.6.2           R6_2.5.1              lifecycle_1.0.3       stringr_1.5.0         pkgconfig_2.0.3      
[41] pillar_1.9.0          gtable_0.3.3          glue_1.6.2            xfun_0.39             tidyselect_1.2.0      chevron_0.2.4         knitr_1.43            rstudioapi_0.14      
[49] htmltools_0.5.5       rmarkdown_2.22        compiler_4.3.1

OS / Environment

Melkiades commented 9 months ago

I tried to replicate the difference but with the current main version this discrepancy is not apparent:

# Define the file path and the string to search
filePath_nl <- "l_ae_with_new_line.txt"
filePath_no_nl <- "l_ae_no_new_line.txt"
searchString <- "blah blah blah"

# Read the file
lines_nl <- readLines(filePath_nl)
lines_no_nl <- readLines(filePath_no_nl)

# Find the line number
lineNumber_nl <- grep(searchString, lines_nl)
lineNumber_no_nl <- grep(searchString, lines_no_nl)

# Print comparison
tibble::tibble(
  "File with new line" = lineNumber_nl,
  "File without new line" = lineNumber_no_nl,
  "Difference" = lineNumber_nl - lineNumber_no_nl)
# A tibble: 11 × 3
   `File with new line` `File without new line` Difference
                  <int>                   <int>      <int>
 1                   35                      35          0
 2                   70                      70          0
 3                  105                     105          0
 4                  141                     141          0
 5                  176                     176          0
 6                  211                     211          0
 7                  246                     246          0
 8                  281                     281          0
 9                  316                     316          0
10                  351                     351          0
11                  380                     369         11
shajoezhu commented 9 months ago

working together with https://github.com/insightsengineering/formatters/issues/243

shajoezhu commented 9 months ago

the newly updated version works very well. the above example returns

# A tibble: 11 × 3
   `File with new line` `File without new line` Difference
                  <int>                   <int>      <int>
 1                   35                      35          0
 2                   70                      70          0
 3                  105                     105          0
 4                  140                     140          0
 5                  175                     175          0
 6                  210                     210          0
 7                  245                     245          0
 8                  280                     280          0
 9                  315                     315          0
10                  350                     350          0
11                  380                     369         11