How to hide figures in knitr, but create them as png? - r

I am currently doing some statistical analysis in R and use knitr to generate results and an overview document.
There are some additional plots, which I want to be done and saved as a .png (with specified file name and location), but not included in the generated .html file (too many of them, and they are not at the end).
Using dev.copy(png, ...) works fine for generating the plots, but the figures appear in the .html. If I specify fig.keep=none the .png files are created, but blank.
Is there some way to do what I want?

This is from knitr website:
fig.show: ('asis'; character) how to show/arrange the plots; four
possible values are
asis: show plots exactly in places where they were
generated (as if the code were run in an R terminal)
hold: hold all
plots and output them in the very end of a code chunk
animate: wrap
all plots into an animation if there are mutiple plots in a chunk
hide: generate plot files but hide them in the output document
fig.show = 'hide' worked for me.

Related

How to plot many plots to a pdf and AND a subset to Rmarkdown report?

I need to generate about >50 plots. I want to save them all in one pdf and have just a few of them appear in my R markdown html output.
I can print many plots to a single pdf like this:
pdf("path/to/out.pdf")
for (sam in samples){
plot(sam) # simplified stand-in for many lines of code
}
dev.off()
That puts one sample's plot on each page of a pdf that I can flip through later.
I want some of those plots to appear in my markdown, without duplicating the plot code.
I've tried:
pdf("path/to/out.pdf")
count=0
for (sam in samples){
plot(sam) # simplified stand-in for many lines of code
if (sample(1:10,1)==1){ #randomly select a few examples
count = count + 1
dev.copy(which=dev.prev()) # to revert back to the default device
dev.set(dev.next()) # to resume printing to the same pdf
}
}
dev.off()
With this, the pdf is correct and count is >0, but the plots do not appear in the markdown report.
I think something from the dev.copy() family will do what I want but I can't seem to make it work. In other variants I tried, dev.print() complains about only printing from a screen device, or I get errors saying that I cannot copy to the same device, or cannot copy to a null device.
Similar questions (such as Saving plot as pdf and simultaneously display it in the window (x11)) often want to do the reverse: print to screen a then to a pdf. And they generally only deal with one plot.
I want to make several plots, and I want ALL of my plots to go to the pdf, but only a FEW to go to the Rmarkdown html report as an example of what is in the pdf.
I'm working on a mac using R 3.5.1, RStudio v1.1.456
Thanks in advance!

Distinct colors for R input and output in Rnw chunks - knitr

I'd like to render R input and output code from the same Rnw chunk using two different colors so that they can be more easily interpreted. So far my attempts failed while using a custom CSS template, where any change in colors appear to systematically change both input and output.
Thank you for your time.
F

R export graph to pdf with page labels and import into LaTeX using page labels

I was wondering if it is possible to set page labels in PDFs exported from R and import the PDF page as an image using said labels into LaTeX?
What I want to do is:
export several graphs from R to a single multi-page PDF file
label the pages in the PDF
Idea:
PDF("multipage.pdf")
graph_object_1 (label="bubblechart")
graph_object_2 (label="bargraph")
graph_object_3 (label="scatterplot")
dev.off()
import as graph into LaTeX using the label instead of a page number
\includegraphics[page={bargraph}]{multipage}
I am aware that I can access pages using the respective page number. But it is common that the graphs change over time, more are added, some are removed. And adapting the page numbers in the TeX file every time would be painful.
I know about Sweave and knitR but my co-authors don't use R and they are therefore not an option.
I am wondering why you want to use a single pdf file. Why not
pdf("bubblechart.pdf")
...
dev.off()
pdf("bargraph.pdf")
...
dev.off()
and then
\begin{figure}
\includegraphics{bubblechart}
\end{figure}
and so forth...
LaTeX would do all the numbering and you have maximal flexibility...

R loop through complete script, find all plots generated and save them

I have the following problem using R and not found a solution so far:
I have a script where I run several operation and generate some plots. At the end, I would like to have a nice piece of code that automatically saves all the plots generated into the current working directory. So far, I am using:
trellis.device(device="png", filename="Plot_A.png")
print(Plot_A)
dev.off()
Which is working fine for just one specific plot. Now I am looking for some kind of for loop that takes all the plots and saves them with the name of the plot as a png file
In grid based plotting packages (lattice and ggplot), you can store the plot in an object and call print on them to trigger actual rendering of the plot. What you could do is not render the image on the spot, but append any plots to a list. Then, at the end, you can loop over the plots and output them.
plot_list = list()
lattice_plot = xyplot()
plot_list = append(plot_list, lattice_plot)
for(plot in plot_list) {
png('name.png')
print(plot)
dev.off()
}
Not exactly an answer but an alternative workflow.
If you are saving your plots in order to use it somewhere else, for example to include them in a Word document or in a presentation, you could just put your code in an RMarkdown document and knitr it to generate an html or doc document with all the output generated by the code, including plots. With RStudio all that could be done with a few clicks.
It may even be easier to take all plots from the Word document than from a folder of png files.

have knitr output figures in different formats

Let's say I have a long report that produces a lot of figures which knitr makes as pdfs and it works really well with LaTeX. At the end of the project, my co-authors would like to have also raster based figures. One option would be to convert everything using ImageMagick. Another option would be to specify for each chunk dev = c("jpg", "pdf"), but given the number of figures, this could be cumbersome.
Is there a global switch to make knitr produce figures in pdf and other formats at the same time?
I think in the preamble
opts_chunk$set(dev = c("pdf", "jpg"))
should do. Within a R-chunk of course.

Resources