So.
I want to insert some image files into an Rmarkdown document, auto-generate labels and be able to reference those images from elsewhere in the text. I'm using bookdown, so I start off with
---
output:
html_document:
toc: true
number_sections: true
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(bookdown)
```
According to the bookdown manual if I have the following code chunk:
```{r knnPlot, echo=FALSE, fig.cap="Knn Plot"}
knitr::include_graphics("knn-cs3-gs2.png")
```
then I should have a label fig:knnPlot automatically generated, because using fig.cap apparently puts R in a figure environment and automatically prepend "fig" to the label. It then tells me I should be able to reference the figure using \#ref(label), or rather, in this case, \#ref(fig:knnPlot)
When I do this however, the text "\#ref(fig:knnPlot)" is output, rather than an actual cross reference. There's no figure label, no numbering. How is this meant to work?
I can't get the hard coding method suggested here to work. Nor can I get the only other option I can find to work, it tells me to install pandoc-crossreference, which leads me down an absurd rabbit hole of installing haskell of all things along with endless dependencies and obscure pointless error messages which lead to spectacularly unhelpful developer forums filled with 6 pages of error logs.
You are not creating a bookdown document. Use bookdown::html_document2 instead:
---
title: "Bookdown"
output:
bookdown::html_document2:
fig_caption: yes
number_sections: yes
toc: yes
---
```{r Doge, echo=FALSE, fig.cap="Mighty Doge"}
knitr::include_graphics("unnamed.png")
```
Check out this picture: \#ref(fig:Doge)
Related
I am using the bookdown package to produce a large document with tables using the knitr::kable function. I can get the document to produce tables in HTML perfectly, but in PDF, the table contents just appear as a list of numbers. This is also true for the output to Word. The compiled LaTeX just appears to be a list of numbers as well.
This is also true of output from other packages like sjplot.
The same problem also appears when the sample code from the bookdown book.
knitr::kable(
list(
head(iris[, 1:2], 3),
head(mtcars[, 1:3], 5)
),
caption = 'A Tale of Two Tables.', booktabs = TRUE
)
Produces the expected output in HTML:
But, produces the following in PDF:
The YAML header in index.rmd are:
#output ~~~~~~~~~~~ [see _output.yml]
site: bookdown::bookdown_site
output: [bookdown::gitbook, bookdown::pdf_book, bookdown::word_document2, bookdown::html_document2]
documentclass: book
The output settings in the _output.yml are:
bookdown::pdf_book:
keep_tex: true
latex_engine: xelatex
includes:
in_header: tex/biblio.tex
number_sections: yes
pandoc_args: ["--top-level-division=chapter"]
It turns out the problem arises from some interaction with the kableExtra package
So, even if you specify (as I did in my sample code) the knitr namespace (knitr::kable), there is still some downstream interference if kableExtra is loaded. So, this will not work when rendering to PDF:
library(kableExtra, warn.conflicts = TRUE)
knitr::kable(head(iris, 20), caption = 'Here is a nice table!', booktabs = TRUE)
While everything works for the HTML output, the rendering to PDF does not work for the tables and you just end up with a list of the table cell values (for both PDF and Word) - as shown in the image above.
To solve, remove any library statements
Then, ensure you unload the package:
detach("package:kableExtra", unload = TRUE)
And, finally, for me, I found that I needed to "Restart R and Clear All Outputs" or "Terminate R" from the R-Studio "Session" menu.
I'd like to have the section titles in the header not the body of the page. \fancyhead[C]...puts the section title in the header but how do you suppress the body printing of the section?
---
title: "Untitled"
classoption: landscape
output:
pdf_document:
number_sections: false
dev: pdf
keep_tex: false
toc: yes
header-includes:
- \usepackage{fancyhdr}
- \usepackage{etoolbox}
- \pagestyle{fancy}
- \fancyhead[L]{left}
- \fancyhead[R]{right}
- \fancyhead[C]{ \nouppercase{\leftmark} }
---
```{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)
```
\newpage
# 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.
\newpage
# page 3
You can also embed plots, for example:
```{r pressured, 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.
I have a summary figure that I want to insert at the top of my HTML report generated using knitr/rmarkdown. But the inputs of this figure depend on outputs generated from downstream code chunks and child documents. Is there a way to do this without having to put the summary figure at the end of the document?
I have tried ref.label='last' option in the top level code chunk (with eval=FALSE) to add a duplicate code chunk in the end of the document {r last, eval=TRUE}, but has not worked for me.
Any suggestions?
You can insert HTML anywhere into your document referencing the figure by the name knitr will use for it.
To figure out the name, temporarily change the YAML header to something like this:
output:
html_document:
self_contained: FALSE
This won't embed figures in the output, they'll be kept in a separate directory. Open the output in an editor, or view source in a browser, and look for the line that displays the figure. In my test example below, it was being displayed by this code:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
I copied that line into the document, and removed the self_contained setting, and got this source code:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This is the figure before it was generated:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
And here it is being generated:
```{r rnormplot}
x <- rnorm(100)
plot(x)
```
One key is to name the code chunk that generates the plot (mine is named rnormplot) because that helps to make sure the figure name is independent of the rest of the document.
I try to get Highouts Output in my Powerpoint presentation, but the suggestion does not work .This feature is still pretty new and I was not able to find something valueable in the docu.
---
title: "Untitled"
output:
beamer_presentation: default
powerpoint_presentation: default
ioslides_presentation: default
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(highcharter)
```
### R Markdown
This is an R Markdown presentation. 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.
### Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
### Slide with R Output
```{r cars, echo = TRUE}
highchart() %>%
hc_add_series(data = mtcars, type = "bar", hcaes(y = cyl))
```
### Slide with Plot
```{r pressure}
plot(pressure)
```
Produces:
Functions that produce HTML output found in document targeting pptx output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
I added the always_allow_html: yes part to the top, but it still does not work. Can anyone help me?
I'm seeing different behaviour in an R Markdown (Rmd) file depending on how I reference the image I'd like to embed in an HTML document. In the simple example below, the second image is embedded in the document, but the first (using the R chunk) is not.
---
title: "title"
output:
html_document:
mode: selfcontained
theme: null
---
```{r packages, echo=FALSE}
library(htmltools)
```
```{r imgTest, echo=FALSE}
img(src = "http://placehold.it/350x150")
```
<img src="http://placehold.it/350x150">
This is the output in the HTML (for the relevant bit):
<p><img src="http://placehold.it/350x150"/></p>
<p><img src="data:image/png;base64,iVBORw0KG<SNIPPED>"></p>
In summary, using the htmltools function img() within a code chunk does not embed the image but instead it remains a link.
For various reasons, I need the document to be truly self-contained (no links) and to also avoid raw HTML.
Can anyone explain why this is happening and offer a solution? I've tried a variety of chunk options without success so far.