Producing ggplots from a loop (and generating the files) without printing any visible output in RMarkdown
<p>I am building a table with a mixture of numbers, text, and plots. I constructed my plots with ggplot, and added them to the table afterwards (please see code below). Because I will (eventually) have many plots, I need to use a loop to efficiently create them all. However, because ggplot seems to require printing to generate image links for each plot, I am unable to use <code>invisible()</code>, and subsequently get the pesky '[<a href="https://i.stack.imgur.com/4rp39.png" rel="nofollow noreferrer">1</a>] [[2]] [[3]]' output at the top of the image below.</p>

<p>How can I compile the document without printing any visible output from ggplot?</p>

<pre><code>```{r score_table, fig.show = "hide", echo = FALSE, fig.height=.75, fig.width=2.5}

#Load libraries
library(knitr)
library(ggplot2)

#Item data
items &lt;- data.frame(text = sapply(1:3, FUN = function(x){
  paste0(sample(x = LETTERS, size = 60, replace = T), collapse = "")}))

#Score data
score_set = replicate(n = 3, expr = {data.frame(other = rep("other", 4),
  score=sample(1:7,4,TRUE))}, simplify = F)

#Plot function
plotgen&lt;-function(score_set,other,score){
  p &lt;- ggplot(score_set, aes(factor(other), score))
  p + geom_violin(fill = "#99CCFF") + coord_flip() + scale_x_discrete(name=NULL) +
    scale_y_continuous(breaks = round(seq(1, 7, by = 1),1), limits = c(1,7), name=NULL) +
    theme(axis.text.y=element_blank(),axis.title.y=element_blank(),axis.ticks.y=elemen    t_blank(),
          panel.grid.major.y = element_line(colour = "black"),
          panel.grid.minor = element_blank(),
          panel.background = element_rect(fill = "white"),
          panel.border = element_rect(colour = "black", fill=NA, size=1)) +
    geom_hline(yintercept=sample(1:7,1,TRUE), size = 1.5, colour = "#334466")
}

#Generate plots
print(lapply(seq_along(score_set), FUN = function(x){plotgen(score_set[[x]],other,score)}))

out &lt;- cbind(row.names(items), as.character(items$text), sprintf("![](%s%s-%s.png)", 
       opts_current$get("fig.path"), opts_current$get("label"), 1:nrow(items)))

#Build table
kable(out, col.names = c("ID", "Text", "Scores"))
```
</code></pre>

<p><a href="https://i.stack.imgur.com/4rp39.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4rp39.png" alt="enter image description here"></a></p>