Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Monday, October 21, 2013

ESS (Emacs Speaks Statistics) now supports compiling knitr

First, new version of ESS needs to be installed.  I use Aquamacs, so I want to make sure that I update Aquamacs. You can do this by uncommenting appropriate lines in Makeconf file of the ESS distribution (search for Aquamacs in Makeconf file).

Two variables need to be set within emacs.

  • Change 'ess-swv-processor' to 'knitr'
  • Set the variable 'ess-swv-plug-into-AUCTeX' to use the existing interface for compiling LaTeX files: i.e., C-c C-c
Now you should be able to use 'C-c C-c Sweave' or 'C-c C-c LaTeXSweave' commands to compile your Rnw files.

Thursday, March 1, 2012

"..." arguments in R

"..." arguments is a way to pass unknown parameters to downstream functions.

This is a typical usage.

> a <- function(...){
return(b(...))
}

b <- function(...){
return(c(...))
}

c <- function(varIn){
return(varIn)
}
> a(varIn="hello")
[1] "hello"

Sunday, February 12, 2012

tikzDevice and knitr

Ubuntu's late packages are based on texlive 2009.
Latest is texlive 2011; so I updated to it but I started getting this error

Error in get("tikz", envir = as.environment("package:tikzDevice"))(..., : Graphics API version mismatch

Error was due to tikzDevice package being older than what knitr package expects
To fix this problem, install the repository version of tikzDevice

install.packages( 'tikzDevice', repos = 'http://r-forge.r-project.org' )

After that all is good. (ref thread)

Friday, February 3, 2012

"knitr" and "ggplot2" for Automatic LaTeX Report Generation with R

knitr is supported under R versions >2.14.1. So I upgraded R, and also needed some extra packages for ggplot2. (probably not all are required for ggplot2 but I am interested in using these packages in my research)

sudo apt-get install libX11-dev libglu1-mesa-dev libxml2-dev libgeos-dev libproj-dev libgdal-dev

Some R packages as well

install.packages(c("rgdal","maps","maptools","rgeos"))

Only, then, was I able to install knitr package according to this post.

install.packages("devtools")
library(devtools)
install_github("ggplot2","hadley")

install_github("knitr","yihui")

Thursday, February 2, 2012

Using pgfSweave Package for R

pgfSweave can generate a latex document with R code in it. This means that plots are updated automagically, but setting up/getting used to it can be a big pain. The difference between pgfSweave and Sweave is that latter cannot cache plots. Hence, if one uses packages like ggplots, the report generation can be much slower than using pgfSweave.

To use pgfSweave, start with putting this in the preamble
\documentclass{article}

\usepackage{tikz}
\tikzset{external/system call={latex \tikzexternalcheckshellescape -halt-on-error
-interaction=batchmode -jobname "\image" "\texsource";
dvips -o "\image".ps "\image".dvi;}}

\begin{document}

<<setup,echo=F>>=
setCacheDir("cache")
options(keep.space=TRUE)
@

<<data,cache=T,tidy=T>>=
# hey, a comment
data(airquality)
print(kruskal.test( Ozone ~ Month, data = airquality )) # and another
@

\begin{figure}[!ht]
\centering
%notice the new options
{\pgfkeys{/pgf/images/include external/.code={\includegraphics[width=6in]{#1}}}
<<boxplot,cache=T,echo=T,fig=T,width=3,height=3,tikz=T,external=T,highlight=T>>=
boxplot(Ozone ~ Month, data = airquality,
main='Ozone distribution',xlab='Month',ylab='Concentration')
@
}% this brace ends the effect of 'include external'
\caption{This is from pgfSweave. Text is typset by \LaTeX\ and so matches the
font of the document.}
\end{figure}

\end{document}


Following inserts a cleaned up (and highlighted) code block whose result is cached <<data,cache=T,tidy=T>>=
# hey, a comment
data(airquality)
print(kruskal.test( Ozone ~ Month, data = airquality )) # and another
@


Following inserts a plot of size 2x2 sq.in. and embeds it at size 3x3 sq.in. \begin{figure}[!ht]
\centering
%notice the new options
{\pgfkeys{/pgf/images/include external/.code={\includegraphics[width=3in]{#1}}}
<<boxplot2,cache=T,echo=T,fig=T,width=2,height=2,tikz=T,external=T,highlight=T>>=
boxplot(Ozone ~ Month, data = airquality,
main='Ozone distribution',xlab='Month',ylab='Concentration')
@
}% this brace ends the effect of 'include external'
\caption{This is from pgfSweave. Text is typset by \LaTeX\ and so matches the
font of the document.}
\end{figure}


The Makefile from pgfSweave package shows how to compile things.
R CMD pgfsweave --pgfsweave-only pgfSweave-latex-example.Rnw
latex pgfSweave-latex-example.tex
make -f pgfSweave-latex-example.makefile
latex pgfSweave-latex-example.tex
dvipdf pgfSweave-latex-example.dvi


This post was useful.