Comments (14)
Hello, may I get any help with this question? Thank you so much!
from officedown.
sure,
Function knitr::include_graphics()
is part of package 'knitr', not 'officedown'. I am afraid I can't do anything for modifying include_graphics()
from officedown.
Thanks @davidgohel!
Got it. So is there any other way to keep the original height/width ratio when importing a figure?
I like the officedown
package and I'm using it to draft the thesis, which contains a large amount of figures...
from officedown.
It is probably easy with only magick::image_read()
.
Why not using the classical way that works well?
I just tried and without using include_graphics()
but usual markdown, it works as expected:
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
library(officer)
library(officedown)
```
![](zzz.png)
from officedown.
For future readers
knitr::include_graphics
is not part of officedown but knitr. There is nothing to do with package officedown.
Users should use the Markdown syntax to insert images in a markdown document. (note : this does not apply only to officedown format but to all Markdown format),
![caption](path/to/image)
from officedown.
Thank you very much for your advice, @davidgohel.
The classical way of Markdown can insert the figure as its original height/width ratio. May I ask, can it be auto-numbered and cross-referenced?
I also tried magick::image_read()
. This can be auto-numbered, but officedown
still change the ratio:
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
library(officer)
library(officedown)
library(magick)
```
![The result of MAE](figures/mae and test - update.png
```{r mae, fig.cap="MAE"}
mae <- image_read('figures/mae and test - update.png')
mae
```
from officedown.
Captions are simple paragraphs. Use markdown or use what is documented in the manual:
https://ardata-fr.github.io/officeverse/officedown-for-word.html#custom-cross-reference
from officedown.
I asked a similar question here and used magick
its not pretty put it does work for projects that require it.
from officedown.
@alapo why don't you use the markdown tag for image? It respects dimensions.
The SO you linked is about changing graph dimensions so that it fills a page. Here the question is how to embed an external image in a word document and keep the aspect ratio without specifying the dimensions.
I think the answer is to simply use markdown tag for image.
from officedown.
@davidgohel I am unsure. The document in the SO question is for a scientific publication...as far as I am aware if you use the markdown tag it would mean that you cannot reference the image in text. In order to convert from pixel information using magick::image_info
you would need to specify the dpi/resolution. Apologies for the comment.
from officedown.
The SO question is not about adding a caption but maximizing the dimensions in a Word page:
(I don't understand why people want to write PNG on disk and insert it in the rmarkdown just after... Why not using the classic way, i.e. print a graph, use chunk options and benefit of captions/cross-ref and many options...)
A caption is a paragraph, inserted before or after an element (an image here).
If you use officedown or officer, you can create a caption that can be cross-referenced with officer::block_caption()
.
from officedown.
Thanks for your advice @davidgohel.
Sometimes, making figures for publication can involve a large amount of code (computation), especially when mapping or setting complicated ashthetics. If we print them directly, the rendering of the document is time-consuming.
In addition, the fig.height
and fig.width
settings of the code chunk are not always working appropriately for the rendering output. That is, a suitable setting for the code chunk output may become unsuitable in the rendering Word document. This situation often happens when the figure is large or with multiple panels. I also encountered this problem when using bookdown
.
Therefore, it might be easier to save the figure as a fixed PNG or JPEG then inserted it back.
from officedown.
OK, thanks for the explanation. I think I understand now.
- You don't want output graphics with the classical way (you should see caching options, it could help a lot), so you generated graphics in images. But these graphics need captions and should be cross-referencable.
- In the same time, you want a similar rendering between the word output and what you are seeing in the R Markdown view (probably in RStudio), Word is a paged support and HTML (the R Markdown editor) has different behavior.
For 1:
---
title: "test"
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(officedown)
library(officer)
z <- run_autonum(seq_id = "fig", pre_label = "Fig ",
tnd = 1, bkm = "")
```
# An example
```{r}
block_caption("blah",
style = "Image Caption",
autonum = set_autonum_bookmark(z, bkm = "fig1")
)
```
![hey hey](zz.png)
# Another example
```{r}
block_caption("blah blah",
style = "Image Caption",
autonum = set_autonum_bookmark(z, bkm = "fig2")
)
```
![ho ho](zz.png)
## Reference
This cross ref fig1 : \@ref(fig:fig1)
This cross ref fig1 : \@ref(fig:fig2)
For 2:
Unless you can show a reproducible example, I think fig.height
and fig.width
are working as expected. If you want to declare an issue, you should always come with a reproducible example. R Markdown editor (visual or not) is not a WYSIWYG interface.
You need to consider HTML and Word are 2 different formats. Word has a maximum width, this does not affect display in R Markdown viewer that is HTML. If your Word doc has a 7 inches' width, figures' width should not exceed that width because they won't be resized as in a Web browser.
I think you are making the assumption that formats should all behave the same, but it's not the case. If you want an HTML 'paged' format, I highly recommend package pagedown. If you need to stick to Word format, you'll have to live with its characteristics.
from officedown.
Got it. This works perfectly on my side. Thank you so much for your kind guidance, @davidgohel. I really appreciate that!
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
- (How to) Add footnotes through officedown HOT 1
- Fields Word document HOT 1
- Name for `tab.id` must not contain `_`? HOT 1
- `rpptx_document` does not use the assigned layout in code chunk HOT 3
- accessing different parts of word doc HOT 3
- how to add page numbers, title page and sections in word using officedown HOT 1
- Extra section break is created after MULTCOLUM stop HOT 5
- Option to output Word doc with line numbering HOT 5
- dml bg is always "white" in R Markdown HOT 2
- `block_pour_docx` but only for part of file to be poured
- 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.