Rmarkdown knits fine with Rstudio button, but not knit() command

27 views
Skip to first unread message

Aliaksei Holik

unread,
Mar 21, 2019, 6:09:43 PM3/21/19
to knitr
Hi everyone,

This has been puzzling me for a few days and I can't seem to figure it out.

I've recently had a need to compile an Rmarkdown document programmatically (as part of an R script) as opposed to my usual way of hitting a command+shift+K in Rstudio. If I use a default Rmd templare generated by Rstudio and run the following:

require(knitr)
render_html
()
knit
(input = "test.Rmd", output = "test.html")

The document compiles without error, but the front matter is not processed and neither is markdown. This is what I get:

--- title: "test" author: "Aliaksei Holik for KYN" date: "21/03/2019" output: html_document --- ## R Markdown This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

I've managed to find a workaround by prefixing the code with %\VignetteEngine{knitr::knitr} and running:

rmarkdown::render(input = "test.Rmd", output_format = "html_document", output_file = "test.Rmd")

But this just ignores the front matter completely. I.e. I get a perfectly formatted html document, but the title author information and date are missing along with the TOC.

So I wonder if there's something blindingly obvious that I'm missing or doing wrong.

Many thanks,

Aliaksei.


Aliaksei Holik

unread,
Mar 21, 2019, 6:12:44 PM3/21/19
to knitr
Here's partial output of my sessionInfo():
> sessionInfo()
R version
3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6


attached
base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    


other attached packages
:
[1] rmarkdown_1.11 knitr_1.21    



Rlastra .

unread,
Mar 22, 2019, 10:45:40 AM3/22/19
to Aliaksei Holik, knitr
Hi, i have an answer.

Just check de Doc for  "Knitr" and there in some paragraph says:

"First knitr will try to decide the pattern list based on the filename extension of the input document, e.g. Rnw files use the list apat$rnw, tex uses the list apat$tex, brew uses apat$brew and HTML files use apat$html; for unkown extensions, the content of the input document is matched against all pattern lists to automatically determine which pattern list is being used. You can also manually set the pattern list using the knit_patterns object or the pat_rnw series functions in advance and knitr will respect the setting."

So, Knitr try to render the document, not the "rmarkdown".

So, just CODE looks like this:

knitr::knit(input = "test.Rmd", output = "test.html") 

And it works!!!

image.png

--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aliaksei Holik

unread,
Mar 22, 2019, 12:53:30 PM3/22/19
to Rlastra ., knitr
Thank you Rlastra,

I'm not sure what change you are proposing. The only difference I can see is that you prepend knit with the library name (knitr::). Unfortunately, in my hands it still doesn't work.

I've looked into the excerpt from the documentation you mentioned and it refers to the patterns used to detect and extract R code from the file. I'd like to note that the R code gets rendered just fine. What doesn't render is the front matter (e.g. title) and markdown. I.e. in the resulting html I get **Knitr** instead of Knitr, etc.

I've now tried changing the file extension to md, which is one of the in-built patterns (Rmd isn't, I guess that's what you were saying). But I get the same output as when using Rmd extension.

I wonder if it needs to be rendered step-by-step. I.e. render Rmd to LaTeX and  LaTeX to html, or similar. I've tried to play with that idea, but unsuccessfully.

Thanks again,

Aliaksei.

Reply all
Reply to author
Forward
0 new messages