0

Why Officer write to doc reverse? I face that after i updated my R 4.0 to 4.2.

This is my code:

main.doc   <-read_docx()
body_add_par(main.doc," first ")
body_add_par(main.doc," second " )
body_add_par(main.doc," third ")
docx_summary(main.doc)

That is the result:

Content at cursor location:
  level num_id    text style_name content_type
1    NA     NA  third      Normal    paragraph

  doc_index content_type style_name     text level num_id

1         1    paragraph     Normal   **third**     NA     NA

2         2    paragraph     Normal  **second**     NA     NA

3         3    paragraph     Normal   **first**     NA     NA
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • 1
    Interesting, I got the expected result with officer 0.4.4, and then upon updating to 0.6.1, I got your result. Sounds like a bug introduced between versions. I have entered an issue here: https://github.com/davidgohel/officer/issues/484 – Jon Spring Mar 25 '23 at 22:42
  • Does this happen if you assign the result of each `body_add_par()` back to `main.doc`? – Z.Lin Mar 26 '23 at 02:39
  • Sorry for i couldnt get your question exactly Z.Lin, and what i have to do.. In the .docx result is same. At 0.3.12 version of officer (R 4.0.2) it was working. I updated to 0.6.1 (R 4.2.3) order comes reversed.. – OsmanAydin Mar 27 '23 at 08:24

1 Answers1

1

Based on the answer in the comment from @z-lin that is correct:

library(officer)
main.doc <- read_docx()
main.doc <- body_add_par(main.doc," first ")
main.doc <- body_add_par(main.doc," second " )
main.doc <- body_add_par(main.doc," third ")
docx_summary(main.doc)

Results need to be assigned.

You can read more about this in first paragraphs of the doc: https://ardata-fr.github.io/officeverse/officer-for-word.html

[...] use one of the body_add_* functions: [...]

They all have the same output and the same first argument: the R object representing the Word document, these functions are all taking as first input the document that needs to be filled with some R content and are all returning the document, that has been augmented with the new R content(s) after the function call.

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • David thanx, yes for the above example it works. However when using in function it reversed again ```me_add_par=function(f.doc,a_par) { f.doc<-body_add_par(f.doc, value = paste0("one-",a_par)) f.doc<-body_add_par(f.doc, value = paste0("two-",a_par)) } me_add_par(main.doc," function first") me_add_par(main.doc," function second") me_add_par(main.doc," function third") ``` – OsmanAydin Mar 27 '23 at 18:09
  • In the version 0.3.12 it (par,fpar,img ..) works in forward order, its logic was more understandable for me. Is there a way to make it..? Appreciated your kind interest. – OsmanAydin Mar 27 '23 at 18:22
  • See your call, instead of `me_add_par(main.doc," function first")`, use `main.doc <- me_add_par(main.doc," function first") `, this is R :) in the past it worked because of a side effect of a bug, it has been fixed. I think the manuals, guides and all my answers on SO show usage of assignment. – David Gohel Mar 27 '23 at 19:45
  • Thank you again @David, that was an interesting assigment for R :). To inform, I test two functions below, i found; me_add_par_1 works good with assigment, but me_add_par_2 gives error. ```me_add_par_1=function(f.doc,a_par) { a<-5*5 f.doc<-body_add_par(f.doc, value = a_par) } me_add_par_2=function(f.doc,a_par) { f.doc<-body_add_par(f.doc, value = a_par) a<-5*5 } main.doc<-me_add_par1(main.doc," text1") main.doc<-me_add_par2(main.doc," text2")``` – OsmanAydin Mar 28 '23 at 10:16
  • I a little bit confused that body_add_* logic. Why the document cannot countinue from the last cursor position and need such assigments (pls no need to answer, just a wish)? Actually my script has function in function cases to make code simple. My script works on 0.3.12 besides i am trying 0.6.1. Thanks again your kind responds. – OsmanAydin Mar 28 '23 at 10:18
  • If you say something is not working and I disagree, I have to answer:) What is not working is your function, it returns `a`, not `f.doc`. – David Gohel Mar 28 '23 at 11:47
  • after using me_add_par_2 function i test with ```docx_summary(main.doc)``` gives: Error in x$doc_obj : $ operator is invalid for atomic vectors. and doc file not produced with ```print(main.doc, target="test.docx")``` I have functions including calculations and body_add_*.. – OsmanAydin Mar 28 '23 at 15:38
  • As I explained to you in my previous comment, your R function is invalid, the last instruction is `a<-5*5`, not `f.doc`. – David Gohel Mar 28 '23 at 15:49
  • As seen solution for me, converting my functions to ```me_add_par_1``` like, it works. Namely, i should put ```body_add_par``` at the last line of function. – OsmanAydin Mar 28 '23 at 15:49