\documentclass{beamer}
%\documentclass[handout]{beamer} aurait supprimé les ``overlays''
\newcommand{\mathem}[1]{$\boldsymbol{#1}$}
\newcommand{\key}[1]{{\normalfont\texttt{\uppercase{#1}}}}

\usepackage{fancyvrb}
\usepackage{amsmath,amssymb}
\definecolor{midnightblue}{rgb}{0.098,0.098,0.439}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{,formatcom={\color{midnightblue}}}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\newenvironment{Schunk}{}{}
\DefineVerbatimEnvironment{Code}{Verbatim}{,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{CodeInput}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{CodeOutput}{Verbatim}{,formatcom={\color{midnightblue}}}
\newenvironment{CodeChunk}{}{}

\newcommand{\commande}[1]{\texttt{#1}}

\newcommand{\vide}[1]{}
\newtheorem{proposition}{Proposition}
\newcommand{\prob}{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont P}}
\newcommand{\Prob}{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont P}}
%\newcommand{\transp}{^{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont T}}}
\newcommand{\transp}{^{\text{\fontfamily{cmss}\fontseries{m}\fontshape{n}\selectfont T}}}
\newcommand{\Log}{\rm Log}
\newcommand{\Argmax}{\rm Argmax}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\var}{\mathrm{var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\cov}{\mathrm{cov}}
\def\ind {\mbox {\rm 1\!\!\,l}}%fonction indicatrice

% \usepackage{pdfanim}
% \input{pdfanim-patch}
% \PDFAnimLoad[loop,remember,startframe=1,scaletype=A,height=100pt,width=150pt]{cloud}{balls}{10}

\usetheme{Warsaw}% le thème choisi pour ma présentation
\useoutertheme[hooks]{tree}
%\usecolortheme{seahorse}



\DeclareGraphicsRule{.pdftex}{pdf}{.pdftex}{}



\usepackage[french]{babel}
%\usepackage[latin1]{inputenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}%pour les includegraphics
\usepackage{array}% pour des tableaux particuliers. Nécessaire pour les révéler colonne par colonne
\usepackage{amsthm}
\usepackage{color}
%\usepackage{hyperref}
\usepackage{xspace} % rajouté le 25/05/2009
% On définit le symbole R
\newcommand{\R}{{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont R}\xspace}


\newtheorem{thm}{Théorème}
\newtheorem{result}{Résultat}
\newtheorem{boite}{}
\newtheorem{lemme}{Lemme}
\usepackage{bm,txfonts}
\newcommand{\gras}[1]{\ensuremath{\bm{\mathbf {#1}}}}

\newenvironment{cadre}[2]{%
\begin{center}%
$\text{ }$\\%
\fbox{$\text{ }$ $\text{ }$\parbox{0.9\textwidth}{%
$\text{ }$\\%
$\text{ }$\\%
#2%
$\text{ }$\\%
}$\text{ }$ $\text{ }$}
}{%
\end{center}%
}

\def\fleche{%
\begin{center}%
{\Huge \color{blue}$\Downarrow$\normalcolor}%
\end{center}}



\setbeamercovered{transparent}%l'autre option : invisible, cache totalement le texte pas encore découvert (ie, n'utilise pas le ``grisé'').

\colorlet{orange}{red!70!yellow}% mes petites couleurs que j'aime bien
\colorlet{mauve}{blue!70!red}
\colorlet{brouge}{red!70!blue}

\addtobeamertemplate{footline}{$\quad$\insertframenumber/\inserttotalframenumber\phantom{a}}
%pour ajouter un numéro de page en bas.
% une autre solution : \useoutertheme{shadow}


\title[Introductory course on the \R software]{Introductory course on the \R software}
%l'argument entre [] est une version courte du titre, qu'on met en bas des transparents.

\author[Lafaye de Micheaux, Pierre] % (optionnel, utiliser seulement s'il y a beaucoup d'auteurs)
{\textbf{P.~Lafaye de Micheaux}\inst{1}}


\institute[Départements et Universités] % (optionnel, mais nécessaire la plupart du temps)
{
  \inst{1}%
  	Mathematics and Statistics Departement\\
  	Université de Montréal\\
	CANADA
}

\date[2014]{New South Wales University\\
Sydney, Australia\\ June 10, 2014\\
\vspace{1ex}
\url{https://biostatisticien.eu/springeR/courseRw5.pdf}
}

%\AtBeginSection[] % pour mettre la table des matières 
%                 % au début de chaque section
%{ \begin{frame}<beamer> 
%\frametitle{Plan de la présentation} 
%\tableofcontents[currentsection] 
%\end{frame} }

 \pgfdeclareimage[height=96mm,width=128mm]{nombidon}{BatimentGIN}
  \setbeamertemplate{background}{\pgfuseimage{nombidon}}

% \logo{\includegraphics[width=1.5cm]{Rlogo.jpg}}% je définis le logo qui sera mis sur tous les transparents

\setbeamertemplate{background}{transparent}

\usepackage{ragged2e}
\justifying



\usepackage{algpseudocode,algorithm}


\RequirePackage{xcolor} 
\definecolor{beaubleu}{rgb}{0.26,0.31,0.61}
\definecolor{beauvert}{rgb}{0.27,0.52,0.42}


\usepackage{upquote}

\begin{document}


\begin{frame}
\titlepage


\vfill
\end{frame}

%\begin{frame}
%\tableofcontents[pausesections]
%\vspace*{\fill}
%\end{frame}



\section{Goals}

\begin{frame}
\frametitle{Goals of today lecture}

Describing the instructions for
\begin{itemize}
\item control structures;
\item creating basic 
functions.
\end{itemize}

\end{frame}


\section{Control Flow}


\begin{frame}[fragile]
\frametitle{Instructions \commande{if} and \commande{else}}

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ if (TRUE) 1+1}
\textit{\textcolor{beauvert}{[1] 2}}
>\textcolor{beaubleu}{ x <- 2}
>\textcolor{beaubleu}{ y <- -3}
>\textcolor{beaubleu}{ if (x <= y) \{}
+\textcolor{beaubleu}{    z <- y-x}
+\textcolor{beaubleu}{    print("x smaller than y")}
+\textcolor{beaubleu}{             \} else \{ }
+\textcolor{beaubleu}{    z <- x-y}
+\textcolor{beaubleu}{    print("x larger than y")}
+\textcolor{beaubleu}{    z }
+\textcolor{beaubleu}{                    \}}
\textit{\textcolor{beauvert}{[1] "x larger than y"}}
\textit{\textcolor{beauvert}{[1] 5}}
\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Instruction \commande{for}}

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ for (i in 1:3) print(i)}
\textit{\textcolor{beauvert}{[1] 1}}
\textit{\textcolor{beauvert}{[1] 2}}
\textit{\textcolor{beauvert}{[1] 3}}
>\textcolor{beaubleu}{ x <- c(1,3,7,2)}
>\textcolor{beaubleu}{ for (var in x) print(2*var)}
\textit{\textcolor{beauvert}{[1] 2}}
\textit{\textcolor{beauvert}{[1] 6}}
\textit{\textcolor{beauvert}{[1] 14}}
\textit{\textcolor{beauvert}{[1] 4}}
\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Instruction \commande{while}}

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ x <- 2}
>\textcolor{beaubleu}{ y <- 1}
>\textcolor{beaubleu}{ while(x+y<7) x <- x+y}
>\textcolor{beaubleu}{ x}
\textit{\textcolor{beauvert}{[1] 6}}
\end{Verbatim}

\end{frame}

\section{Creating functions}

\begin{frame}[fragile]

The formula $BMI=\frac{Weight}{Height^2}$ is easily programmed in \R as follows:
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ BMI <- function(weight,height) \{}
+\textcolor{beaubleu}{  bmi <- weight/height^2}
+\textcolor{beaubleu}{  names(bmi) <- "BMI"}
+\textcolor{beaubleu}{ return(bmi)}
+\textcolor{beaubleu}{ \}}
\end{Verbatim}
We can now execute the function  \commande{BMI()} we just created:
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\scriptsize,fontseries=b]
>\textcolor{beaubleu}{ BMI(70,1.82)}
\textit{\textcolor{beauvert}{     BMI }}
\textit{\textcolor{beauvert}{21.13271 }}
>\textcolor{beaubleu}{ BMI(1.82,70) # Note that it is not possible to swap the}
\textcolor{beaubleu}{               # arguments of a function,}
\textit{\textcolor{beauvert}{         BMI }}
\textit{\textcolor{beauvert}{0.0003714286 }}
>\textcolor{beaubleu}{ BMI(height=1.82,weight=70) # unless they are preceded by their}
\textcolor{beaubleu}{                             # names.}
\textit{\textcolor{beauvert}{     BMI }}
\textit{\textcolor{beauvert}{21.13271 }}
\end{Verbatim}
This function only outputs a single value.
\end{frame}

\begin{frame}[fragile]
The code below outputs a list of several variables.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ BMI <- function(weight,height) \{}
+\textcolor{beaubleu}{  bmi <- weight/height^2}
+\textcolor{beaubleu}{  res <- list(weight,height,bmi)}
+\textcolor{beaubleu}{ return(res)}
+\textcolor{beaubleu}{ \}}
\end{Verbatim}
 \commande{BMI()} returns a list of unnamed elements:
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ BMI(70,1.82)}
\textit{\textcolor{beauvert}{[[1]]}}
\textit{\textcolor{beauvert}{[1] 70}}
\textit{\textcolor{beauvert}{[[2]]}}
\textit{\textcolor{beauvert}{[1] 1.82}}
\textit{\textcolor{beauvert}{[[3]]}}
\textit{\textcolor{beauvert}{[1] 21.13271}}
\end{Verbatim}


\end{frame}

\begin{frame}[fragile]

To name the elements of the list, you can use the following code
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ BMI <- function(weight,height) \{}
+\textcolor{beaubleu}{  bmi <- weight/height^2}
+\textcolor{beaubleu}{  res <- list(Weight=weight,Height=height,BMI=bmi)}
+\textcolor{beaubleu}{ return(res)}
+\textcolor{beaubleu}{ \}}
\end{Verbatim}


which gives the following result:
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ BMI(70,1.82)}
\textit{\textcolor{beauvert}{$Weight}}
\textit{\textcolor{beauvert}{[1] 70}}
\textit{\textcolor{beauvert}{$Height}}
\textit{\textcolor{beauvert}{[1] 1.82}}
\textit{\textcolor{beauvert}{$BMI}}
\textit{\textcolor{beauvert}{[1] 21.13271}}
\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Your turn to work!}

Do the Worksheet F on page 140.\\
$ $\\
\url{http://biostatisticien.eu/springeR/Rbook-chap5.pdf}\\


\end{frame}


% \bibliographystyle{jss}
% \bibliography{biblio}

\end{document}

