Convert: no decode delegate errors in Rmarkdown - r

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.

Related

Knitr HTML preview fails in RStudio, though render() successfully creates HTML file

I'm getting started with rmarkdown and knitr. In the sample document provided by RStudio, I can successfully use render() to generate an HTML file that views fine in Chrome. However, when I click on the knit button, it generates a .markdown file and then returns the following error without rendering the preview:
Error generating HTML preview for ~/path/to/file/report.rmarkdown system error 2 (The system cannot find the file specified)
I think it's getting hung up at the pandoc stage. Is it possible that RStudio is looking for pandoc in the wrong place? Pandoc was already installed at C:\Program Files (x86)\Pandoc\pandoc.exe, but RStudio installed its own instance at C:\Program Files\RStudio\bin\pandoc\pandoc.exe, so maybe it's looking in the wrong place and/or confusing settings from one with the other?
Any help would be greatly appreciated. Thanks!
And just in case, here's the RMarkdown template I'm starting with:
---
title: "Monthly Report"
author: "Kris Shaffer"
date: "February 17, 2017"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
```
## 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 <http://rmarkdown.rstudio.com>.
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:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
R version 3.3.2
RStudio 1.0.136
rmarkdown 1.3
knitr 1.15.1
pandoc 1.17.2 (both installations)
Turns out it was as simple as a file extension. I changed the filename from report.rmarkdown to report.Rmd. Knit works fine now.

R Markdown - no ODT and LaTeX options as an output

I found R markdown/knitr useful tool to document my work and generate summary document.
I work with .Rmd (R markdown) files in RStudio.
It seems that knitr provide appropriate functionality to generate .odt (Open Document Text) and .tex (LaTeX) documents from .Rmd.
However, R studio allows to choose .docx, .html and .pdf formats only.
I would like to avoid MS Word format since I prefer open standards and working under Linux.
Is it possible to add .odt and .tex options to Rstudio menu?
It doesn't seem possible to output odt directly in RStudio, but you can always use knitr::knit to produce a markdown document and pandoc to produce the odt:
library(knitr)
knit("myDoc.Rmd")
system("pandoc myDoc.md -o myDoc.odt")
You may have to adjust the pandoc options and adapt the template to get a nice looking result.
As for latex, you can keep the tex sources when compiling to pdf with the following option in your yaml front matter:
---
output:
pdf_document:
keep_tex: true
---

Concatenating mutliple .md files into webpage with knitr?

I have 6 files (for which I locally have the .Rmd code) posted to http://rpubs.com/sshleifer using knitr.
I want to create one webpage that concatenates all these webpages into one webpage, without copying and pasting the .Rmd code into one document (it will crash Rstudio).
I have tried making a new RMD file containing the chunk:
```{r learning, echo=FALSE, warning=FALSE, results='asis', message=FALSE,cache=TRUE}
knit('learning.Rmd')
```
but this only displays the progress of the knitting of Learning.Rmd at the new location, rather than the actual html created.
Thanks!

Compile R Script to markdown

Using RStudio's "Compile Notebook to html" feature, I noticed that a temporary .md file was created in the process, but deleted automatically. At one point I was lucky enough to see its content, and it is exactly what I need: the code chunks alternate with output chunks, all perfectly formatted.
So my question is: how do I generate such an .md file directly form an R script?
You could also run knitr::spin() directly from the R console.
If you run "Knit to HTML", a markdown file is temporarily created and deleted. To keep the markdown file, add the following to the YAML code at the top of the RMD file.
---
output:
html_document:
keep_md: true
---
Here is a helpful reference: https://bookdown.org/yihui/rmarkdown/html-document.html#keeping-markdown

trouble finding file source in .rmd chunk when knitting .rmd from master .R file

Let's say I have a project directory called testknit (and I do, see github for MRE), and inside this I have several subdirectories, including scripts where I keep .R and .rmd files.
In RStudio, I create a project and select this testknit directory so that when I open the project, the working directory is mypath/testknit.
Inside testknit/scripts I have a master.R file. If I want to source a file called testsource1.R, which is also in testknit/scripts, I can run source("scripts/testsource1.R") from within master.R.
library(knitr)
getwd()
# [1] "mypath/testknit"
source("scripts/testsource1.R")
So far so good.
But let's say I also want to knit a .rmd file called test.rmd that is located in testknit/scripts. I can run knit("scripts/test.rmd") from master.R.
My test.rmd file does the following:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
```{r option1}
source("scripts/testsource2.R")
```
```{r option2}
source("testsource2.R")
```
Since test.rmd exists within testknit/scripts, I specify opts_knit$set(root.dir='../') in the first chunk so knitr knows that my root directory is really one level up.
When I open test.rmd in RStudio and click knit HTML, predictably, the option1 chunk works and the option2 chunk does not.
But when I try to knit test.rmd by running knit("scripts/test.rmd") from master.R instead of knitting from within the .rmd file, neither chunk option works. Both return an error that there is no file by that name.
What am I doing wrong? Why can't R find testsource2.R when knitting the .rmd file from the master .R?
See github link above for reproducible example.
Update:
As I noted below in the comments, I tried adding wd <- getwd() just before opts_knit$set and changed (root.dir='../') to (root.dir=wd). So when I run knit("scripts/test.rmd") from master.R, the option2 chunk runs because the wd I added gets set to mypath/testknit/scripts. But if i open the .rmd file and run all chunks, wd is set to the root directory, mypath/testknit, and the option1 chunk runs.
I need the working directory to remain the project root. This does not seem like an elegant solution to me, but changing:
```{r setup}
library(knitr)
opts_knit$set(root.dir='../')
```
to
```{r setup}
library(knitr)
wd <- ifelse(basename(getwd())=="scripts",
gsub("/scripts", "", getwd()),
getwd())
opts_knit$set(root.dir=wd)
```
lets me run all chunks when in the .rmd file or knit("scripts/test.rmd") from master.R. It works, but it feels like I am taking the wrong approach.
#Yihui: Perhaps you can make ../ an absolute path using normalizePath('../'). A relative working directory can be confusing to reason about (at least my head hurts after I read too many levels of relative paths :). BTW, when you Knit HTML in RStudio, RStudio first changes the working directory to the input Rmd file.
Me: yes! using just opts_knit$set(root.dir=normalizePath('../')) works for knitting the .rmd file from master.R and knitting to html or running all chunks from within the .rmd. I updated the github example. test-b.rmd now shows this. thanks!

Resources