Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtsave breaks when in the loop #1726

Open
Tracked by #1792
rjazwiec opened this issue Jun 20, 2024 · 1 comment
Open
Tracked by #1792

gtsave breaks when in the loop #1726

rjazwiec opened this issue Jun 20, 2024 · 1 comment
Assignees

Comments

@rjazwiec
Copy link

Prework

Description

Table is saved to .docx properly when in separate chunk. When I try to generate many files from a loop it gives

Unknown output format doc
Pandoc can convert to DOCX, but not to DOC.
Error: pandoc document conversion failed with error 22

When generating .RTF files from the same loop it works properly

Reproducible example

Sample of data:

baza <- structure(list(data_file = c("032_01_012", "032_01_013", "032_01_026", 
"032_01_077", "032_01_088", "032_02_012", "032_02_040", "032_02_075", 
"032_02_086", "032_02_093", "032_03_013", "032_03_070"), chrom_number = c(12, 
13, 26, 77, 88, 12, 40, 75, 86, 93, 13, 70), seq_number = c(1, 
1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3), sample_id = c("BLK", "ZS", 
"1001A_Day_88_0.25h", "2002B_Day_88_2h", "2501B_Day_88_8h", "BLK", 
"1002A_Day_176_PD", "2001B_Day_176_8h", "2002B_Day_176_12h", 
"2501B_Day_176_4h", "ZS", "2001B_Day_267_12h"), sample_name = c("BLK", 
"ZS", "1001A_Day_88_0.25h", "2002B_Day_88_2h", "2501B_Day_88_8h", 
"BLK", "1002A_Day_176_PD", "2001B_Day_176_8h", "2002B_Day_176_12h", 
"2501B_Day_176_4h", "ZS", "2001B_Day_267_12h"), type = c("DoubleBlank", 
"Blank", "Sample", "Sample", "Sample", "DoubleBlank", "Sample", 
"Sample", "Sample", "Sample", "Blank", "Sample"), compound = c("HNK", 
"HNK", "HNK", "HNK", "HNK", "DHNK", "KET", "HNK", "HNK", "HNK", 
"KET", "HNK"), is_reint = c(0.622, 0.853, 0.873, 0.862, 0.873, 
2.495, 2.307, 0.886, 0.905, 0.875, 2.512, 0.916)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

Code:

seq <- 2

sub <- paste("Run 032_", str_pad(seq, 2, pad = "0"), sep = "")
  
  seq_list <- baza %>%
    filter(seq_number == seq) %>%
    rowid_to_column("No") %>%
    select(No, chromatogram_no = data_file,
           sample_name, compound) %>%
    gt() %>%
    tab_header(title = "Reintegrated chromatogram list",
                       subtitle = sub)
  
  gtsave(
    seq_list,
    filename = paste("reintegrated_list_", sub, ".docx", sep = ""),
    path = "Reintegrated/"
  )

Works properly for single seq. Works for any of seq from dataframe.

When put in a loop like this:

seq <- 1:3

for (sequence in seq) {
  sub <- paste("Run 032_", str_pad(sequence, 2, pad = "0"), sep = "")
  
  seq_list <- baza %>%
    filter(seq_number == sequence) %>%
    rowid_to_column("No") %>%
    select(No, chromatogram_no = data_file,
           sample_name, compound) %>%
    gt() %>%
    tab_header(title = "Reintegrated chromatogram list",
               subtitle = sub)
  
  gtsave(
    seq_list,
    filename = paste("reintegrated_list_", sub, ".docx", sep = ""),
    path = "Reintegrated/"
  )
  
}

Gives error message quoted previously.

When one line is changed to:

filename = paste("reintegrated_list_", sub, ".rtf", sep = "")

It properly generates 3 files.

Expected result

It should generate .docx files.

Session info

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Polish_Poland.utf8 LC_CTYPE=Polish_Poland.utf8 LC_MONETARY=Polish_Poland.utf8 LC_NUMERIC=C
[5] LC_TIME=Polish_Poland.utf8

time zone: Europe/Warsaw
tzcode source: internal

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

other attached packages:
[1] gtsummary_1.7.2 gt_0.10.1 janitor_2.2.0 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[8] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.0 tidyverse_2.0.0 renv_1.0.7

loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 xml2_1.3.6 stringi_1.8.3 hms_1.1.3 digest_0.6.35
[7] magrittr_2.0.3 evaluate_0.23 grid_4.3.2 timechange_0.3.0 fastmap_1.1.1 broom.helpers_1.15.0
[13] fansi_1.0.6 scales_1.3.0 cli_3.6.2 crayon_1.5.2 rlang_1.1.3 bit64_4.0.5
[19] munsell_0.5.1 withr_3.0.0 yaml_2.3.8 parallel_4.3.2 tools_4.3.2 tzdb_0.4.0
[25] colorspace_2.1-0 vctrs_0.6.5 R6_2.5.1 lifecycle_1.0.4 snakecase_0.11.1 fs_1.6.3
[31] bit_4.0.5 vroom_1.6.5 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.4 glue_1.7.0
[37] xfun_0.43 tidyselect_1.2.1 rstudioapi_0.16.0 knitr_1.46 htmltools_0.5.8.1 rmarkdown_2.26
[43] compiler_4.3.2

@rjazwiec
Copy link
Author

rjazwiec commented Dec 4, 2024

Ok. I accidently found solution.
The problem is not in the loop.
I saw that error was not in the first time I ran the loop. But when I was writing this issue i missed it.

The problem is that gtsave have problem with overwriting .docx documents (that's not the case with .rtf)

Simple but slow fix:

if (file.exists("file_to_save.docx")) {
  file.remove("file_to_save.docx")
} 

 gtsave(
    seq_list,
    filename = "file_to_save.docx",
    path = "Reintegrated/"
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants