James Curran
James Curran

Reputation: 1294

Coloring R input and output differently using knitr

I would like to have my input R code printed/displayed on a light grey background and the output on a white background. So for example if my .Rnw file contains this chunk:

<<>>=
t.test(zinc~sex, data = zinc.df)
@

I would like the input to be rendered with a light grey background:

> t.test(zinc~sex, data = zinc.df)

and the output to be rendered on a white background, e.g.

Welch Two Sample t-test

data: zinc by sex

t = -15.08, df = 2678, p-value < 2.2e-16

(Output deliberately truncated but you get the idea). It seems like I might be able to do this with knitr themes, but I can't quite see it. Like many, I'm not keen to start fiddling with the listings package.

Upvotes: 3

Views: 1076

Answers (3)

S&#233;bastien Rochette
S&#233;bastien Rochette

Reputation: 6661

If your working with Rnw and Latex only, you can use the background option of the chunks. However, you will need to write your chunk twice.
(1) with echo=TRUE, eval=FALSE and background='white'

<<echo=TRUE, eval=FALSE, background='white'>>=
@  

(2) with echo=FALSE, eval=TRUE and background='yellow'

<<echo=FALSE, eval=TRUE, background='yellow'>>=
@    

Upvotes: 1

user1134616
user1134616

Reputation: 604

This is the best I can do:

\documentclass[12pt,letterpaper,twoside]{book}  
\usepackage{inconsolata}
\usepackage{listings,color}

<<setup, include=FALSE>>=
options(width=60)
opts_chunk$set(fig.align='center', concordance=TRUE, fig.show='hold', size='footnotesize', prompt=FALSE, comment=NA, tidy=TRUE, results='markup', tidy.opts=list(width.cutoff=50), comment='#-#', highlight=TRUE)
opts_knit$set(concordance=TRUE,self.contained=FALSE)
color_block_output = function(x) {
function(x, options)  paste('\\begin{outputblock}',x,'\\end{outputblock}',sep="")
}
knit_hooks$set(output = color_block_output(''))
@

\lstnewenvironment{outputblock}{
\lstset{backgroundcolor=\color{white},
frame=single,
framerule=0pt,
basicstyle=\ttfamily,
columns=fullflexible}}{}

\begin{document}


<<test>>=
print('test')

(mat <- matrix(data=1:25,nrow=5,ncol=5)) 
@

\end{document}

The result will be:enter image description here

I haven't find a solution for the situation when there is no output (the white space looks a bit strange) and the trailing grey space. Anyway, this is a start, hope this helps.

Upvotes: 0

mlegge
mlegge

Reputation: 6913

Your problem is best suited using the listings package, but it need not be painful -- here is an example (note the backgroundcolor parm in the lstdefinestyle):

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{color}

%-- Define your colors, easy to look up
\definecolor{Rcodegreen}{rgb}{0,0.6,0}
\definecolor{Rcodegray}{rgb}{0.5,0.5,0.5}
\definecolor{Rcodepurple}{rgb}{0.58,0,0.82}
\definecolor{Rbackcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{customstyle}{
backgroundcolor=\color{Rbackcolour},   
commentstyle=\color{Rcodegreen},
keywordstyle=\color{Rmagenta},
numberstyle=\tiny\color{Rcodegray},
stringstyle=\color{Rcodepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,         
breaklines=true,                 
captionpos=b,                    
keepspaces=true,                 
numbers=left,                    
numbersep=5pt,                  
showspaces=false,                
showstringspaces=false,
showtabs=false,                  
tabsize=2
}

\lstset{style=customstyle}

\title{Fooing with foo}

\begin{document}
\maketitle

<<tidy=TRUE,highlight=FALSE>>=
x <- "h3ll0 w0rld"
cat(x, "\n")

@

\end{document}

Upvotes: 1

Related Questions