Comments (6)
Hello,
No sorry.
from officedown.
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.
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.
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
- Generate the document via officedown - rmarkdown::render
- 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.
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
- Generate the document via officedown - rmarkdown::render
- Open the resulting document, edit it and print/save it
This is inspired by:
https://stackoverflow.com/questions/39713618/knitr-hook-to-postprocess-pdf-outputhttps://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.
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)
- Math font in rdocx_document HOT 1
- Add image to word doc header without using reference docx
- Error when using `knit_print_block` in loop HOT 6
- Header Problems concomitant with page numbering HOT 6
- Support {.unlisted .unnumbered} to exclude headings from table of contents HOT 2
- table caption style in yaml not used HOT 4
- insert section with page_margins, header and footer
- How to split long flextable in officedown powerpoint rmarkdown HOT 1
- landscape? HOT 1
- block_caption() in markdown corrups Word files HOT 5
- In-consistencies with the officeverse bookdown HOT 1
- layout='Content with Caption' not working HOT 2
- Header and footer from reference_docx not working with orientation switches.
- Knitting to word with hyperlink in footnote HOT 1
- Two column layout results in missing header and footer also affects Title and ToC.
- Applying a table style to a reference docx HOT 1
- Typo Readme HOT 2
- Non-reproducible code from the package example HOT 5
- Embedded fonts in reference document not carrying thru to output HOT 1
- Images in page headers of reference docx misplaced when using <!---BLOCK_LANDSCAPE_START---> <!---BLOCK_LANDSCAPE_STOP---> HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from officedown.