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

Rendering both PDF and HTML is only possible interactively, not via script #301

Closed
jemus42 opened this issue Dec 2, 2018 · 3 comments
Closed

Comments

@jemus42
Copy link

jemus42 commented Dec 2, 2018

Describe the bug
I have some Rmd documents (weekly assignments) I want to render both as PDF (for turning them in) and HTML (for sharing them with classmates), and to make it easier, I use a simple build script that calls rmarkdown:render() for both formats:

#!/usr/bin/env Rscript

rmarkdown::render(input = "testing.Rmd", output_format = "html_document")
rmarkdown::render(input = "testing.Rmd", output_format = "pdf_document")

However, when used in a script, the commands yield an error when building the PDF:

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.

Since it is possible to render both PDF and HTML interactively in RStudio through the "knit" button, I am confused as to what exactly I'm doing wrong. Also, your previous comments on Stackoverflow I found through googling suggest that kabelExtra functions are "smart" enough to decide for themselves which output format is required.

To Reproduce

I have prepared a demo project here: https://github.com/jemus42/Rmd-pdf-html-debug
You can clone the project, interactively render the testing.Rmd to PDF and HTML, and then execute the lines in the build.R script which should yield the described error.

I would like to know if this is some bug somewhere or if my understanding of the knitr/RMarkdown/kableExtra internals is merely insufficient.

Edit:
Tested this on macOS Mojave and Ubuntu 16.04, each with current R 3.5.1 and up to date packages (CRAN)

@haozhu233
Copy link
Owner

Hi, @jemus42 this question is kind of related with this SO question. The trick this time is to add envir = new.env(). I put the detailed explanation in my updated answer there.

In your case,

rmarkdown::render(input = "testing.Rmd", output_format = "html_document", envir = new.env())
rmarkdown::render(input = "testing.Rmd", output_format = "pdf_document", envir = new.env())

@jemus42
Copy link
Author

jemus42 commented Jan 26, 2019

Thank you! You have now solved my first and only problem with kableExtra so far! :)

@haozhu233
Copy link
Owner

@jemus42 👍👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants