How do I tell to knitr to use theme_classic if I export my Rmd notebook to PDF and use dark_theme_gray from ggdark package if I export my Rmd notebook to HTML?
Try this. (Sorry. I had no ggdark installed. So I just used theme_gray for HTML output). knitr provides helper functions to check for is_html_output or is_latex_output.
---
title: "test"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Plot
```{r}
library(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point()
if (knitr::is_html_output()) {
p + theme_gray()
} else if (knitr::is_latex_output()) {
p + theme_classic()
}
```
Related
My R Markdown files contain code to save PDF files e.g.
pdf(paste0("plots/filtering/", exp_name, "_umap.pdf"))
walk(list(p4, p5, p6, p7), print)
dev.off()
When I knit to HTML, I get the following printed as part of the HTML.
## quartz_off_screen
## 2
Is there a way to prevent this? I already have the knitr chunk options message and warning set to FALSE.
Best wishes,
Lucy
Wrap the the call to dev.off with invisible(...).
MWE
---
title: "Test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(purrr)
```
## R Markdown
```{r}
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, disp, color = factor(vs))) + geom_point()
```
```{r}
pdf("mtcars.pdf")
walk(list(p1, p2), print)
invisible(dev.off())
```
I'm trying to link figures throughout my R Markdown file and I keep getting the error when I try to knit the profile:
[WARNING] Citeproc: citation Picture not found
Further, when I try to cite using #chunk-name, the output produces a result such as chunk-name? with a question mark.
I have tried to solve this error by downloading pandoc due to some internet searching but absolutely nothing has helped me. Anything regarding this error message would be helpful.
I have attached a code of my YAML if it's of any help:
Apologies I don't know how to attach it as a code without the lines coalescing.
Example of GG Plot I was trying to create:
```{r Picture , fig.asp=0.5, out.width= "100%", warning=FALSE, message=FALSE, fig.cap= " Student's grade count vs planning "}
library(ggplot2)
ggplot(qn1frame, aes(x=(hourly_plan), fill=category
)) + geom_bar() + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") +
labs(fill = "Grades", y = "Proportion", x = "ewuhrwe") + plot_layout(guides = 'collect') +
plot_annotation(tag_levels = 'A')
ggplot(qn1frame, aes(x=(hourly_plan), fill=category
)) + geom_bar(aes(stat="identity"), position= "fill") + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") +
labs(fill = "Grades", y = "Proportion", x = "Occupational exposure (yrs)") + plot_layout(guides = 'collect') + plot_annotation(tag_levels = 'A')
```
How I've been trying to create a citation back to this graph:
Testing captions:
Referring to #Picture..
However, I get the WARNING output when I render the file as well as in the actual code I get: (Picture?) when I tag it. Any help would be immensely helpful.
Thank you!
You are having the question mark sign ?? instead of a reference because you are trying to refer from a code chunk that generates two figures. So when you use `#ref(fig:chunk-name) to refer to the figures, it gets confused as to which figure to refer to.
So one option could be using a separate chunk for each plot. Or if you want to generate multiple plots from the same chunk, you need to refer them with \#ref(fig:chunk-name-1), \#ref(fig:chunk-name-2) etc.
Reproducible Example
---
title: "Cross Referencing"
output:
bookdown::html_document2:
self_contained: yes
code_folding: hide
code_download: yes
toc: yes
toc_float: yes
number_sections: yes
fig_caption: TRUE
link-citations: true
link-references: true
date: "2022-09-16"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```
## R Markdown
```{r}
#| cars-plot,
#| fig.cap=c("Displacement vs Miles per gallon", "HP vs Miles per gallon"),
#| echo=FALSE,
#| fig.height=3,
#| fig.width=4
ggplot(mtcars, aes(mpg, disp)) +
geom_point()
ggplot(mtcars, aes(mpg, hp)) +
geom_point()
```
Testing captions:
Referring to \#ref(fig:cars-plot-1) and \#ref(fig:cars-plot-2)
Is there a way of make it so that knitr caches stuff so that last_plot() works properly? For example this document:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(ggplot2)
```
```{r cars}
ggplot(mtcars, aes(hp, mpg)) +
geom_point()
```
The first value is `r last_plot()$data$mpg[1]`
The first time is generated, it will have one figure and the text "The first value is 21". But the second time, the last inline code will fail.
Using R markdown with PDF output, I want to cite a figure into another figure caption. I also want to cite a BibTex reference in the caption. Any ideas? Here's an example of code:
---
title: "Untitled"
author: "me"
date: "today"
output:
pdf_document:
latex_engine: lualatex
number_sections: no
linestretch: 1.5
bibliography: input/Library.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r}
df <- mtcars
library(ggplot2)
```
```{r, fig.cap="some stuff"}
ggplot(df, aes(cyl, mpg)) + geom_point()
```
```{r, fig.cap="some more stuff. here I'd like to cite figure 1. I would also like a BibTex citation"}
ggplot(df, aes(cyl, hp)) + geom_point()
```
In my experience cross-referencing works better when one uses bookdown::pdf_document2 or bookdown::html_document2. Note that it makes sense to name the chunk that produces the figure since that name is used in the label used for referencing:
---
title: "Untitled"
author: "me"
date: "today"
output:
bookdown::pdf_document2:
latex_engine: lualatex
number_sections: no
bookdown::html_document2:
default
linestretch: 1.5
bibliography: packages.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
knitr::write_bib(c(.packages()), 'packages.bib')
```
```{r}
df <- mtcars
library(ggplot2)
```
```{r stuff, fig.cap="some stuff"}
ggplot(df, aes(cyl, mpg)) + geom_point()
```
```{r, fig.cap="some more stuff. here I'd like to cite figure \\#ref(fig:stuff). I would also like a BibTex citation [#R-base]"}
ggplot(df, aes(cyl, hp)) + geom_point()
```
For the BibTeX reference I am using an automatically created one, but the adaption to your case should be obvious.
In a knitr document, I am calling an external code chunk which generates a ggplot2 object similar to the following:
# file.R
# ---- create_plot ----
library(ggplot2)
data(mtcars)
gg <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
gg
In my document (Flexdashboard Rmd, specifically):
# dashboard.Rmd
```{r setup}
knitr::read_chunk("plot.R")
```
```{r create_plot}
```
I would like to call this chunk and suppress the printing of the gg object, allowing me to do some tweaks to the object (title, colors, etc.) and display the plot later in my document. I've tried results='hide' when calling the chunk with no success. My desired document would be something like:
# dashboard.Rmd
```{r setup}
knitr::read_chunk("plot.R")
```
```{r create_plot, results='hide'}
```
```{r display_plot}
gg <- gg + labs(title = "Custom title")
gg
```
Is this possible without editing the external chunk to omit the final gg call?
You'll need to include = FALSE in the chunk option. This way, the code will run but not be included. I often do this when I run batch code using source(). Anyhow, give this a try:
```{r display_plot, include = FALSE}
gg <- gg + labs(title = "Custom title")
gg
```