pandoc error in finding image file - r

Just wanted to insert two images in to the rmarkdown pdf document. When knitting it gives the error
pandoc.exe: Could not find image paste0(Figs,%20%22Fig1.png%22)',
skipping... pandoc.exe: Unable to convert
paste0(Figs,%20%22Fig1.png%22)' for use with pdflatex. ! Missing
\endcsname inserted.
Below is the code
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output: pdf_document
fig_caption: yes
---
```{r, echo=FALSE}
Figs <- 'C:/Users/arvin/Figs/'
```
![Fig1](paste0(Figs, "Fig1.png"))
I used knitr_1.14 and r_markdown_1.0.

It is clear, why your code does not work: you used the function paste outside the R-environment. So pandoc searches for paste0(Figs,%20%22Fig1.png%22) as a filename.
You can't do it in this way you wanna do this. The regular usage is
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output:
pdf_document:
fig_caption: yes
---
![Fig1](C:/Users/arvin/Figs/Fig1.png)
BUT there is another solution, where you have the full control of image size, and you can plot like a regular R plot, using grid.raster from the grid package. And here your approach will work, because we are in a R environment:
```{r fig.width=10, fig.height=10, echo=FALSE}
library(png)
library(grid)
Figs <- 'C:/Users/arvin/Figs/'
img <- readPNG(paste0(Figs, "Fig1.png"))
grid.raster(img)
```

Inline R code should be put in `r `:
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output:
pdf_document
fig_caption: yes
---
```{r, echo=FALSE}
Figs <- 'C:/Users/arvin/Figs/'
```
![Fig1](`r paste0(Figs, "Fig1.png")`)
BTW, an unsolicited pro-tip: Do not use absolute paths. Use relative paths.

Related

RMarkdown is not referencing tables

Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)
My table is in Table \#ref{tab:tab1}
library(knitr)
kable(tab1, caption="my table")
AS we see in Figure \#ref{fig:plot1}
plot(seq(1,10,1))
You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your #ref. In that case it reference to your figures and tables. You can use the following code:
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
My table is in Table \#ref(tab:tab1)
```{r tab1, echo =FALSE}
var1<-sample(LETTERS)
tab1<-table(var1)
library(knitr)
kable(tab1, caption="my table")
```
\newpage
AS we see in Figure \#ref(fig:plot1)
```{r plot1, fig.cap ="plot", echo=FALSE}
par(mar = c(4, 4, .2, .1))
plot(seq(1,10,1))
```
Output:
As you can see on the image, when you click on 1 in will go to your table.

plots after dynamic sections using R Markdown pander, knitr and pandoc

I use this rmd:
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
---
## A function that generates sections
```{r setup, include = FALSE}
library(pander)
create_section <- function() {
# Inserts "## Title (auto)"
pander::pandoc.header('Title (auto)', level = 2)
# Section contents
# e.g. a random plot
plot(sample(1000, 10))
# a list, formatted as Markdown
# adding also empty lines, to be sure that this is valid Markdown
pander::pandoc.p('')
pander::pandoc.list(letters[1:3])
pander::pandoc.p('')
}
```
## Generate sections
```{r, results='asis', echo=FALSE}
n_sections <- 3
for (i in seq(n_sections)) {
create_section()
}
```
and then:
library(knitr);
library(rmarkdown);
setwd("C:/bla")
knit('test_md.Rmd')
rmarkdown::pandoc_convert("test_md.md", to = "pdf", output = "test_pdf.pdf")
This kind of works but the plots are all rendered after the sections:
Each section should contain the plot. Any ideas? Thanks!
PS:
Wrapping:
plot(sample(1000, 10))
in print:
print(plot(sample(1000, 10)))
forces output to be produced. Unfortunately, NULL is also printed underneath the plot.
Could you just add pdf_document in the YAML and then generate the PDF by knitting?
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
output: pdf_document
---
I was able to get the same result you described when running the rmarkdown::pandoc_convert() on the .md file, all the plots at the end of the .PDF file.

R Markdown internal link to tables

Is there a possibility to create an internal link to a table in a rmarkdown-article?
I would like to click on this link and then I automatically come to my table.
I want to set the internal link in the text in the Empirical Results-part for table_1.
I've already tried [#table_1] but it doesn't work.
can someone help me?
Here is my example code:
---
title: "title"
subtitle: "subtitle"
author: "me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
keywords: "keywords"
output:
pdf_document:
fig_cap: yes
keep_tex: yes
documentclass: article
capsize: normalsize
fontsize: 11pt
geometry: margin=1in
spacing: doublespacing
footerdate: yes
abstract: 'Insert abstract here'
---
\newcommand*{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
\keywords{keywords}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
##required packages
library(tidyverse)
library(knitr)
library(kableExtra)
```
# Introduction
# Literature review
# Data and Methodology
# Empirical Results
In table_1 you can see something....
[#table_1]
# Conclusions
\newpage
# References
\newpage
# Appendix
```{r echo=F, warning=F, message=F}
df1 <- tibble(column1= 1:10, column2=11:20)
table_1 <-df1%>%kable("latex",
booktabs=T,
caption = "example table")%>%
kable_styling(latex_options = "hold_position")
table_1
```

Hide comments in R markdown

Is is possible to hide some comments in code when kniting using knitr / R markdown? Example:
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r}
# Generate some data
rnorm(2)
## But keep this comment
```
When kniting I would like the first comment to disapear, but keep the second one somehow.
Here is a quick example of modifying the hook to change knitr behavior.
---
title: "SOSO"
author: "SO"
date: 2017-06-06
output: pdf_document
---
```{r setup-hook, echo=FALSE}
hook_in <- function(x, options) {
x <- x[!grepl("^#\\s+", x)]
paste0("```r\n",
paste0(x, collapse="\n"),
"\n```")
}
knitr::knit_hooks$set(source = hook_in)
```
```{r}
# Generate some data
# Lines that starts with `# ` will be removed from the rendered documents
rnorm(2)
## But keep this comment
## But lines that starts with `## ` will be kept
```
produces this
In fact, you can choose to show any lines of R code by passing numeric indices to the chunk option echo, e.g.
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r echo=4:7}
# Generate some data
rnorm(2)
## But keep this comment
```
See knitr documentation for more information.

improper placement of dynamic figures when fig_caption=true in pdf_document

Knitr misplaces the output graphics when output format is pdf and fig_caption is set to true. Could you reproduce this problem?
---
title: "Untitled"
author: "xxx"
date: "December 19, 2016"
output:
pdf_document:
fig_caption: true
---
# section 1
## sub 1
some text
```{r, fig.cap="caption2"}
plot(10:1)
```
# section 2
the plot draws under section 2.
I have also filed an issue on github repo but no answer yet.

Resources