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())
```
Related
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)
I have a few ggplot2 plots stored in a named list, plt_list, and I would like to display the plots in R or Rmarkdown, without the names or list indices (e.g. [[1]]) to be displayed; just the plots. I have tried unname(plt_list), but the indices are printed into the console (or in Rmarkdown document, before each plot). With invisible nothing is displayed. Is there any way to do this in R?
We can use walk from purrr to display in Rmarkdown
---
title: "Title"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r plot_create, echo = FALSE}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(purrr))
p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_col()
plt_lst <- list(p1 = p1, p2 = p1, p3 = p1)
```
```{r plots, echo = FALSE, results = 'asis'}
walk(plt_lst, print)
```
-output
If we are trying this in R console, a for loop should also work
for(i in seq_along(plt_lst)) plt_lst[[i]]
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.
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()
}
```
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
```