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

Generate a PDF with Chrome using the Knit button of RStudio IDE #155

Merged
merged 7 commits into from
Dec 3, 2019

Conversation

RLesur
Copy link
Collaborator

@RLesur RLesur commented Nov 30, 2019

Here's a proposal to create a custom render function for the RStudio IDE.
My main problem is to give a name for this new function 😃 (that's why I open a PR).

In short, by adding knit: pagedown::knit_pdf_chrome in the YAML header, a PDF is also generated by clicking on the "Knit" button of RStudio.
I don't like very much the current name knit_pdf_chrome but I have no better idea. I'd appreciate any proposal.

This PR also enhances the documentation about printing to PDF.

@RLesur RLesur requested a review from yihui November 30, 2019 14:06
Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing a new function, I'd like make chrome_print work for the Knit button. We need to add the encoding argument to it (and ignore it). Then the only tricky thing is determine whether chrome_print is called by RStudio when the user clicks the Knit button. I think this should be sufficient:

is_rstudio_knit = !interactive() && !is.na(Sys.getenv('RSTUDIO', NA)) && !missing(encoding)

If you want to be more strict, you can add another condition length(match.call()) == 3 (to make sure only two arguments are passed to the function).

Then we call rmarkdown::render(..., quiet = is_rstudio_knit) to suppress the message, and emit the message('\nOutput create: ...') by ourselves.

Does this proposal sound good?

@RLesur
Copy link
Collaborator Author

RLesur commented Dec 3, 2019

Does this proposal sound good?

Of course... because I had already thought about that 😄
I like this idea very much. I will try.

@RLesur
Copy link
Collaborator Author

RLesur commented Dec 3, 2019

Then we call rmarkdown::render(..., quiet = is_rstudio_knit)

I already tried quiet = TRUE. In this case, this also removes the knitr progress messages because the quiet argument is passed to knitr::knit() by rmarkdown::render().
This leads to a different user experience. That's why I used suppressMessages() and not quiet.

@yihui
Copy link
Member

yihui commented Dec 3, 2019

I wonder what happens if you don't change the quiet argument, but just add another message() in the end. When there are two messages, RStudio might be open the file in the last message. You can test it.

@RLesur
Copy link
Collaborator Author

RLesur commented Dec 3, 2019

You can test it.

I've already tested it 😉 and RStudio IDE opens the file in the first message.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you!

@RLesur RLesur merged commit e242027 into master Dec 3, 2019
@RLesur RLesur deleted the knit_pdf_chrome branch December 3, 2019 21:01
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

Successfully merging this pull request may close these issues.

3 participants