[Tex/LaTex] inline knitr code that isn’t a simple number

knitrrsweave

It is easy enough to do something like \Sexpr{1 + 1}. But how can I do \Sexpr{mymatrix} with mymatrix being a matrix object in R workspace? More generally, how can I print object that is more complex than just a number using inline code chunk?

Best Answer

To answer your question in the comment: there is no single name. I said functions, and I mean string manipulation functions like paste(), substr(), and so on. You may take a look at the packages for generating tables, and try capture.output(), then paste(). But I doubt if it is worth it -- why not using a code chunk? For example,

<<results='asis'>>=
library(xtable)
xtable(mymatrix)
@

<<results='asis'>>=
library(knitr)
kable(mymatrix)
@

You can certainly make it in the inline code if you do not mind the long R expression, e.g. \Sexpr{paste(capture.output(kable(mymatrix)), collapse='\n')}.