GithubHelp home page GithubHelp logo

Comments (6)

davidgohel avatar davidgohel commented on June 18, 2024

Hello,

No sorry.

from officedown.

mtkerbeR avatar mtkerbeR commented on June 18, 2024

I solved a similar problem using a customized knit function - maybe this could help here as well:

https://bookdown.org/yihui/rmarkdown-cookbook/custom-knit.html

from officedown.

heor-andreas avatar heor-andreas commented on June 18, 2024

I solved a similar problem using a customized knit function - maybe this could help here as well:

https://bookdown.org/yihui/rmarkdown-cookbook/custom-knit.html

Thanks for pointing this out.
That looks promising but it looks like I can only get the rmarkdown::render function to run within the knit parameter. Did you manage to get other functions wrapped within the knit?

from officedown.

mtkerbeR avatar mtkerbeR commented on June 18, 2024

The following code works for me (please note, the empty lines within knit-function and there seems to be a parsing problem if you write comments within the code).

The general strategy is

  1. Generate the document via officedown - rmarkdown::render
  2. Open the resulting document, edit it and print/save it

This is inspired by:
https://stackoverflow.com/questions/39713618/knitr-hook-to-postprocess-pdf-output

https://stackoverflow.com/questions/28500096/r-markdown-variable-output-name/28785774#28785774

---
date: "2020-10-11"
author: "Your Name"
title: "officedown template"

knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_format = officedown::rdocx_document(),
      envir = globalenv()
    )
    
    filename <- paste0(basename(tools::file_path_sans_ext(input)), '.docx')
  
    doc <- officer::read_docx(filename)
    
    officer::body_add_par(doc, "... and this is written via customized knit")
    
    print(doc, filename)
    
  })
---

This is a text via officedown.

from officedown.

heor-andreas avatar heor-andreas commented on June 18, 2024

The following code works for me (please note, the empty lines within knit-function and there seems to be a parsing problem if you write comments within the code).

The general strategy is

  1. Generate the document via officedown - rmarkdown::render
  2. Open the resulting document, edit it and print/save it

This is inspired by:
https://stackoverflow.com/questions/39713618/knitr-hook-to-postprocess-pdf-output

https://stackoverflow.com/questions/28500096/r-markdown-variable-output-name/28785774#28785774

---
date: "2020-10-11"
author: "Your Name"
title: "officedown template"

knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_format = officedown::rdocx_document(),
      envir = globalenv()
    )
    
    filename <- paste0(basename(tools::file_path_sans_ext(input)), '.docx')
  
    doc <- officer::read_docx(filename)
    
    officer::body_add_par(doc, "... and this is written via customized knit")
    
    print(doc, filename)
    
  })
---

This is a text via officedown.

Thanks again, that works well for me.
I actively avoided the empty lines when I wrote my code...

do you think there is a way to access the params or any part of the YAML header through this function?

here's what I tried, but it looks like the params isn't being picked up. (it works fine printed in the document).
there's obviously the way to specify the two parameters as part of the function call like: (function(input, short_title="My short title",...) {} but I guess it would be more convenient as a parameter

That said I just realised I don't seem to be able to pass arguments to this when I package this up in a function...

---
date: "`r Sys.Date()`"
title: "Study Report"
output: 
  officedown::rdocx_document:
    reference_docx: template.docx
params:
  short_title: "My Study short title"
  document_type: "My Study Report"
 
knit: (function(input, ...) {
    rmarkdown::render(
      input,
      envir = globalenv()
    )

    filename <- paste0(basename(tools::file_path_sans_ext(input)), '.docx')
    
    doc <- officer::read_docx(filename)

    officer::headers_replace_all_text(doc, "< Insert Short study title >", params$short_title)

    print(doc, filename)

  })

from officedown.

mtkerbeR avatar mtkerbeR commented on June 18, 2024

Interesting question - I think "yes", there should be a way to "Access YAML parameters in a customized knitr-function"

However, as this question seems to be unrelated to officedown, I would suggest to pose this question at another place like stackoverflow (probably together with a more general example using hml rather than officedown/officer to increase chances of a good/fast answer).

from officedown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.