Git Product home page Git Product logo

Comments (14)

xiaochi-liu avatar xiaochi-liu commented on September 25, 2024

Hello, may I get any help with this question? Thank you so much!

from officedown.

davidgohel avatar davidgohel commented on September 25, 2024

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.

xiaochi-liu avatar xiaochi-liu commented on September 25, 2024

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.

davidgohel avatar davidgohel commented on September 25, 2024

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)

Capture d’écran 2022-02-14 à 13 27 57

from officedown.

davidgohel avatar davidgohel commented on September 25, 2024

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.

xiaochi-liu avatar xiaochi-liu commented on September 25, 2024

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
```

image

from officedown.

davidgohel avatar davidgohel commented on September 25, 2024

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.

alapo avatar alapo commented on September 25, 2024

I asked a similar question here and used magick its not pretty put it does work for projects that require it.

from officedown.

davidgohel avatar davidgohel commented on September 25, 2024

@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.

alapo avatar alapo commented on September 25, 2024

@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.

davidgohel avatar davidgohel commented on September 25, 2024

The SO question is not about adding a caption but maximizing the dimensions in a Word page:

Capture d’écran 2022-02-23 à 17 40 43

(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.

xiaochi-liu avatar xiaochi-liu commented on September 25, 2024

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.

davidgohel avatar davidgohel commented on September 25, 2024

OK, thanks for the explanation. I think I understand now.

  1. 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.
  2. 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)

Capture d’écran 2022-02-24 à 09 24 35

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.

xiaochi-liu avatar xiaochi-liu commented on September 25, 2024

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)

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.