knit to pdf in emacs using polymode - r

I am trying to knit my Rnw file into PDF. With ess-noweb mode, I could knit and view the pdf but with polymode, I could knit to tex with name filename[woven].tex but could not view the pdf. Do I need to open the tex file again and compile it or is there any direct way?

Use M-n-e to export Rnw to PDF in polymode. It will take care of both weaving and latexing, and opens up the PDF for you.

Related

.rmarkdown file, blogdown and in window preview for RStudio

I have been using .Rmarkdown files to create blog posts in R blogdown. The output of chunk of codes in .Rmarkdown documents are printed in the console and not in the document.
If instead I create a .rmd file, then previews are in window. RStudio Global options are set to show preview in window for RMarkdown documents.
Is this an expected behavior of .rmarkdown files?
Thanks
The differences between .Rmarkdown and .Rmd is explained here. The most relevant quote being:
In this book, we usually mean .Rmd files when we say “R Markdown documents,” which are compiled to .html by default. However, there is another type of R Markdown document with the filename extension .Rmarkdown. Such R Markdown documents are compiled to Markdown documents with the extension .markdown, which will be processed by Hugo instead of Pandoc.
More specific differences are explained in the book linked above.

Insert GIF in pdf using rmarkdown

I am using rmarkdown to generate both HTML and pdf file. In my .Rmd file, I included a GIF like this:
![](www/script.gif)
When I knit the to HTML it works fine.
rmarkdown::render(documentation_file, encoding="UTF-8")
However, when I try to knit to PDF using
rmarkdown::render(documentation_file, rmarkdown::pdf_document(latex_engine = "xelatex"), encoding="UTF-8")
I have the following problem:
! LaTeX Error: Unknown graphics extension: .gif.
I do not mind to lose the animation of the gif, a static version of it is perfectly fine.
Is there any easy way to include/convert on the fly the GIF to my PDF document?
It's not possible to directly include GIFs in a LaTeX document.
In general LaTeX, you can only include GIFs if you use latex to compile your document; when using pdflatex, xelatex and lualatex you need to manually convert your figure to e.g. PNG, JPG or PDF.
RMarkdown by default uses pdflatex; while you may change the LaTeX engine by specifying e.g. latex_engine: xelatex below pdf_document in the YAML header of your document, it is not possible to use latex to compile (latex would first create a DVI file, which is then converted to a PS and then in turn to a PDF).
So the easiest (and only) solution would be to convert all GIF figures to PNGs (or JPGs), and then include them as images in your RMarkdown document.
Try this in your chunk
if (knitr:::is_latex_output()) {
knitr::asis_output('\\url{....}')
} else {
knitr::include_graphics("script.gif")
}
And in the url put the url to your gif
You could create a snapshot image of the first frame of the gif, https://stackoverflow.com/a/12554723/10346727
I don't think PDF supports gif format, or any moving format for that matter.

Convert: no decode delegate errors in Rmarkdown

After updating my R installation on my Mac with a couple of packages (most recently, RODBC), when I knit .Rmd files to pdf, I get error such as the one below.
convert: no decode delegate for this image format `images.trial/pressure-1.png'.
convert: missing an image filename
It occurs on .Rmd files that I've written on a Windows machine, and on a standard .Rmd file that I created from the base template (File>New File>R markdown...), with an initial r chunk that I've used to set knitr chunk options:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,fig.path="images/",dev="png")
```
If I exclude that knitr::opts_chunk code when I knit to pdf, I get no errors; if I knit to html with that knitr:opts_chunk intact, I get no errors.
I've run this on .Rmd files that have worked without errors in the past and on .Rmd files that were created directly from the template.

How to save R source code to a PDF

So I have some R source code, how do I save that source code into a PDF. I am able to save the markdown output as PDF through knitr. But how do I save the code itself onto a PDF?
Use echo = TRUE in your R chunk

Jump to position in .Rnw file in PDF produced by knitr

I'm using knitr to convert .Rnw files to .pdf files. I can use Skim to jump from a position in the .pdf file to the .tex file produced by knitr.
How can I jump from a position in the .pdf file to the .Rnw?
Check out SyncTeX. For example, RStudio's PDF viewer provides a synchronization between the PDF and the underlying Rnw file, see here or here.

Resources