Can I access the chunk info (such as line number) before rendering? #12051
-
DescriptionI am building a gallery of plots with Quarto using the For example, I have the following document: ---
lightbox: true
execute:
echo: false
---
```{r}
#| lightbox:
#| group: r-graph
#| description: "[Code](<PATH TO REPO>/gallery.qmd#L7)"
plot(iris)
```
```{r}
#| lightbox:
#| group: r-graph
#| description: "[Code](<PATH TO REPO>/gallery.qmd#L14)"
plot(mtcars)
``` When the user clicks on a plot, the link to the code appears in the description (the link doesn't work because of the invalid path, but it works with a real link). As you can see, the line numbers in the link ( Therefore, is there a way to know automatically the line number at which a chunk starts and use this info? No clue if that would work but I have in mind something like |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You want a line to know where it is located? That's not possible without static code analysis. There is also |
Beta Was this translation helpful? Give feedback.
-
As more context, source lines are tracked internally so that quarto can correctly give information about where an error is thrown. This is far from perfect and this is done by engine and possibly with quarto fixup (as internal source can be different). However, this is not exposed easily today. To have something like Something to experiment there probably. PR welcome in knitr if you have a proposal to expose internal nicely. Otherwise, post processing idea as shared above could be working well for your specific use case. You would probably need some sort of identifiers on plots so that you can match source line with HTML output in the document. Playing with pre-render script to create a JSON representation of you source document mapping to line number and then JS script in your document to process the JSON and add the description could be another idea. Or processing through Lua to add before HTML rendering. Anyway, a lot to play with for a fun project 😄 I'll be interest in your results 😉 - I am not sure we can provide some more help with this any time soon unfortunately. 😞 |
Beta Was this translation helpful? Give feedback.
-
Thanks for your answers @cderv and @mcanouil! I've moved to a simpler solution which is simply to store the script and image in an external folder and then do something like:
for each plot. It's a bit more manual but I don't want to overengineer this. |
Beta Was this translation helpful? Give feedback.
Thanks for your answers @cderv and @mcanouil!
I've moved to a simpler solution which is simply to store the script and image in an external folder and then do something like:
for each plot. It's a bit more manual but I don't want to overengineer this.