Unicode characters in plots to use in dynamic reports using R, Sweave and knitr - r

I have a problem properly displaying fonts in plots generated by ggplot2 in LaTeX reports generated by R studio in Sweave using knitr.
At first I was not able to properly generate pdfs with polish fonts but that problem was tackled in this post:
Unicode Characters in ggplot2 PDF Output
In short, the author adviced using Cairo package (in R) to generate plots using ggplot2.
This worked for me - once -> meaning I was able to generate a plot with polish characters, but when I am trying to use it in sweave document to generate LaTeX report using knitr like this:
<<pieniadze_graph,fig=TRUE,echo=FALSE>>=
library(Cairo)
cairo_pdf("TutorialExercisesPart2-pieniadze_graph.pdf")
plot1 <- qplot(expenditure, data = cas) + xlim(0, 8000) +
xlab(expression(paste("Pieniądze wydane na ucznia ($)"))) +
ylab("Liczba szkółńćźżś")
print(plot1)
#
I get an error.:
Running pdflatex on TutorialExercisesPart2.tex...failed
While investigating while that happend -> i found that the file that cairo is soppoused to generate is blank (there is a pdf file of a name given to cairo_pdf but it can not be opened with pdf viewer -> error cannot open text file)
Now one note is necessary: The cairo_pdf function requires file name to be set. So I give the pdf a name, that is required to be used later by the tex file in a format filename-chunk_name.pdf (So much for dynamic reports :P)
So I am not for the cairo_pdf option.
Is there a way to generate proper pdf files without cairo_pdf option?
I was not able to find anything more on this topic without the cairo-pdf...
When I delete the cairo part my tex file is generated nicely with an ugly looking dots labels PDF file in it...

Rather than opening a device like you do with calling cairo_pdf(), you should instruct knitr to use the device. Have a look at the knitr options and "dev" in particular. In summary, you need to
<<pieniadze_graph,fig=TRUE,echo=FALSE,dev='cairo_pdf'>>=
...
or to make it default for all chunks
opts_chunk$set(dev='cairo_pdf')

Ok I finally got it!
It really was a problem with Sweave coding in the chunk heading.
After #Matev's response I began testing the dev='cairo_pdf' -> but this did not change anything in the output.
Why? Because the
<<dev='cairo_pdf'>>=
#
is only interpreted by knitr Rnw file weaver!!! And I was using the Sweave weaver (this is set in the global options of R studio under Sweave section).
After recognising the not-so-obvious mistake (Because both Sweave and knitr use similar chunk heading script format) I seached what Leisch had to say about that in his Sweave Manual. This is his solution for everyone who has the same problem:
Put this code early in the document (after R libraries)
<<>>=
my.Swd <- function(name, width, height, ...)
grDevices::cairo_pdf(filename = paste(name, "pdf", sep = "."),
width = width, height = height)
#
You may now use the following code in seperate chunks
<<chunk_name,grdevice=my.Swd,fig=TRUE>>=
#
or as #Matev adviced globally set chunk options for the entire document (but again his answer was for the knitr weaver):
\SweaveOpts{grdevice=my.Swd}
Now You will get beautiful plots generated by the cairo_pdf device (base R device) which handles uniode fonts nicely!!! And they will get sweaved into Your dynamic reports like magic!
And I would like to thank Yihui for the knitr package which is GREAT!

Related

R Notebook: Include figures in report and save plots

I'm using an R Notebook and I'd like my plots to automatically save to disk when the code is run, as well as display inline.
knitr: include figures in report *and* output figures to separate files addresses this for R Markdown but the solution given doesn't work for an R Notebook. Is there a similar option for R Notebooks?
Try setting the knitr fig.path option:
knitr::opts_chunk$set(fig.path = "path/to/figures/")
Where path/to/figures/ is the path to a subdirectory where your figures will be saved. The trailing slash is necessary. This should be a relative path, either relative to the RNotebook file or to the project directory. See here::here() for a handy way to locate the project directory.
This will put each figure into that directory; figure names will be based on the chunk name (so name your chunks!)
This is what eventually worked for me (see #TCZhang 's answer to my question here):
In addition to setting the knitr chunk fig.path="figures/" option suggested by #DonJ, try setting output: html_document, or just press the dropdown next to the Preview [Notebook] button at the top and press Knit to HTML. I think the reason this isn't working is that your output is set to output: html_notebook.
I don't know why this doesn't work specifically when the doc is in R Notebook format. I would also prefer if this worked for output: html_notebook, so it might be an issue we need to open with RStudio or knitr.

How to avoid generating pdf-file per figure in sweave?

Sorry if I'm asking a stupid question, but I'm kinda new to R/Sweave.
I have noticed that if I run my file, RStudio automatically generates a pdf-file for each figure plotted (as well as a pdf-file containing all generated figures from the Sweave-file). For example, suppose I have the following chunk of code in RStudio (simplified version):
\begin{figure}[htbp]
\centering
<<fig1, fig=TRUE, echo=FALSE>>=
plot(pts.X,1:length(pts.X),
main = "Type I error for X-var IT")
#
\caption{}
\label{X-var}
\end{figure}
Then, RStudio saves a pdf-file called R/SweaveFileName-fig1.pdf as well as a pdf-file Rplots.pdf which will also contain any other figure included in the Sweave-file. Since my R/Sweave files contain a lot of figures, I was wondering whether it is possible to change this option in R/Sweave. And, if not, is it possible to redirect these pdf-files into a separate folder?
You can't avoid generating the figures. RStudio isn't really doing much of the work here; it's just directing other software to do it.
R generates the figure, and the LaTeX source code to import it.
LaTeX imports the figure and produces the final .pdf for the whole document.
You can tell R to put the files in a particular place using \SweaveOptions{prefix.string = figs/}. Put this into your document somewhere
pretty early, and all figures will be put into a directory called "figs" (which must exist for this to work).
For more details about the options in Sweave, see the vignette in the utils package.

Automatically generated LaTeX beamer slides with R/knitr

I am working on a LaTeX report template that automatically generates a beamer document, pulling in figures from specified directories and placing them one per slide.
Here is an example of the code that I am using for this, as a code chunk in my .Rnw document:
<<results='asis',echo=FALSE>>=
suppressPackageStartupMessages(library("Hmisc"))
# get the plots from the common directory
Barplots_dir<-"/home/figure/barplots"
Barplots_files<-dir(Barplots_dir)
# create a beamer slide for each plot
# use R to output LaTeX markup into the document
for(i in 1:length(Barplots_files)){
GroupingName<-gsub("_alignment_barplot.pdf", "", Barplots_files[i]) # strip this from the filename
file <- paste0(Barplots_dir,"/",Barplots_files[i]) # path to the figure
cat("\\subsubsection{", latexTranslate(GroupingName), "}\n", sep="") # don't forget you need double '\\' because one gets eaten by R !!
cat("\\begin{frame}{", latexTranslate(GroupingName), " Alignment Stats}\n", sep="")
cat("\\includegraphics[width=0.9\\linewidth,height=0.9\\textheight,keepaspectratio]{", file, "}\n", sep="")
cat("\\end{frame}\n\n")
}
#
However I recently came across this article by Yihui Xie which includes a remark about cat("\\includegraphics{}") being a bad idea. Is there a reason for this, and is there a better option?
To be clear, these figures are generated by other programs as part of a larger pipeline; generating them within the document is not an option, but I need the document to be able to dynamically find and insert them into the report. I know that there are some capabilities to do this directly from within LaTeX itself but cat'ing out the LaTeX markup I need seemed like an easier and more flexible task.
cat("\\includegraphics{}") is likely to be a bad idea if you are from the old Sweave world (where one might need to open a graphics device, draw a plot, close the device, and cat("\\includegraphics{}")). No kittens will be killed as long as you understand what you are doing. Your use case seems to be very reasonable to me, and I don't have a better approach.

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.

R2HTML or knitr for dynamic report generation?

I want to write an R function which processes some data and then automatically outputs an html report. This report should contain some fixed text, some text changing according to the underlying data and some figures.
What is the best way to go?
R2HTML or knitr?
What are the advantages of one over the other?
As far as I understood R2HTML allows me to build the html file sequentially while knitr already operates on an predefined .Rhtml file.
So, either use R2HTML or stitch and spin from knitr for on the fly report generation.
I would appreciate any suggestions or hints.
I grab this nice opportunity to promote pander a bit :)
This package was written for similar reasons like #Yihui's great knitr, although I wanted to let users really concentrate on the text and R code without dealing with chunk options etc. So letting users generate pretty HTML, pdf or even docx or odt output automatically with some predefined options.
These options affects e.g. the cache engine (handling dependencies without any chunk options) or the default plot options (let it be a "base" R graphics, lattice or ggplot2), so that you do no thave to set the color palette or the minor grid in each of your plots, just once - or live with the package defaults :)
The package captures the results (besides errors/warnings and other messages and the output) of all run R expression and can convert to Pandoc's markdown automatically. There are some helper functions that let you convert the resulting document written in a brew-like syntax automatically to e.g. HTML if you have pandoc installed, or export R objects to markdown/HTML/any other supported format in a live R session with a reference class.
Short demo:
brew file
Pandoc.brew('file_name.brew', output = 'foo.html', convert = 'html')
HTML output
knitr, every time. Handles graphics, lets you write your report with markdown instead of having to write html everywhere (if you want), caches things, makes coffee for you etc.
You can also build an HTML file sequentially as long as you have a decent text editor like Emacs/ESS or RStudio, etc. R2HTML is excellent in terms of its wide support to many R objects (see methods(HTML)), but I'll probably frown on RweaveHTML() due to its root Sweave().
That said, I think it may be a good idea to combine R2HTML and knitr, e.g.
# A LOESS Example
```{r loess-demo, results='asis'}
cars.lo <- loess(dist ~ speed, cars)
library(R2HTML)
HTML(cars.lo, file = '')
```
I was using the R Markdown syntax in the above example. The key is results='asis' which means to writing raw HTML code into the output.
I believe that you can also use Sweave to create HTML files, though I have heard that knitr is easier to use.

Resources