4

I am starting to use knitr to print graphics from R in my document. So far what I have been doing (I don't know if this is the best approach, but it is working) is to load the my R workspace with

<<prepare-data, include=FALSE>>=
load("~/Desktop/r_work_directory/workspace.RData")
@ 

after the preamble, then calling an R script to produce the graphics with

<<child-foo, child='figure/foo.R', include=FALSE>>=
@

and finally adding the figure with

\begin{figure}
  \centering
  \makebox[\textwidth]{\includegraphics[width=0.7\paperwidth]{figure/foo.pdf}}
  \caption{This is Foo} 
      \label{fig:Foo}
\end{figure}

The only thing I couldn't get with this method is to have the fonts in the graphics to match those in my document. I saw in the knitr manual that consistency is given (only?) through the tikzDevice. Unfortunately the tikzDevice package is not available with R 3.0.

Is there any other approach to get font style consistency?

4
  • 1
    tikzDevice was only archived on CRAN; it was not deleted, and you can still install from source, e.g. install.packages('tikzDevice', repos='http://r-forge.r-project.org', type='source')
    – Yihui Xie
    Sep 27, 2013 at 19:39
  • It worked! I managed to install it from source. I had to first install.packages('filehash') and then install.packages("~/Downloads/tikzDevice_0.6.3.tar.gz", repos = NULL, type="source")
    – Francesco
    Sep 27, 2013 at 22:01
  • I'm glad you figured it out. That is exactly what you need to do. I really hope one day someone can bring tikzDevice back to CRAN (I have said this many times...). Anyway, please feel free to write an answer to your own question.
    – Yihui Xie
    Sep 27, 2013 at 23:18
  • If anyone is trying to install tikzDevice on a Mac, by following the suggestions in these comments, you need the llvm-gcc-4.2 compiler installed on your machine. Previously, you could get this on your machine by installing Xcode and the command line tools. However, Apple deprecated support for llvm-gcc in Xcode 5. The simplest solution that I found was just to download Xcode 4 from Apple's Developer website.
    – Adam Liter
    Sep 30, 2013 at 15:33

1 Answer 1

2

I managed to install tikzDevice on Mac running R 3.0 executing the following from R console:

install.packages('filehash')
install.packages("~/Downloads/tikzDevice_0.6.3.tar.gz", repos = NULL, type="source")

With the tikzDevice I could let knitr producing my plots with the same font family used in the rest of the document.

In the .Rnw document I added

<<prepare-data, include=FALSE>>=
load("~/Desktop/r_work_directory/workspace.RData")
@ 

to load my pre-saved R workspace, so to call R objects previously created directly from the knitr chucks embedded (as the following) in the \figure environment of my document.

\begin{figure}
  \centering

  <<my-plot, dev='tikz', fig.width=6, fig.height=3, out.width='.95\\textwidth', message=FALSE, echo=FALSE>>=
  plot(my_R_object)
  @

  \caption{This is my plot} 
  \label{fig:my-plot}
\end{figure} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .