reference knitr chunk in R markdown - r

I'm using knitr to embed R code within a markdown (.Rmd) file. I'm converting the file to a PDF file with Pandoc. The contents of my .Rmd file looks like this:
Report
========================================================
This is my report. Some analysis is included in LINK TO CHUNK NAMED MY.ANALYSIS HERE
```{r my.analysis, echo=FALSE}
summary(cars)
```
Where it says LINK TO CHUNK NAMED MY.ANALYSIS HERE, am I able provide a link to the code chunk named my.analysis in the outputted PDF?
I believe the OP asked a similar, but slightly different question here: figure captions, references using knitr and markdown to html

Do this:
Report
========================================================
This is my report. Some analysis is included in \href{http://stackoverflow.com/q/16445247/1000343}{LINK TO CHUNK NAMED MY.ANALYSIS HERE}
```{r my.analysis, echo=FALSE}
summary(cars)
```
And then covert the markdown file (not Rmd) to the PDF.
This also works using the devel version of reports:
```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=TRUE)
library(reports); library(knitr);
```
Report
========================================================
This is my report. Some analysis is included in `r HR("http://stackoverflow.com/q/16445247/1000343", "LINK TO CHUNK NAMED MY.ANALYSIS HERE")`
```{r my.analysis, echo=FALSE}
summary(cars)
```
You then convert the html file to pdf.

Related

Erro R "Error: '\U' used without hex digits in character string starting ""C:\U"

When starting RStudio, this error appears. I can run several commands, but I can't compile anything in Rmarkdown. Does anyone know how I can fix it?
The error does not appear when typing a path to read from the file. It even appears when I try to compile an empty file in RMarkdown.
The error even appears with the default example script in RMarkdown.
---
title: "Untitled"
author: "Autor"
output: pdf_document
---
```{r setup, include=FALSE}
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.

RMarkdown not render correctly

I tried to create a RMarkdown file, with the default template by RStudio like below:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
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.
When I clicked knit, it only rendered a raw html file like below, as if no css is applied to the html.
How to fix it?

R Markdown excessive white space PDF output

Does anybody know why R markdown generates excessive white space above each plot and how can I fix that ? Is there a knitr option to be included ? Or any chunk option maybe ?
I've provided 3 images at the bottom of this post so that you can see what I mean.
Haven't used any other chunk option than echo, warning and message and about the plot it is a basic ggplot.
Let me know in the comments if I need to provide any code example of my chunks for a better view.
Edit: here's a simple rmarkdown file generating the same excessive white space above ggplots.
---
title: "Untitled"
author: "Razvan Cretu"
date: "January 9, 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r import, echo=FALSE}
library('ggplot2')
```
## 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}
ggplot(cars, aes(speed, dist))+
geom_line()
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Rmarkdown: knit PDF to look exactly like HTML

When declaring output format in YAML section of my Rmarkdown document, is there a way to make sure it knits to PDF, but looks like a knitted HTML?
Nothing about knitted PDF looks the same: the font is some flavor of serif (assume I can change that), blockquotes and code chunks look different.
I like knitted HTML and the look, but is there a way to generate a PDF looking exactly the same?
As an example, just a simple default sample code:
---
title: "Untitled"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
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>.
> This is a quote
This is a link: <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)
```
Here are 2 samples how 2 outputs look out of the box

Setting knitr to output separate images

I'm trying to get the PNG plots from knitr output to write to disk as separate files without doing it manually.
What I tried
The dev = 'png' setting from http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/ (also mentioned in this question)
self_contained: no from knitr: include figures in report *and* output figures to separate files.
Neither worked. The folder the knitting process ran in has no extra files, and the HTML document has base64 embedded images in its source.
Environment
RStudio 0.99.903
R 3.2.3
knitr 1.15.1
MWE: The RStudio RMarkdown file, with abovementioned options added:
---
title: "Untitled"
output: html_document
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(dev="png",
dev.args=list(type="cairo"),
dpi=96)
```
## 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.
self_contained is an option for html_document, not a top-level YAML setting. The document below works using just that. PNG is the default figure type, so you don't need to specify that.
---
title: "Untitled"
output:
html_document:
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```

Resources