R code and Sweave output - EDIT

UPDATED Question 1 In the lattest update of KnitR the output is clean as what I expected. Maybe an update was made.

The new output :

Before update

I'm trying to get a particular output with Sweave and R code. For now I have this code :

\documentclass[11pt,french]{report}
\usepackage{babel} %%french
\usepackage{amsmath,amsfonts,amssymb} %%maths
\usepackage[utf8]{inputenc}   % LaTeX
\usepackage[T1]{fontenc}      % LaTeX
\usepackage[dvipsnames,table,xcdraw]{xcolor}
%% Background code chunk
\usepackage{listings}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%% background code chunk listing
\lstset{
language=R}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}

\lstset{style=mystyle}


witch is coming from this site.

The R code I'm trying to implement is :

%% script R
\begin{lstlisting}[linerange=\\begin\{Sinput\}-\\end\{Sinput\},includerangemarker=false, caption = Code source en R pour l'exemple]
<<eval =FALSE>>=
# dataset
x <- c(2,3,6,9,12); y <- c(2,5,3,6,5)

# Estimations des parametres
reg <- lm(y ~ x)

# Resume de l'estimation
summary(reg)

# Valeurs de Yt
fitted(reg)

# Residus
residuals(reg)
@
\end{lstlisting}


The output :

My questions :

1- How can I get a better output for the ~ symbol ?

2- Is there a way to eval the code without getting and output like this ?

• Could you precise what you mean with "better" (it is very subjective) and how it should look like? – nebi Aug 1 '17 at 18:24
• Something like the \sim in latex. a Tilde but not in the air. Now it look like a tilde to get over a character. It's the symbol meaning y follow X. – David Beauchemin Aug 2 '17 at 4:07
• About question 2: obviously remove eval =FALSE and depending of what you want exactly , add echo=FALSE or results='hide'. – Fran Aug 6 '18 at 21:09
• @Fran This solved the case. – David Beauchemin Aug 22 '18 at 17:35

<<eval = TRUE, echo=FALSE, fig=TRUE, height=4,width=4.5>>=

The solution is just to add echo=false and eval=false for preventing the \begin{Schunk} and other Tex command.