improper placement of dynamic figures when fig_caption=true in pdf_document - r

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.

Related

Run shiny document with table of content

When I add the following lines to add table of content to a shiny document:
toc: true
toc_float: true
The document fails to run. It runs fine without them. I get the following error:
Error Scanner error: mapping values are not allowed in this context at line 5, column 8 (line 5 is toc: true)
How can I resolve this issue?
The code I ran:
---
title: "Untitled"
runtime: shiny
output:
html_document
toc: true
toc_float: true
---
### section 1
```{r, echo=FALSE}
sliderInput("slider", "Number of observations:", 1, 100, 50)
renderText({paste0(rep('blah', 100), collapse = '\n')})
```
### section 2
```{r, echo=FALSE}
renderPlot({hist(rnorm(500)[seq_len(input$slider)])})
```
You're just missing a : from html_document. It should be html_document:
---
title: "Untitled"
runtime: shiny
output:
html_document:
toc: true
toc_float: true
---

Table of contents under a tab in R Markdown

I am trying to make an R Markdown doc with tabs and a table of contents under each tab
Here is my header:
---
title: "Palliative/Comfort Care/Hospice Patients Report"
output:
html_document:
toc: true
toc_floating: true
theme: "cerulean"
date: "`r format(Sys.time(), '%B %d, %Y')`"
pdf_document: default
---
Followed by:
# {.tabset}
## tab1
## tab 2

R Markdown index for titles

I have seen people that are able to create an index based on the titles that you have on your R markdown document like in this picture
If somebody could let me know how would I have to modify this code so it creates that left index table?
---
title: "Untitled"
author: "Juan Lozano"
date: "October 19, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title 1
Text Text Text Text Text
```{r cars}
summary(cars)
```
## Title 2
Text Text Text Text Text
As #hrbrmstr 's link indicates there are many YAML customisations possible:
This is the sort of thing I usually use:
---
title: "Untitled"
author: "john Smith"
date: "today ;)"
output:
html_notebook:
fig_caption: yes
number_sections: yes
toc: yes
toc_float: yes
html_document:
fig_caption: yes
number_sections: yes
toc: yes
toc_float: yes
df_print: paged
bibliography: /path/to/library.bib
---

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.

pandoc error in finding image file

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.

Resources