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

Improve knitr::spin rendering with .Rmd files #2281

Closed
3 tasks done
kylebutts opened this issue Aug 22, 2023 · 7 comments
Closed
3 tasks done

Improve knitr::spin rendering with .Rmd files #2281

kylebutts opened this issue Aug 22, 2023 · 7 comments
Assignees
Labels
question Questions (which should belong to forums instead of Github)

Comments

@kylebutts
Copy link
Contributor

I recently discovered knitr::spin which I agree is knitr's best hidden gem!

The current version uses knit2html which (1) raises warning that rmarkdown::render should be used and (2) ignores the output yaml option:

knitr/R/spin.R

Lines 119 to 121 in 50e28da

if (format == 'Rmd') {
knit2html(outsrc, text = txt, envir = envir)
} else if (!is.null(outsrc) && (format %in% c('Rnw', 'Rtex'))) {

A proposed solution that works with both text and hair options

if (format == 'Rmd') {
  if (is.null(outsrc)) {
    knit2html(outsrc, text = txt, envir = envir) 
  } else {
    rmarkdown::render(outsrc, envir = envir)
  }
} else if (!is.null(outsrc) && (format %in% c('Rnw', 'Rtex'))) { 

Would you welcome a PR adding this change?


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@cderv
Copy link
Collaborator

cderv commented Aug 22, 2023

Documentation of the function says

knitr/R/spin.R

Lines 44 to 47 in 50e28da

#' When the output format is Rmd, it is compiled to HTML via
#' \code{\link{knit2html}()}, which uses R Markdown v1 instead of v2. If you
#' want to use the latter, you should call
#' \code{rmarkdown::\link[rmarkdown]{render}()} instead.

Try using rmarkdown::render() on the .R file directly. See documentation at https://rmarkdown.rstudio.com/docs/reference/compile_notebook.html

@cderv cderv added the question Questions (which should belong to forums instead of Github) label Aug 22, 2023
@kylebutts
Copy link
Contributor Author

Thanks @cderv. I hadn't seen that in the documentation but pieced it together. Why not have the default be to use render which seems more robust (using v2) and lets you specify output format?

@cderv
Copy link
Collaborator

cderv commented Aug 22, 2023

I'll let @yihui answer to that.

@kylebutts
Copy link
Contributor Author

I originally found about this function from the R Markdown Cookbook: https://bookdown.org/yihui/rmarkdown-cookbook/spin.html

This example would suggest you get a pdf document out, but you get an html

@cderv
Copy link
Collaborator

cderv commented Aug 22, 2023

Thanks for the context, this is indeed confusing...

@yihui as anything changed since we wrote the R Markdown cookbook ?

rmarkdown::render() on R file seems the way to spin and create the report with Rmd format. Should knitr::spin() offer to do rmarkdown::render() if a Rmd file with R Markdown V2 is detected ?

Otherwise, the cookbook should be updated 🤔

@yihui
Copy link
Owner

yihui commented Aug 22, 2023

Why not have the default be to use render which seems more robust (using v2) and lets you specify output format?

@kylebutts knitr::spin() was introduced before the rmarkdown package was born, so it uses knitr::knit2html() instead of rmarkdown::render() mainly for historical reasons.

This example would suggest you get a pdf document out, but you get an html

We need to clarify in the book that the R script is supposed to be rendered via rmarkdown::render() instead of knitr::spin() (spin() is only an intermediate step). Thanks!

Should knitr::spin() offer to do rmarkdown::render() if a Rmd file with R Markdown V2 is detected ?

@cderv That's probably a good idea. Anyway, the cookbook definitely needs a bit more clarification.

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 Feb 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Questions (which should belong to forums instead of Github)
Projects
None yet
Development

No branches or pull requests

3 participants