Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quarto figures documentation doesn't include include_graphics() #12041

Open
nspyrison opened this issue Feb 6, 2025 · 9 comments
Open

Quarto figures documentation doesn't include include_graphics() #12041

nspyrison opened this issue Feb 6, 2025 · 9 comments
Labels
support a request for support

Comments

@nspyrison
Copy link

What would you like to do?

Report an issue on quarto.org

Description

https://quarto.org/docs/authoring/figures.html does not demonstrate knitr::include_graphics().

Please add a working example.

knitr::include_graphics() isn't rendering causing an unexpected amount of trouble, including:
https://stackoverflow.com/questions/47155042/knitrinclude-graphics-in-bookdown-is-not-rendering-the-image
#3245

@nspyrison nspyrison added the documentation Doc improvements & quarto-web label Feb 6, 2025
@mcanouil
Copy link
Collaborator

mcanouil commented Feb 6, 2025

Why would Quarto demonstrate this? And why are you referencing a thread older than quarto itself?

There is no need for knitr::include_graphics()1 since Quarto resolves Markdown image syntax natively as demonstrated in the documentation you've linked.

What is your issue exactly?

Footnotes

  1. It's also R specific while Quarto is not.

@mcanouil mcanouil added support a request for support and removed documentation Doc improvements & quarto-web labels Feb 6, 2025
@nspyrison
Copy link
Author

To show how to use variable images paths.

I am trying to create a report that has a variable image. I want to assign the image path to a variable rather than use a hard coded image.

@mcanouil
Copy link
Collaborator

mcanouil commented Feb 7, 2025

You need to understand what the function does and emits, and how paths work.
Use keep-md: true.

Quarto uses:

  • /path/from/quarto/project/root/picture.png
  • ../path/relative/to/quarto/document/picture.png

So if you generate the markdown for the images (which is what the function does), you need to emits the right paths.

@nspyrison
Copy link
Author

Ahhh, that is producing the correct markdown, so include_graphics isn't the issue, but why is the image not being rendered. My example rendered the image occasionally, but not consistently. The image path is correct. Do you know when this icon is used?


title: "Untitled"
format: html
editor: visual

Quarto

plate_img_path <- "images/Bi-07_Plate1_CoE_AntiOx.PNG"
knitr::include_graphics(here::here(plate_img_path))

Image

@mcanouil
Copy link
Collaborator

mcanouil commented Feb 7, 2025

I can't help you without a reproducible example of your setup.

Could you share a small self-contained "working" (reproducible) example to work with, i.e., a complete Quarto document or a Git repository? The goal is to make it as easy as possible for us to recreate your problem so that we can fix it: please help us help you! Thanks.


You can share a self-contained "working" (reproducible) Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````).
See https://quarto.org/bug-reports.html#small-is-beautiful-aim-for-a-single-document-with-10-lines.

If you have multiple files (and if it is absolutely required to have multiple files), please share as a Git repository.

RPython
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: knitr
---

This is a reproducible Quarto document.

{{< lipsum 1 >}}

```{r}
x <- c(1, 2, 3, 4, 5)
y <- c(1, 4, 9, 16, 25)

plot(x, y)
```

![An image]({{< placeholder 600 400 >}}){#fig-placeholder}

{{< lipsum 1 >}}

The end after @fig-placeholder.
````
````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: jupyter
---

This is a reproducible Quarto document.

{{< lipsum 1 >}}

```{python}
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.plot(x, y)
plt.show()
```

![An image]({{< placeholder 600 400 >}}){#fig-placeholder}

{{< lipsum 1 >}}

The end after @fig-placeholder.
````

Additionally and if not already given, please share the output of quarto check within a code block (i.e., using three backticks ```txt), see https://quarto.org/bug-reports.html#check.

@mcanouil
Copy link
Collaborator

mcanouil commented Feb 7, 2025

I cannot reproduce your issue.
You should take a look at the path your are creating with here.
Use keep-md: true.

Image

@nspyrison
Copy link
Author

I have created a repository here: https://github.com/nspyrison/quarto_test

quarto check:

C:\Users\spyri\Documents\R\quarto_test>quarto check
A new release of Deno is available: 1.28.2 → 2.1.9 Run `deno upgrade` to install it.

[>] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.1: OK
      Dart Sass version 1.55.0: OK
[>] Checking versions of quarto dependencies......OK
[>] Checking Quarto installation......OK
      Version: 1.3.450
      Path: C:\Program Files\RStudio\resources\app\bin\quarto\bin
      CodePage: 1252

[>] Checking basic markdown render....OK

[>] Checking Python 3 installation....OK
      Version: 3.9.12 (Conda)
      Path: C:/Users/spyri/anaconda3/python.exe
      Jupyter: 4.9.2
      Kernels: python3

(\) Checking Jupyter engine render....Traceback (most recent call last):
  File "C:\Users\spyri\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\spyri\anaconda3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\spyri\anaconda3\lib\site-packages\ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "C:\Users\spyri\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 22, in <module>
    from IPython.core.application import (
  File "C:\Users\spyri\anaconda3\lib\site-packages\IPython\__init__.py", line 52, in <module>
    from .terminal.embed import embed
  File "C:\Users\spyri\anaconda3\lib\site-packages\IPython\terminal\embed.py", line 15, in <module>
    from IPython.core.interactiveshell import DummyMod, InteractiveShell
  File "C:\Users\spyri\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 73, in <module>
    from IPython.core.history import HistoryManager
  File "C:\Users\spyri\anaconda3\lib\site-packages\IPython\core\history.py", line 11, in <module>
    import sqlite3
  File "C:\Users\spyri\anaconda3\lib\sqlite3\__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "C:\Users\spyri\anaconda3\lib\sqlite3\dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.


Kernel died before replying to kernel_info
[>] Checking Jupyter engine render....OK

@mcanouil
Copy link
Collaborator

mcanouil commented Feb 7, 2025

Paths need to be relative to the document.
Absolute paths are not allowed as all should be contained within the project either defined by _quarto.yml or by the document itself.

See my previous comment #12041 (comment)

@cderv
Copy link
Collaborator

cderv commented Feb 11, 2025

I have created a repository here: nspyrison/quarto_test

Thanks for the example repo. By default, knitr::include_graphic() will modify to relative path (see rel_path argument) when an absolute path is passed. (here::here(img_path) creates absolute path).

So the intermediate md file contains the following from cell output

![](../images/Capture.PNG){width=126}

Which is correct relative path for your document.

This will be rendered in HTML to

<figure class="figure">
<p><img src="../images/Capture.PNG" class="img-fluid figure-img" width="126"></p>
</figure>

which is expected output.

However, you are working on a single document render. No _quarto.yml to make it a quarto project.

And this test.html page is served by quarto preview from its root. Can you see the issue now ?

quarto preview index.qmd will serve file at url http://localhost:<port>/ - this means that ../images/Capture.PNG is not reachable in this setup. http://localhost:<port>/../images/Capture.PNG leads nowhere from web server setup.

Now without re-running quarto render, if you were to run in R servr::httd(".") from your project root (the folder containing images/ and report/, then url for the doc will be
http://localhost:<port>/report/test.html and then now the image will show as http://127.0.0.1:4321/images/capture.PNG exists.

I hope this clarifies.

To summarize, you are trying to use project structure with relative path pointing to parent directory.
This will only work with some setup that do consider root project as the root for previewing.

So you should leverage Quarto project. Just adding _quarto.yml with

project: 
  type: default

will make quarto preview report/test.qmd in your example have the right root for the preview server.

Hope this helps understand the situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support a request for support
Projects
None yet
Development

No branches or pull requests

3 participants