You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
rmarkdown::render with output_format=html_notebook mangles output file name #1180
If I have an Rmd file named test.Rmd and I try to render it programmatically, passing a specific file name to rmarkdown::render, I expect the output to always be placed at that file name. However, when I set output_format="html_notebook" and output_file="test.html", the generated file is instead named "test.nb.html". This hinders my attempts to put Rmarkdown rendering into a step in my reproducible workflow, because I can't precisely control the output file name.
I know that Rmd notebook output files normally have a special extension, but I think manually specifying an output file name should override any automatic naming. Why should it output to any file name other than the one I explicitly requested in the call to render?
For what it's worth, I'm using the html_notebook as the output format for my html files instead of html_document because it provides nice features like paginated tables and hiding code blocks without me having to do anything special.
Since you are rendering notebooks programmatically, perhaps it is not too much hassle to file.rename() the output. If it is, I'll be happy to investigate this issue in the future (or you can investigate it by yourself and send a pull request).
So, looking at the code, the post_processor function in html_notebook.R is what does the renaming. We can't just skip calling this in render.R, since other formats' post-processor functions perform important tasks. So it seems like the post-processor function interface would need to gain a new argument to indicate whether the output file name was auto-generated or manually specified. Or it could be hacked by attaching an attribute to the output_file variable to indicate that it was manually specified. This doesn't seem particularly elegant, but it could also be mostly internal to render.R and related functions rather than being exposed as an implementation detail.
Another idea would be to split the "post-processor" functionality into two different functions. A post-processor for processing the content of the output file, and a renamer for modifying the output file name. That way, we could always call the post-processor, but only call the renamer if the file name was not manually specified.
Perhaps the best alternative in this case would be to just add all the fancy features included in the html_notebook format to the html_document format as well, perhaps using an opt-in parameter or something if you don't want to change the default. That would remove my motivation for using the html_notebook format programmatically.
Activity
DarwinAwardWinner commentedon Oct 26, 2017
Note that if I use
output_file="test"(i.e. no file extension), then the file name is not mangled.yihui commentedon Oct 27, 2017
.nb.htmlis the special filename extension for R Markdown Notebook output files. That is by design.If you want
.html, you need to use a normal HTML output format likehtml_document.DarwinAwardWinner commentedon Oct 27, 2017
I know that Rmd notebook output files normally have a special extension, but I think manually specifying an output file name should override any automatic naming. Why should it output to any file name other than the one I explicitly requested in the call to
render?For what it's worth, I'm using the
html_notebookas the output format for my html files instead ofhtml_documentbecause it provides nice features like paginated tables and hiding code blocks without me having to do anything special.yihui commentedon Oct 27, 2017
I see.
Since you are rendering notebooks programmatically, perhaps it is not too much hassle to
file.rename()the output. If it is, I'll be happy to investigate this issue in the future (or you can investigate it by yourself and send a pull request).DarwinAwardWinner commentedon Oct 27, 2017
Yes, that's the workaround I've gone with: creating a temporary output file with no extension and then renaming it to the name I actually want.
DarwinAwardWinner commentedon Nov 1, 2017
So, looking at the code, the
post_processorfunction in html_notebook.R is what does the renaming. We can't just skip calling this in render.R, since other formats' post-processor functions perform important tasks. So it seems like the post-processor function interface would need to gain a new argument to indicate whether the output file name was auto-generated or manually specified. Or it could be hacked by attaching an attribute to the output_file variable to indicate that it was manually specified. This doesn't seem particularly elegant, but it could also be mostly internal to render.R and related functions rather than being exposed as an implementation detail.Another idea would be to split the "post-processor" functionality into two different functions. A post-processor for processing the content of the output file, and a renamer for modifying the output file name. That way, we could always call the post-processor, but only call the renamer if the file name was not manually specified.
Perhaps the best alternative in this case would be to just add all the fancy features included in the html_notebook format to the html_document format as well, perhaps using an opt-in parameter or something if you don't want to change the default. That would remove my motivation for using the html_notebook format programmatically.
github-actions commentedon Nov 3, 2020
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.