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

Output Format-specific output_file #277

Closed
jamiefolson opened this issue Oct 13, 2014 · 12 comments
Closed

Output Format-specific output_file #277

jamiefolson opened this issue Oct 13, 2014 · 12 comments

Comments

@jamiefolson
Copy link
Contributor

@jamiefolson jamiefolson commented Oct 13, 2014

I am currently using various knitr options with pandoc to render multiple distinct versions of an output file to different locations.

To clarify, I have a top-level directory structure like:

  • src
  • build
  • data

I currently place all knitr cache, knitr figures, build markdown and pdf/html output in the build directory. This makes it easier to identify generated artifacts from the source files, as well as keep multiple output (from multiple knit/pandoc calls) files separate.

It would be great if I could transition this process over to render using different output_formats and use the Rstudio UI. However, it seems that both the intermediate and output files are defined inside render based on the filename when no output_file argument is defined (as happens from the rstudio UI). This means that I'd need to use a post-processor to move the initial output file to my target location, populating my source directory with ambiguous output.

Maybe output_format could return a filename to render so that output_formats could choose their output_files?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Oct 14, 2014

Another possibility for you is to keep your previous workflow and just define a custom knit function inside your document. If you add a metadata field named "knit" with the name of an R function that will be called rather than render. The hookup to the RStudio previewer is a bit delicate but possible: so long as the the string "Output created: " exists in the console output from your custom knit that file will then be shown in the appropriate previewer.

@jamiefolson
Copy link
Contributor Author

@jamiefolson jamiefolson commented Oct 14, 2014

That sounds pretty slick. Would this be a replacement for knit or render? A custom knit would only create the "intermediate" output, not the final output from pandoc.

Would this "knit" field be defined as a knitr hook or option? Could this be specified in the knitr options to output_format instead?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Oct 14, 2014

The function called from the "knit" entry needs to produce everything
(including calling pandoc, etc.). The reason it's called "knit" is that it
is tied to the "Knit" button in RStudio.

The "knit" field would be included directly in the document YAML metadata.
Here's an example usage (a custom knit function defined for slidify):

ramnathv/slidify@07794a3

On Tue, Oct 14, 2014 at 10:20 AM, Jamie F Olson notifications@github.com
wrote:

That sounds pretty slick. Would this be a replacement for knit or render?
A custom knit would only create the "intermediate" output, not the final
output from pandoc.

Would this "knit" field be defined as a knitr hook or option? Could this
be specified in the knitr options to output_format instead?


Reply to this email directly or view it on GitHub
#277 (comment).

@jamiefolson
Copy link
Contributor Author

@jamiefolson jamiefolson commented Oct 14, 2014

Are these YAML "hooks" documented anywhere? Would it be possible to hook into the "Knit HTML" and "Knit PDF" buttons, too?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Oct 14, 2014

No, they aren't documented (yet). If you have this hook enabled then the
button just reads "Knit" and the HTML and PDF options don't appear.

On Tue, Oct 14, 2014 at 11:06 AM, Jamie F Olson notifications@github.com
wrote:

Are these YAML "hooks" documented anywhere? Would it be possible to hook
into the "Knit HTML" and "Knit PDF" buttons, too?


Reply to this email directly or view it on GitHub
#277 (comment).

@jamiefolson
Copy link
Contributor Author

@jamiefolson jamiefolson commented Oct 15, 2014

Are there any arguments passed to the "knit" function?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Oct 15, 2014

Yes (see the example I linked to previously).

On Wed, Oct 15, 2014 at 2:00 PM, Jamie F Olson notifications@github.com
wrote:

Are there any arguments passed to the "knit" function?


Reply to this email directly or view it on GitHub
#277 (comment).

@jamiefolson
Copy link
Contributor Author

@jamiefolson jamiefolson commented Oct 15, 2014

So the only arguments specified for the "knit" function are the filename and the encoding, both passed as unnamed arguments?

@jjallaire
Copy link
Member

@jjallaire jjallaire commented Oct 15, 2014

Yes that's exactly right.

On Wed, Oct 15, 2014 at 2:10 PM, Jamie F Olson notifications@github.com
wrote:

So the only arguments specified for the "knit" function are the filename
and the encoding, both passed as unnamed arguments?


Reply to this email directly or view it on GitHub
#277 (comment).

@jamiefolson
Copy link
Contributor Author

@jamiefolson jamiefolson commented Oct 15, 2014

Thanks!

@lmmx
Copy link

@lmmx lmmx commented Feb 28, 2015

For anyone else who came across this searching, the hook function can be anonymous, but has to be a one-liner, e.g. a knit function to set the output filename to "README.md":

knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = paste0(dirname(inputFile),'/README.md')) })

Metadata is still used to set the contents of the file as expected.

See also this related StackOverflow Q&A

@github-actions
Copy link

@github-actions github-actions bot commented 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants