officer icon indicating copy to clipboard operation
officer copied to clipboard

Cursor unable to advance even though there are still more elements

Open ischmidt20 opened this issue 8 months ago • 0 comments

As part of my project, I require the ability to edit some docx features that I have so far been unable to figure out how to do with officer, so I decided to edit the xml directly. For example, I have tables that follow a very specific format that I need to copy and paste into different places and slightly edit their text. However, whenever I do this, the rdocx object (more specifically, the officer_cursor object) does not appear to pick up the fact that there are now additional nodes in the document. I prefer to use the cursor_reach function and then cursor_forward to navigate throughout the document, but this fails when I add nodes and the cursor reaches the index of what was previously the last node.

Here is a MRE. I cannot upload work files directly from my work computer but I will attach it later. EDIT: Attached here: officerTest.docx

library(officer)
library(dplyr)
library(xml2)

doc = read_docx(path = "officerTest.docx")

allNodes <- (docx_body_xml(doc) %>% xml_children() %>% xml_children())
myTable = allNodes[3] # Identifies table

# Inserts copy table at end of document
docx_body_xml(doc) %>%
    xml_children() %>%
    xml_add_child(myTable, .where = 5)

This "works" in that it inserts a copy of the original table at the end of the document and everything saves as expected with print. If I do

doc$officer_cursor$which = 5
print(doc %>% docx_current_block_xml() %>% xml_text())

I get the expected result. Yet if I do:

print(doc %>% cursor_forward() %>% docx_current_block_xml() %>% xml_text())

I get the same exact text as before, even though what should have happened is the cursor advance to the next element, which is the table, and print that text. In fact, if I do:

doc$officer_cursor$which = 6
print(doc %>% docx_current_block_xml() %>% xml_text())

Then I get the text of the table as expected. So it seems what is happening is that the cursor thinks there are only 5 elements and it can't move forward any further, when in fact, it can (and should). Could this please be fixed? Or, if there are any preferred approaches for inserting nodes directly into the XML that do not cause this behavior, I would be happy to learn about those.

When submitting a new issue:

  • [x] Provide the code that is producing the error, it has to be a minimal reproducible example. Stackoverflow is providing good explanations about it: https://stackoverflow.com/help/mcve. You can use package reprex to help you: http://reprex.tidyverse.org/. The most popular R stackoverflow question is about the subject: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example.

  • [x] Provide the results of R command sessionInfo(). It had to be executed after you loaded the packages used by your example. This will let me know what is your version of R and what are the versions of the packages you used in your example.

R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] xml2_1.3.3    dplyr_1.1.0   officer_0.6.6

loaded via a namespace (and not attached):
 [1] bit_4.0.5         compiler_4.4.0    renv_1.0.1        promises_1.2.0.1  tidyselect_1.2.0 
 [6] Rcpp_1.0.10       zip_2.3.1         blob_1.2.3        later_1.3.0       systemfonts_1.0.4
[11] textshaping_0.3.6 uuid_1.1-0        fastmap_1.1.1     mime_0.12         R6_2.5.1         
[16] shinyjs_2.1.0     generics_0.1.3    openxlsx_4.2.5.2  htmlwidgets_1.6.1 tibble_3.1.8     
[21] openssl_2.0.5     shiny_1.8.1.1     DBI_1.1.3         pillar_1.8.1      rlang_1.1.0      
[26] utf8_1.2.3        DT_0.27           cachem_1.0.7      stringi_1.7.12    httpuv_1.6.15    
[31] bit64_4.0.5       RSQLite_2.3.0     memoise_2.0.1     cli_3.6.0         magrittr_2.0.3   
[36] digest_0.6.31     rstudioapi_0.16.0 xtable_1.8-4      askpass_1.1       lifecycle_1.0.3  
[41] vctrs_0.6.0       glue_1.6.2        ragg_1.2.5        fansi_1.0.4       pkgconfig_2.0.3  
[46] tools_4.4.0       htmltools_0.5.4  
  • [x] Make sure you did checked you had the latest version of the package on CRAN (and on github if issue exists with CRAN version).

  • [x] Make sure you searched in the open and closed issues on the github repository.

ischmidt20 avatar Jun 05 '24 20:06 ischmidt20