Q:

Class tufte-book and R listings code box width problem

When I run the following MWE:
\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%

%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%

\begin{document}

\begin{Schunk}
\begin{rexample}\label{set1}\hfill{}\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}\end{Schunk}

\begin{Schunk}
\begin{rexample}\label{set2}\hfill{}\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}\end{Schunk}

\end{document}

which contains R input and output code, the following output is produced

Notice that the width of the input box is different from the width of the output box. How do I make those equal in width?
This code requires Sweavel.sty available here sweavel.sty is generated when one runs knir on an .Rnw file which contains LaTeX and R code

A:

You need to redefine the definition of the style Routstyle for listings.
In your used file Sweavel.sty it is defined as 
\lstdefinestyle{Routstyle}{%
  fancyvrb=false,
  literate={~}{{$\sim$}}1{R^2}{{$R^{2}$}}2{^}{{$^{\scriptstyle\wedge}$}}1{R-squared}{{$R^{2}$}}2,%
  frame=single, 
  framerule=0.2pt,
  framesep=1pt,
  basicstyle=\Routcolor\Routsize\ttfamily,%
  backgroundcolor=\Routbackground}

You can use the following code in your preamble instead:
\lstdefinestyle{Routstyle}{%
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
}

So with the following complete code
\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%

%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

\lstdefinestyle{Routstyle}{% <==========================================
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
} % <===================================================================
%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%

\begin{document}

\begin{Schunk}
\begin{rexample}
\label{set1}\hfill{}
\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}
\end{Schunk}

\begin{Schunk}
\begin{rexample}
\label{set2}\hfill{}
\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}
\end{Schunk}

\end{document}

with the following result:

