Coloring R input and output differently using knitr

I would like my R input code to be printed / displayed on a light gray background and the output on a white background. For example, if my .Rnw file contains this snippet:

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

      

I would like the input to appear on a light gray background:

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

and the output to be displayed on a white background like

Welch Two Sample T-Test

data: zinc by gender

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

(The output is intentionally truncated, but you get the idea.) It looks like I could do it with newbie themes, but I can't see it. Like many others, I'm not going to start the game with a listing package.

+3


source to share


3 answers


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 have not found a solution for a situation where there is no output (white space looks a bit weird) and trailing gray space. Anyway, this is a start, hope it helps.

+1


source


Your problem is best suited with a package listings

, but it shouldn't be painful - here's an example (take a look at backgroundcolor

parm's 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}

      

+1


source


If you only work with Rnw and Latex, you can use the background

chunks option . However, you will need to write your piece twice.
(1) c echo=TRUE

, eval=FALSE

andbackground='white'

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

      

(2) c echo=FALSE

, eval=TRUE

andbackground='yellow'

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

      

+1


source







All Articles