Add a flextable at a bookmark in the body of a word document from R











up vote
1
down vote

favorite












I'm trying to add a flextable at a specific bookmark within the body of a Word document. So far I have found the following options:




  • footers_flextable_at_bkm {flextable}

  • headers_flextable_at_bkm {flextable}

  • body_add_flextable {flextable}


But none of them do exactly what I need.



I tried to write my own code to do it (see below), but it says that the docx_str function is not found. (I coped/modified this code from the headers_flextable_at_bkm function). Does anyone have a way to do this? Or is it a fuction that could be added to the flextable package?



body_flextable_at_bkm <- function(x, bookmark, value){

stopifnot(inherits(x, "rdocx"), inherits(value, "flextable"))

str <- docx_str(value, doc = x, align = "center")

xml_elt <- as_xml_document(x)

for(doc_obj in x$body){
if( doc_obj$has_bookmark(bookmark) ){
doc_obj$cursor_bookmark(bookmark)
cursor_elt <- doc_obj$get_at_cursor()
xml_replace(cursor_elt, xml_elt)
}
}
}









share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm trying to add a flextable at a specific bookmark within the body of a Word document. So far I have found the following options:




    • footers_flextable_at_bkm {flextable}

    • headers_flextable_at_bkm {flextable}

    • body_add_flextable {flextable}


    But none of them do exactly what I need.



    I tried to write my own code to do it (see below), but it says that the docx_str function is not found. (I coped/modified this code from the headers_flextable_at_bkm function). Does anyone have a way to do this? Or is it a fuction that could be added to the flextable package?



    body_flextable_at_bkm <- function(x, bookmark, value){

    stopifnot(inherits(x, "rdocx"), inherits(value, "flextable"))

    str <- docx_str(value, doc = x, align = "center")

    xml_elt <- as_xml_document(x)

    for(doc_obj in x$body){
    if( doc_obj$has_bookmark(bookmark) ){
    doc_obj$cursor_bookmark(bookmark)
    cursor_elt <- doc_obj$get_at_cursor()
    xml_replace(cursor_elt, xml_elt)
    }
    }
    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to add a flextable at a specific bookmark within the body of a Word document. So far I have found the following options:




      • footers_flextable_at_bkm {flextable}

      • headers_flextable_at_bkm {flextable}

      • body_add_flextable {flextable}


      But none of them do exactly what I need.



      I tried to write my own code to do it (see below), but it says that the docx_str function is not found. (I coped/modified this code from the headers_flextable_at_bkm function). Does anyone have a way to do this? Or is it a fuction that could be added to the flextable package?



      body_flextable_at_bkm <- function(x, bookmark, value){

      stopifnot(inherits(x, "rdocx"), inherits(value, "flextable"))

      str <- docx_str(value, doc = x, align = "center")

      xml_elt <- as_xml_document(x)

      for(doc_obj in x$body){
      if( doc_obj$has_bookmark(bookmark) ){
      doc_obj$cursor_bookmark(bookmark)
      cursor_elt <- doc_obj$get_at_cursor()
      xml_replace(cursor_elt, xml_elt)
      }
      }
      }









      share|improve this question















      I'm trying to add a flextable at a specific bookmark within the body of a Word document. So far I have found the following options:




      • footers_flextable_at_bkm {flextable}

      • headers_flextable_at_bkm {flextable}

      • body_add_flextable {flextable}


      But none of them do exactly what I need.



      I tried to write my own code to do it (see below), but it says that the docx_str function is not found. (I coped/modified this code from the headers_flextable_at_bkm function). Does anyone have a way to do this? Or is it a fuction that could be added to the flextable package?



      body_flextable_at_bkm <- function(x, bookmark, value){

      stopifnot(inherits(x, "rdocx"), inherits(value, "flextable"))

      str <- docx_str(value, doc = x, align = "center")

      xml_elt <- as_xml_document(x)

      for(doc_obj in x$body){
      if( doc_obj$has_bookmark(bookmark) ){
      doc_obj$cursor_bookmark(bookmark)
      cursor_elt <- doc_obj$get_at_cursor()
      xml_replace(cursor_elt, xml_elt)
      }
      }
      }






      r flextable officer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 1 at 1:34









      kenlukas

      1,2321217




      1,2321217










      asked Oct 1 at 1:30









      Anna Burn

      949




      949
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This function should do what you need:




          body_flextable_at_bkm <- function(x, bookmark, value){
          x <- cursor_bookmark(x, bookmark)
          x <- body_add_flextable(x = x, value = value, pos = "on")
          x
          }






          share|improve this answer





















          • On another note, is there way to automatically calculate and add column sums into the footer of a table?
            – Anna Burn
            Oct 3 at 4:02


















          up vote
          0
          down vote













          After a bit more digging there is a workaround where you add set_curser(x, bookmarkID) before the body_add_flextable(x, flextable) command.



          For consistaency it would still be nice to have the body_flextable_at_bkm as part of the flextable package so it can be used like headers_flextable_at_bkm and footers_flextable_at_bkm commands.



          Edit: the body_flextable_at_bkm command should now work with the latest version of the package.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52583571%2fadd-a-flextable-at-a-bookmark-in-the-body-of-a-word-document-from-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            This function should do what you need:




            body_flextable_at_bkm <- function(x, bookmark, value){
            x <- cursor_bookmark(x, bookmark)
            x <- body_add_flextable(x = x, value = value, pos = "on")
            x
            }






            share|improve this answer





















            • On another note, is there way to automatically calculate and add column sums into the footer of a table?
              – Anna Burn
              Oct 3 at 4:02















            up vote
            1
            down vote



            accepted










            This function should do what you need:




            body_flextable_at_bkm <- function(x, bookmark, value){
            x <- cursor_bookmark(x, bookmark)
            x <- body_add_flextable(x = x, value = value, pos = "on")
            x
            }






            share|improve this answer





















            • On another note, is there way to automatically calculate and add column sums into the footer of a table?
              – Anna Burn
              Oct 3 at 4:02













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            This function should do what you need:




            body_flextable_at_bkm <- function(x, bookmark, value){
            x <- cursor_bookmark(x, bookmark)
            x <- body_add_flextable(x = x, value = value, pos = "on")
            x
            }






            share|improve this answer












            This function should do what you need:




            body_flextable_at_bkm <- function(x, bookmark, value){
            x <- cursor_bookmark(x, bookmark)
            x <- body_add_flextable(x = x, value = value, pos = "on")
            x
            }







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 1 at 7:53









            David Gohel

            2,4322614




            2,4322614












            • On another note, is there way to automatically calculate and add column sums into the footer of a table?
              – Anna Burn
              Oct 3 at 4:02


















            • On another note, is there way to automatically calculate and add column sums into the footer of a table?
              – Anna Burn
              Oct 3 at 4:02
















            On another note, is there way to automatically calculate and add column sums into the footer of a table?
            – Anna Burn
            Oct 3 at 4:02




            On another note, is there way to automatically calculate and add column sums into the footer of a table?
            – Anna Burn
            Oct 3 at 4:02












            up vote
            0
            down vote













            After a bit more digging there is a workaround where you add set_curser(x, bookmarkID) before the body_add_flextable(x, flextable) command.



            For consistaency it would still be nice to have the body_flextable_at_bkm as part of the flextable package so it can be used like headers_flextable_at_bkm and footers_flextable_at_bkm commands.



            Edit: the body_flextable_at_bkm command should now work with the latest version of the package.






            share|improve this answer



























              up vote
              0
              down vote













              After a bit more digging there is a workaround where you add set_curser(x, bookmarkID) before the body_add_flextable(x, flextable) command.



              For consistaency it would still be nice to have the body_flextable_at_bkm as part of the flextable package so it can be used like headers_flextable_at_bkm and footers_flextable_at_bkm commands.



              Edit: the body_flextable_at_bkm command should now work with the latest version of the package.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                After a bit more digging there is a workaround where you add set_curser(x, bookmarkID) before the body_add_flextable(x, flextable) command.



                For consistaency it would still be nice to have the body_flextable_at_bkm as part of the flextable package so it can be used like headers_flextable_at_bkm and footers_flextable_at_bkm commands.



                Edit: the body_flextable_at_bkm command should now work with the latest version of the package.






                share|improve this answer














                After a bit more digging there is a workaround where you add set_curser(x, bookmarkID) before the body_add_flextable(x, flextable) command.



                For consistaency it would still be nice to have the body_flextable_at_bkm as part of the flextable package so it can be used like headers_flextable_at_bkm and footers_flextable_at_bkm commands.



                Edit: the body_flextable_at_bkm command should now work with the latest version of the package.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 4:09

























                answered Oct 1 at 2:42









                Anna Burn

                949




                949






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52583571%2fadd-a-flextable-at-a-bookmark-in-the-body-of-a-word-document-from-r%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini