1
votes

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".

2
I don't see where you have defined an object called select. Also, you could set some global chunk options so that you dont have to type out those options for every single chunk - rawr
I was naively assuming that the code chunk option would set a variable select='M1' - stegzzz
I've never used that option. can you point me to the description - rawr
Please take a look at my Rnw example. Amongst the code options I typed select='M1' thinking this would set an r variable as in select<-'M1' - stegzzz

2 Answers

0
votes

It seems you had a misunderstanding: code chunk options are not R objects in your workspace. You have a chunk option select='M1', and that does not mean it will give you a variable select in your workspace. Chunk options are for knitr to tune the behavior of a code chunk.

You did not have problems with M1 because you defined it in the code chunk setUpMatrices.

0
votes

Thank you for this @Yihui.

So to achieve my aim in my Rnw code I have written:

<<echo=FALSE>>=
select<-'M2'
@
<<getMatrix, echo=FALSE, include=TRUE, results='asis', message=FALSE, warning=FALSE>>=
@ 

and in the r file I have

## ----getMatrix
cat(get(select))

It seems a bit clunky to have two chunks to communicate with the r-code in this way but it works OK. I understand now that chunk options are not the way to do it, but maybe there is a more elegant solution?