yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 876 forks source link

Request: knitr as pandoc filter? #1940

Open cysouw opened 3 years ago

cysouw commented 3 years ago

I love Knitr and Rmarkdown/Bookdown! However, in many situations I need knits only a few times in a long (scientific) text. In such situations I tend to use just Pandoc Markdown with filters and my text editor of choice. For such a workflow it would be great if it would be possible to reverse the workflow and use Knitr as a filter inside Pandoc. Is that already possible? And if not, would it be possible to make such a filter?


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 3 years ago

That is an interesting idea. Can you share an example of what you have in mind ?

It is possible to have a Pandoc filter running any program. So it would be possible to have a pandoc filter execute R to process some part. I don't this would be included in knitr directly. It would be just a way to use knitr (or mimick knitr) in a Lua filter.

I know about this Lua filter to have "diagram chunk" and it is close from what knitr could do https://github.com/pandoc/lua-filters/tree/master/diagram-generator

Unfortunately I won't have time to look into it very soon, even if I find such challenges quite fun. However, if you are willing to try something for a Proof of Concept of what you want, we'll be happy to look into it and help !

Here are some ressources:

And maybe someone already tried something. We'll see if anyone reach out here.

Thanks for the suggestion !

cysouw commented 3 years ago

There is also https://github.com/cdupont/R-pandoc by @cdupont to insert R plots (very nice!). However, it does not allow chunks to be evaluated and prepared as nicely as knitr does.

As an example of what I think of, take the minimal markdown examples from the manual, simply rename the .Rnw document as .md and then I would like to use something like:

pandoc knitr-minimal.md --to html --output knitr-minimal.html --filter knitr_filter

cderv commented 3 years ago

Thanks for sharing this project I wasn't aware of.

simply rename the .Rnw document as .md

I am not sure to follow. .Rnw is a latex file with some code chunk in it. I don't see why you would have this type of content in an .md file.

Also, if the aim is to have the exact same file in input as you would have with knitr why not use knitr directly ?

This is basically the workflow behind Rmarkdown (https://bookdown.org/yihui/rmarkdown-cookbook/rmarkdown-process.html) but it would allow to have very minimal impact as it just knit (even if R Markdown was designed so that this workflow goes well)

I am not sure to completely understand yet what would be the advantage of not using rmarkdown to have (knitr then Pandoc) and do the other way around Pandoc calling knitr on some part. I can however understand why R-pandoc to have so part of a .md file processed by Pandoc to be evaluated using R code.

I am surely missing something.

cysouw commented 3 years ago

sorry, I meant .Rmd not .Rnw ...

cderv commented 3 years ago

ok, this is what I thought. Thanks for confirming.

For a full existing .Rmd to .md, best is to use rmarkdown I think. It would be a full project to do all what knitr+rmarkdown are doing as a filter - but maybe possible. It need some thoughts (and time + work).

However, .md to .md can be done with knitr only if you have some code chunks you want to evaluate before running Pandoc.

Having a filter to run block identified as R code could be interesting too for users relying already on an existing Pandoc workflow.

This is an interesting discussion, Thank you!

cysouw commented 3 years ago

and yes, I can simply write a wrapper script around Pandoc when I use a file test.md

That is basically what I have done in the past.

However, Pandoc now has this nice defaults system in which to store all settings. It would be so much easier to add here a filter knitr and have all be done.

The problem is, filters work inside the processing of Pandoc, not outside as my workflow above illustrates.