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

Allow 'pre_knit' hooks to return new file to be used for knit? #974

Closed
kevinushey opened this issue Feb 22, 2017 · 9 comments
Closed

Allow 'pre_knit' hooks to return new file to be used for knit? #974

kevinushey opened this issue Feb 22, 2017 · 9 comments
Labels
feature a feature request or enhancement theme: knitr concerns knitr package

Comments

@kevinushey
Copy link
Contributor

kevinushey commented Feb 22, 2017

Motivation: currently, HTML comments are not really properly handled by R Markdown or knitr. For example, with the following document:

---
title: "Untitled"
output: html_document
---

<!--

```{r}
print("Hello!")
```

-->

Even though the R chunk below is within an HTML comment, that chunk still gets executed, and thus included and processed by Pandoc.

When rendering such a document in html_document mode, those comment markers survive, and so the generated output is not shown in the document, giving the user the impression that the commented chunk was ignored.

However, with html_notebook, we're not so lucky -- the closing comment tag is actually HTML-escaped, and so the comment never actually functions as expected -- we end up seeing the chunk output in the rendered document.

Correct me if I am wrong, but since the current experience is as though HTML comments in R Markdown documents are supported, we need to do something extra to ensure that this works for HTML notebooks.

My initial thought would be that the pre_knit hook could return a file path that would then be passed on to knitr::knit(), and that the pre_knit hook could be in charge of e.g. replacing comments with a placeholder (and later restoring the placeholders in e.g. a post_process hook). However, this is not currently possible because of how the pre_knit hooks are overlaid (the union of return values seems to be returned?)

Is this something that could be made possible?

@jjallaire
Copy link
Member

It looks like if we had the pre_knit function return a filename (as an alternate input) we could assign that to the knit_input variable and that would do the job. We should however inspect all uses of the knit_input variable to ensure that we don't need yet another variable that represents the file to be passed to knitr as distinct from knit_input.

As regards the union of pre_knit hooks, I'd just look for a filename and use the first or last one found (warning if more than one are found).

@baptiste
Copy link

baptiste commented Oct 23, 2017

Tangential to this discussion, I recently discovered an alternative strategy to comment out sections of Rmd documents, which works for both text and chunks,

---
title: "Untitled"
output: html_notebook
---

---
# all this will be commented
# ```{r}
# print("Hello!")
# ```
---

I find it much nicer (with Ctrl+Shift+C) than the html comment tags, but it still doesn't prevent knitr from over-doing it with inline chunks.

@yihui yihui modified the milestones: v1.8, v1.9 Nov 15, 2017
@yihui yihui modified the milestones: v1.9, v1.10 Mar 4, 2018
@yihui yihui modified the milestones: v1.10, v1.11 Jun 15, 2018
@yihui yihui removed this from the v1.11 milestone Jun 25, 2018
@muschellij2
Copy link

muschellij2 commented Jun 25, 2019

So is it possible to do some preprocessing before knitting? I think this would be worthwhile as @jjallaire alluded to above. This would be helpful because otherwise you can't really preprocess the .Rmd/md file (such as stripping out sensitive information or something else tagged).

Using pre_knit does something but not sure as to what from https://github.com/rstudio/rmarkdown/blob/master/R/render.R#L529 and the docs don't seem to indicate the behavior of of the output: https://github.com/rstudio/rmarkdown/blob/master/R/output_format.R#L26. Hard to see how to do this also when defining or extending a format as in https://bookdown.org/yihui/rmarkdown/format-custom.html.

I used to be able to do this by using intermediates_generator and then replacing the knitted file with one where gsub and other text processing removed some tags.

Hacked something together in the pre_process part at https://github.com/muschellij2/didactr/blob/5d47be0d822543da6c550601f8382b57ca8d8a14/R/leanpub_render.R#L113

I may be missing something, but I'm trying to help our users use the convenient Knit button, so have to make a output format, otherwise a wrapper around render could do what I want.

@jdblischak
Copy link
Contributor

jdblischak commented Jul 19, 2019

I agree this would be a nice feature to have. Currently my workflowr package uses a hack to change the value of the internal variable knit_input in the call to render(). By setting the knit_root_dir and the output file, this enables it to knit a modified copy of the original file. I got this idea from Romain's rmarkdowntown package.

yihui added a commit to rstudio/rmarkdown-cookbook that referenced this issue Aug 12, 2019
@cderv cderv removed the enhancement label Apr 14, 2021
yihui added a commit to yihui/knitr that referenced this issue Oct 5, 2021
this is a different approach with HTML comments <!-- --> (rstudio/rmarkdown#974)
@yihui
Copy link
Member

yihui commented Oct 5, 2021

This feature still has not been implemented, but I just came up with another solution to comment out arbitrary content (including code chunks) in R Markdown, i.e., via the comment engine (which requires the dev version of knitr: remotes::install_github('yihui/knitr')), e.g.,

---
title: "Untitled"
output: html_document
---

````{comment}

```{r}
print("Hello!")
```

````

@kevinushey
Copy link
Contributor Author

That is very clever 😄 I love it!

@cderv cderv added feature a feature request or enhancement theme: knitr concerns knitr package labels Jan 11, 2022
@cderv cderv moved this to Backlog in R Markdown Team Projects Jan 12, 2022
@cderv cderv moved this from Backlog to Todo in R Markdown Team Projects Jan 12, 2022
@yihui
Copy link
Member

yihui commented Jan 12, 2022

The comment engine has been included in knitr 1.37: https://github.com/yihui/knitr/releases/tag/v1.37 Not sure if this alternative solution helps with the original problem. I'm closing this issue for now, but we can re-open it if we really need to support HTML comments. Thanks!

@yihui yihui closed this as completed Jan 12, 2022
Repository owner moved this from Todo to Done in R Markdown Team Projects Jan 12, 2022
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement theme: knitr concerns knitr package
Projects
Archived in project
Development

No branches or pull requests

8 participants