Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/r/rpy2talk/rpy2talk.tex
Views: 1023
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1% KOMA-Script Presentation2% LaTeX Template3% Version 1.0 (3/3/13)4%5% This template has been downloaded from:6% http://www.LaTeXTemplates.com7%8% Original Authors:9% Marius Hofert (marius.hofert@math.ethz.ch)10% Markus Kohm (komascript@gmx.info)11% Described in the PracTeX Journal, 2010, No. 212%13% Modified for a talk about RPy2 and IPython's Rmagic extension14% It's also a demo about SMC's capabilities to write a presentation.15% Written by Harald Schilly (harald.schilly@univie.ac.at), 2013-12-0316%17% License:18% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)19%20%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%2122%----------------------------------------------------------------------------------------23% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS24%----------------------------------------------------------------------------------------2526\documentclass[27paper=128mm:96mm, % The same paper size as used in the beamer class28fontsize=11pt, % Font size29pagesize, % Write page size to dvi or pdf30parskip=half-, % Paragraphs separated by half a line31]{scrartcl} % KOMA script (article)3233\linespread{1.12} % Increase line spacing for readability3435%------------------------------------------------36% Colors37\usepackage{xcolor} % Required for custom colors38% Define a few colors for making text stand out within the presentation39\definecolor{mygreen}{RGB}{44,85,17}40\definecolor{myblue}{RGB}{60,60,204}41\definecolor{mybrown}{RGB}{194,164,113}42\definecolor{myred}{RGB}{255,66,56}43% Use these colors within the presentation by enclosing text in the commands below44\newcommand*{\mygreen}[1]{\textcolor{mygreen}{#1}}45\newcommand*{\myblue}[1]{\textcolor{myblue}{#1}}46\newcommand*{\mybrown}[1]{\textcolor{mybrown}{#1}}47\newcommand*{\myred}[1]{\textcolor{myred}{#1}}48%------------------------------------------------4950%------------------------------------------------51% Margins52\usepackage[ % Page margins settings53includeheadfoot,54top=3.5mm,55bottom=3.5mm,56left=5.5mm,57right=5.5mm,58headsep=6.5mm,59footskip=8.5mm60]{geometry}61%------------------------------------------------6263%------------------------------------------------64% Fonts65\usepackage[T1]{fontenc} % For correct hyphenation and T1 encoding66\usepackage{lmodern} % Default font: latin modern font67\usepackage{utopia} % Alternative font: utopia68%\usepackage{charter} % Alternative font: low-resolution roman font69\renewcommand{\familydefault}{\sfdefault} % Sans serif - this may need to be commented to see the alternative fonts70%------------------------------------------------7172%------------------------------------------------73% Various required packages74\usepackage{amsthm} % Required for theorem environments75\usepackage{amsmath}76\usepackage{bm} % Required for bold math symbols (used in the footer of the slides)77\usepackage{graphicx} % Required for including images in figures78\usepackage{tikz} % Required for colored boxes79\usepackage{booktabs} % Required for horizontal rules in tables80\usepackage{multicol} % Required for creating multiple columns in slides81\usepackage{lastpage} % For printing the total number of pages at the bottom of each slide82\usepackage[english]{babel} % Document language - required for customizing section titles83\usepackage{microtype} % Better typography84\usepackage{tocstyle} % Required for customizing the table of contents85\usepackage{ulem}86\usepackage{verbatim}87\usepackage[]{hyperref}88\hypersetup{89colorlinks = true, %Colours links instead of ugly boxes90urlcolor = myblue, %Colour for external hyperlinks91linkcolor = myblue, %Colour of internal links92citecolor = myblue %Colour of citations93}94\usepackage{listings}95\usepackage{wasysym} % for emoticons96%------------------------------------------------9798%------------------------------------------------99% Slide layout configuration100\usepackage{scrpage2} % Required for customization of the header and footer101\pagestyle{scrheadings} % Activates the pagestyle from scrpage2 for custom headers and footers102\clearscrheadfoot % Remove the default header and footer103\setkomafont{pageheadfoot}{\normalfont\color{black}\sffamily} % Font settings for the header and footer104105% Sets vertical centering of slide contents with increased space between paragraphs/lists106\makeatletter107\renewcommand*{\@textbottom}{\vskip \z@ \@plus 1fil}108\newcommand*{\@texttop}{\vskip \z@ \@plus .5fil}109\addtolength{\parskip}{\z@\@plus .25fil}110\makeatother111112% Remove page numbers and the dots leading to them from the outline slide113\makeatletter114\newtocstyle[noonewithdot]{nodotnopagenumber}{\settocfeature{pagenumberbox}{\@gobble}}115\makeatother116\usetocstyle{nodotnopagenumber}117118\AtBeginDocument{\renewcaptionname{english}{\contentsname}{\Large Outline}} % Change the name of the table of contents119%------------------------------------------------120121%------------------------------------------------122% Header configuration - if you don't want a header remove this block123\ihead{\hspace{-2mm}\begin{tikzpicture}[remember picture,overlay]\node [xshift=\paperwidth/2,yshift=-\headheight] (mybar) at (current page.north west)[rectangle,fill,inner sep=0pt,minimum width=\paperwidth,minimum height=2\headheight,top color=myblue!81,bottom color=myblue]{}; % Colored bar\node[below of=mybar,yshift=3.3mm,rectangle,shade,inner sep=0pt,minimum width=128mm,minimum height =1.5mm,top color=black!50,bottom color=white]{}; % Shadow under the colored barshadow124\end{tikzpicture}\color{white}\runninghead} % Header text defined by the \runninghead command below and colored white for contrast125%------------------------------------------------126127%------------------------------------------------128% Footer configuration129\newlength{\footheight}130\setlength{\footheight}{8mm} % Height of the footer131\addtokomafont{pagefoot}{\footnotesize} % Small font size for the footnote132133\ifoot{% Left side134\hspace{-2mm}135\begin{tikzpicture}[remember picture,overlay]136\node [xshift=\paperwidth/2,yshift=\footheight] at (current page.south west)[rectangle,fill,inner sep=0pt,minimum width=\paperwidth,minimum height=3pt,top color=myblue,bottom color=mygreen]{}; % Green bar137\end{tikzpicture}138\myauthor\ \raisebox{0.2mm}{$\bm{\vert}$}\ \myuni % Left side text139}140141\ofoot[\pagemark/\pageref{LastPage}\hspace{-2mm}]{\pagemark/\pageref{LastPage}\hspace{-2mm}} % Right side142%------------------------------------------------143144%------------------------------------------------145% Section spacing - deeper section titles are given less space due to lesser importance146\usepackage{titlesec} % Required for customizing section spacing147\titlespacing{\section}{0mm}{0mm}{0mm} % Lengths are: left, before, after148\titlespacing{\subsection}{0mm}{0mm}{-1mm} % Lengths are: left, before, after149\titlespacing{\subsubsection}{0mm}{0mm}{-2mm} % Lengths are: left, before, after150\setcounter{secnumdepth}{0} % How deep sections are numbered, set to no numbering by default - change to 1 for numbering sections, 2 for numbering sections and subsections, etc151%------------------------------------------------152153%------------------------------------------------154% Theorem style155\newtheoremstyle{mythmstyle} % Defines a new theorem style used in this template156{0.5em} % Space above157{0.5em} % Space below158{} % Body font159{} % Indent amount160{\sffamily\bfseries} % Head font161{} % Punctuation after head162{\newline} % Space after head163{\thmname{#1}\ \thmnote{(#3)}} % Head spec164165\theoremstyle{mythmstyle} % Change the default style of the theorem to the one defined above166\newtheorem{theorem}{Theorem}[section] % Label for theorems167\newtheorem{remark}[theorem]{Remark} % Label for remarks168\newtheorem{algorithm}[theorem]{Algorithm} % Label for algorithms169\makeatletter % Correct qed adjustment170%------------------------------------------------171172%------------------------------------------------173% The code for the box which can be used to highlight an element of a slide (such as a theorem)174\newcommand*{\mybox}[2]{ % The box takes two arguments: width and content175\par\noindent176\begin{tikzpicture}[mynodestyle/.style={rectangle,draw=myblue,thick,inner sep=2mm,text justified,top color=white,bottom color=white,above}]\node[mynodestyle,at={(0.5*#1+2mm+0.4pt,0)}]{ % Box formatting177\begin{minipage}[t]{#1}178#2179\end{minipage}180};181\end{tikzpicture}182\par\vspace{-1.3em}}183%------------------------------------------------184185%----------------------------------------------------------------------------------------186% PRESENTATION INFORMATION187%----------------------------------------------------------------------------------------188189\newcommand*{\mytitle}{R meets Python in the Cloud} % Title190\def\shorttitle{R meets Python}191\newcommand*{\runninghead}{\shorttitle} % Running head displayed on almost all slides192\newcommand*{\newhead}[1]{\renewcommand*{\runninghead}{\shorttitle: #1}}193\newcommand*{\myauthor}{Harald Schilly} % Presenters name(s)194\newcommand*{\mydate}{2013-12-03} % Presentation date195\newcommand*{\myuni}{University of Vienna196\raisebox{0.2mm}{$\bm{\vert}$}\197$\text{Vienna} \leftarrow \text{R Meetup}$} % University or department198199%----------------------------------------------------------------------------------------200201\begin{document}202203%----------------------------------------------------------------------------------------204% TITLE SLIDE205%----------------------------------------------------------------------------------------206207% Title slide - you may have to tweak a few of the numbers if you wish to make changes to the layout208\thispagestyle{empty} % No slide header and footer209\begin{tikzpicture}[remember picture,overlay] % Background box210\node [xshift=\paperwidth/2,yshift=\paperheight/2] at (current page.south west)[rectangle,fill,inner sep=0pt,minimum width=\paperwidth,minimum height=\paperheight/3,top color=myblue,bottom color=myblue]{}; % Change the height of the box, its colors and position on the page here211\end{tikzpicture}212% Text within the box213\begin{flushright}214\vspace{0.65cm}215\color{white}\sffamily216{\bfseries\Large\mytitle\par} % Title217\vspace{0.4cm}218\normalsize219\myauthor\par % Author name220\mydate\par % Date221\vfill222\end{flushright}223224\clearpage225226%----------------------------------------------------------------------------------------227% TABLE OF CONTENTS228%----------------------------------------------------------------------------------------229230%\thispagestyle{empty} % No slide header and footer231232%\small\tableofcontents % Change the font size and print the table of contents - it may be useful to shrink the font size further if the presentation is full of sections233% To exclude sections/subsections from the table of contents, put an asterisk after \(sub)section like so: \section*{Section Name}234235%\clearpage236237%----------------------------------------------------------------------------------------238% PRESENTATION SLIDES239%----------------------------------------------------------------------------------------240241\newhead{Motivation}242\section{R vs. Python}243244\href{http://www.r-project.org}{\textbf{R}}245is a \uline{vertically specialized} environment for statistics.246It is based on the domain specific \textbf{S} language (mid 70-ties),247has a lot of packages for statistics and supporting functionalities.248249\href{http://www.python.org}{\textbf{Python}} is a general purpose language,250designed for beginners (clear syntax, easy to start),251very popular (Class A, Tiboe Index),252excellent support for extensions and253also generally accepted in engineering and scientific areas.254255Remarkably, the latest incarnation of S256(``\texttt{\~}''--formulas and \texttt{data.frame})257and the first version of Python appeared both in 1991.258259\clearpage260261%------------------------------------------------262263\newhead{Common Ground}264\subsection{Common Ground}265266R is primarily an REPL command-line interpreter,267which can also run scripts.268It manages its own ``world'' of objects.269270In order to interface with R:271\begin{itemize}272\item{data-structures:} need to be passed in and out.273\item{functions:} need to be called from the ``outside''274in order to change R's internal state on the ``inside''.275\item{plots:} are created in R and need to be made available.276\item{speed\&reliability:} everything should happen fast and reliably.277\end{itemize}278279280\clearpage281\newhead{RPy2}282\section{RPy2}283284There is one famous Python library to accomplish this:285\href{http://rpy.sourceforge.net/rpy2.html}{\textbf{RPy2}}286287It is a Python module and it is compiled against R's C Code.288That makes it a robust low-level interface.289290R's environment for variable states, the functions, operators,291libraries/packages, and other elements are292\uline{exposed as Python objects}.293294Exchanging data from and to R is based on295converting basic data types, ordered lists, strings,296and most notably297\href{http://wiki.scipy.org/Tentative_NumPy_Tutorial}{\uline{NumPy Arrays}}.298299\clearpage300\newhead{RPy2: Example}301302\begin{lstlisting}[language=python]303# evaluate arbitrary code304>>> summary = rpy2.robjects.r("...") # .reval(...)305306# a function reference307>>> summary = rpy2.robjects.r['summary']308309# calling `seq` directly310>>> rpy2.robjects.r.seq(1,10)311312# after activating NumPy Array conversion:313>>> xx = np.array([5,4,2.2,-1,-5.5])314>>> summary(xx)315\end{lstlisting}316317318\clearpage319\newhead{IPython}320\section{IPython}321322\href{http://www.ipython.org}{\textbf{IPython}} is probably the most323widely used interface to Python for scientific purposes.324It's excellent for rapid prototyping, exchanging ideas and results,325and publishing: e.~g. \href{http://nbviewer.ipython.org/}{NBViewer} renders them on-line.326327Relevant here: a so called328\href{http://ipython.org/ipython-doc/dev/config/extensions/rmagic.html}329{\textbf{R-``magic''-extension}} exists,330based on RPy2, which helps to conveniently talk to R.331332\clearpage333\newhead{IPython: Commands}334\subsection{IPython: Commands}335336\begin{itemize}337\item{\texttt{\%R}} runs a line of R code: \texttt{result = \%R ...}338\item{\texttt{\%\%R -i <invar> -o <outvar> ...}}339runs the entire cell in R while converting the given input and output variables.340\item{\texttt{\%Rpush <var>}} sends the data of a given variable to R341\item{\texttt{\%Rpull <var>}} gets the variable and Python's namespace is populated342\item{\texttt{\%Rget <var>}} is similar, only retrieves the actual data.343\end{itemize}344345\clearpage346\newhead{IPython: Example}347\subsection{IPython: Example}348349Simple example:350\begin{verbatim}351%%R352a <- seq(10) + 5.5353print(summary(a))354\end{verbatim}355gives356\begin{verbatim}357Min. 1st Qu. Median Mean 3rd Qu. Max.3586.50 8.75 11.00 11.00 13.25 15.50359\end{verbatim}360361\mygreen{$\Rightarrow{}$ All examples shown as part of this talk are362\href{http://nbviewer.ipython.org/urls/github.com/sagemath/cloud-examples/raw/master/r/rmagic.ipynb}{\uline{here}}}363364\clearpage365\newhead{Sagemath Cloud}366\section{Sagemath Cloud}367368\href{https://cloud.sagemath.org}{The Sagemath Cloud} is a web-based online369environment for all sorts of computations.370It's not only running371\href{http://www.sagemath.org}{the Sage mathematical software system},372but also R, LaTeX, IPython and much more.373Just open a Linux terminal and explore its capabilities \smiley374375All projects can be \textit{shared with collaborators},376edits in files are \textit{synchronized} in real-time,377and your work is \textit{continuously backed-up}.378379This talk and the examples are part of the380\href{https://github.com/sagemath/cloud-examples}{Sagemath Cloud examples repository}.381382\clearpage383\newhead{Links}384\section{Links}385386\begin{itemize}387\item \href{http://rpy.sourceforge.net/rpy2.html}{\textbf{RPy2}}388\item \href{http://www.ipython.org}{IPython}389\begin{itemize}390\item \href{http://ipython.org/ipython-doc/dev/config/extensions/rmagic.html}{R-``magic''-extension}391\item \href{http://nbviewer.ipython.org/urls/github.com/sagemath/cloud-examples/raw/master/r/rmagic.ipynb}{IPython Rmagic example}392\item \href{http://nbviewer.ipython.org/urls/github.com/ipython/ipython/raw/master/examples/notebooks/R Magics.ipynb}{IPython Documentation Example}393\end{itemize}394\item \href{https://cloud.sagemath.org}{Sagemath Cloud}395\end{itemize}396397398%------------------------------------------------399%------------------------------------------------400401\begin{comment}402\begin{multicols}{2} % Divide text into multiple columns403\mygreen{Sed diam enim, sagittis nec} condimentum sit amet, ullamcorper sit amet libero. \mybrown{Aliquam vel dui orci}, a porta odio. \myred{Nullam id suscipit} ipsum. \myblue{Aenean lobortis} commodo sem, ut commodo leo gravida vitae. Pellentesque vehicula ante iaculis arcu pretium rutrum eget sit amet purus. Integer ornare nulla quis neque ultrices lobortis. Vestibulum ultrices tincidunt libero, quis commodo erat ullamcorper id.404\end{multicols}405406\clearpage407408%------------------------------------------------409410\subsection{Bullet Points and Numbered Lists}411412\begin{itemize}413\item Lorem ipsum dolor sit amet, consectetur adipiscing elit414\item Aliquam blandit faucibus nisi, sit amet dapibus enim tempus eu415\end{itemize}416417\begin{enumerate}418\item Nulla commodo, erat quis gravida posuere, elit lacus lobortis est, quis porttitor odio mauris at libero419\item Nam cursus est eget velit posuere pellentesque420\item Vestibulum faucibus velit a augue condimentum quis convallis nulla gravida421\end{enumerate}422423\clearpage424425%------------------------------------------------426427\subsection{Verbatim}428429How to include a theorem in this presentation:430\begin{verbatim}431\mybox{0.8\textwidth}{432\begin{theorem}[Murphy (1949)]433Anything that can go wrong, will go wrong.434\end{theorem}435}436\end{verbatim}437438\clearpage439440%------------------------------------------------441442443\section{Displaying Information}444445\clearpage446447%------------------------------------------------448449\subsection{Table}450451\begin{table}[h]452\centering453\begin{tabular}{l l l}454\toprule455\textbf{Treatments} & \textbf{Response 1} & \textbf{Response 2}\\456\midrule457Treatment 1 & 0.0003262 & 0.562 \\458Treatment 2 & 0.0015681 & 0.910 \\459Treatment 3 & 0.0009271 & 0.296 \\460\bottomrule461\end{tabular}462\caption{Table caption}463\end{table}464465\clearpage466467%------------------------------------------------468469\subsection{Figure}470471\begin{figure}[h]472\centering\includegraphics[width=0.4\linewidth]{placeholder}473\end{figure}474475\clearpage476477%------------------------------------------------478479\subsection{Theorem}480481The most common definition of \mygreen{Murphy's Law} is as follows.482483\mybox{0.8\textwidth}{ % Example of encapsulating text in a colored box484\begin{theorem}[Murphy (1949)]485Anything that can go wrong, will go wrong.486\end{theorem}487}488489\begin{proof}490A special case of this theorem is proven in the textbook.491\end{proof}492493\begin{remark}494This is a remark.495\end{remark}496497\begin{algorithm}498This is an algorithm.499\end{algorithm}500501\clearpage502503504505%------------------------------------------------506507\thispagestyle{empty} % No slide header and footer508509\bibliographystyle{unsrt}510\bibliography{sample}511512\clearpage513514%------------------------------------------------515516%------------------------------------------------517518\begin{comment}519520\section{Citations}521522An example of the \texttt{\textbackslash cite} command to cite within the presentation:523524This statement requires citation \cite{Smith:2012qr}.525526\clearpage527528529\thispagestyle{empty} % No slide header and footer530531\begin{tikzpicture}[remember picture,overlay] % Background box532\node [xshift=\paperwidth/2,yshift=\paperheight/2] at (current page.south west)[rectangle,fill,inner sep=0pt,minimum width=\paperwidth,minimum height=\paperheight/3,top color=mygreen,bottom color=mygreen]{}; % Change the height of the box, its colors and position on the page here533\end{tikzpicture}534% Text within the box535\begin{flushright}536\vspace{0.6cm}537\color{white}\sffamily538{\bfseries\LARGE Questions?\par} % Request for questions text539\vfill540\end{flushright}541\end{comment}542543%----------------------------------------------------------------------------------------544545\end{document}546%sagemathcloud={"zoom_width":100}547548