I'm trying to use external code chunks with knitr and want to parameterise the chunks. Maybe I'm completely misunderstanding the concept of code chunk options, but here is what I tried to do. In my Rnw file I have:
\documentclass[12pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
<<locate_external_code, include=FALSE, echo=FALSE, message=FALSE, warning=FALSE>>=
library(knitr)
read_chunk('mwex.r')
@
<<setUpMatrices, echo=TRUE, include=FALSE, message=FALSE, warning=FALSE>>=
@
Want to select M1, M2, etc. by `calling' getMatrix setting parameter select to the required value e.g. M1
<<getMatrix, echo=FALSE, include=TRUE, results='asis', message=FALSE, warning=FALSE, select='M1'>>=
@
e.g. M2
<<getMatrix, echo=FALSE, include=TRUE, results='asis', message=FALSE, warning=FALSE, select='M2'>>=
@
The calls don't work but I can get the matrices like this: \newline
<<getM1, echo=FALSE, include=TRUE, results='asis', message=FALSE, warning=FALSE>>=
@
\end{document}
In my r file I have:
## ----setUpMatrices
library(xtable)
NP<-matrix(c(0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1/3, 1/3, 1/3, 0, 0, 1/3, 1/3, 1/3, 0, 0),5,5,byrow=TRUE)
UP<-matrix(c(1/3, 1/3, 1/3, 0, 0),1,5,byrow=TRUE)
mat<-xtable(NP,align=rep('',ncol(NP)+1))
M1<-paste('$',print(mat, floating=FALSE, comment=FALSE,tabular.environment="pmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE),'$',sep='')
mat<-xtable(UP,align=rep('',ncol(NP)+1))
M2<-paste('$',print(mat, floating=FALSE, comment=FALSE,tabular.environment="pmatrix", hline.after=NULL, include.rownames=FALSE, include.colnames=FALSE),'$',sep='')
## ----getMatrix
cat(select)
## ----getM1
cat(M1)
getM1 works OK but the parameterised getMatrix calls result in r reporting "Error: object 'select' not found".