\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Niki (worlddoit)} \pdfinfo{ /Title (r-part5.pdf) /Creator (Cheatography) /Author (Niki (worlddoit)) /Subject (r part5 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{566BA3} \definecolor{LightBackground}{HTML}{F4F5F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{r part5 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Niki (worlddoit)} via \textcolor{DarkBackground}{\uline{cheatography.com/170195/cs/35988/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Niki (worlddoit) \\ \uline{cheatography.com/worlddoit} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 9th December, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Environment 1}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{fa-shield\}\} The environment is the data structure that powers scoping. \newline % Row Count 2 (+ 2) \{\{fa-shield\}\} Scoping is the process of connecting names and values. \newline % Row Count 4 (+ 2) \{\{fa-shield\}\} An environment binds a set of names to a set of values. It is essentially a disordered bag of names. \newline % Row Count 7 (+ 3) \{\{fa-shield\}\} Only one environment at the time is active. \newline % Row Count 9 (+ 2) \{\{fa-shield\}\} The active environment is also called the current environment. \newline % Row Count 11 (+ 2) \{\{fa-shield\}\} Upon calling a function, a new environment hosts its execution and then it is garbaged (unless captured). \newline % Row Count 14 (+ 3) \{\{fa-bolt\}\} A {\bf{promise}} contains \newline % Row Count 15 (+ 1) {\bf{1)}} an expression code for the delayed computation, \newline % Row Count 17 (+ 2) {\bf{2)}} an environment in which it is evaluated, and \newline % Row Count 19 (+ 2) {\bf{3)}} a value accessed only by forcing% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Environment 2}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{fa-shield\}\} Functions to treat environments can be found in `rlang` \newline % Row Count 2 (+ 2) \{\{fa-shield\}\} An environment is conceptually similar to a named list \newline % Row Count 4 (+ 2) `\textgreater{} env1 \textless{}- rlang::env(`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `+ x = c(FALSE, TRUE),`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `+ y = "a",`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `+ z = 2.3,`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `+ t = matrix(1:4,2),`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `+ )`\{\{noshy\}\} \newline % Row Count 10 (+ 1) - Every name is unique. \newline % Row Count 11 (+ 1) - Names are not ordered. \newline % Row Count 12 (+ 1) - It has a parent. \newline % Row Count 13 (+ 1) - It is always modified in place and never copied on modify. \newline % Row Count 15 (+ 2) \{\{fa-shield\}\} Track the parents up to the global \newline % Row Count 16 (+ 1) `\textgreater{} rlang::env\_parents(env2)`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `{[}{[}1{]}{]} \textless{}env: 000000000F217108\textgreater{}` \{\{noshy\}\} \newline % Row Count 18 (+ 1) `{[}{[}2{]}{]} \$ \textless{}env: global\textgreater{}` \{\{noshy\}\}% Row Count 19 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scoping and Environments 1}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Name masking}} follows from static (lexical) scoping in the environments, things defined in the current env are used: \newline % Row Count 3 (+ 3) `\textgreater{} rm(list=ls())`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textgreater{} x\textless{}-10; y\textless{}-20; g\textless{}-function(x) x+10`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `\textgreater{} f\textless{}-function(x) \{g\textless{}-function(x) x+1; y\textless{}-1; z\textless{}-2; r\textless{}-x+y+z+g(x)`\{\{noshy\}\} \newline % Row Count 7 (+ 2) `list(current = current\_env(), caller = caller\_env())\}`\{\{noshy\}\} \newline % Row Count 9 (+ 2) `\textgreater{} env \textless{}- f(x = 7)`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `\textgreater{} env\_print(env\$current)`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `\textless{}environment: 0000000006124F60\textgreater{}`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `parent: \textless{}environment: global\textgreater{}`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `bindings:`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `* r: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `* z: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `* y: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `* g: \textless{}fn\textgreater{} \#this is the g defined in the current env`\{\{noshy\}\} \newline % Row Count 19 (+ 2) `* x: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 20 (+ 1) `\textgreater{} env\_print(env\$caller)`\{\{noshy\}\} \newline % Row Count 21 (+ 1) `\textless{}environment: global\textgreater{}`\{\{noshy\}\} \newline % Row Count 22 (+ 1) `parent: \textless{}environment: package:rlang\textgreater{}`\{\{noshy\}\} \newline % Row Count 23 (+ 1) `bindings:`\{\{noshy\}\} \newline % Row Count 24 (+ 1) `* x: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 25 (+ 1) `* y: \textless{}dbl\textgreater{}`\{\{noshy\}\} \newline % Row Count 26 (+ 1) `* env: \textless{}named list\textgreater{}`\{\{noshy\}\} \newline % Row Count 27 (+ 1) `* .Random.seed: \textless{}int\textgreater{}`\{\{noshy\}\} \newline % Row Count 28 (+ 1) `* f: \textless{}fn\textgreater{}`\{\{noshy\}\} \newline % Row Count 29 (+ 1) `* g: \textless{}fn\textgreater{}`\{\{noshy\}\} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scoping and Environments 1 (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{} g`\{\{noshy\}\} \newline % Row Count 1 (+ 1) `function(x) x+10`\{\{noshy\}\} \newline % Row Count 2 (+ 1) `\textgreater{} env\$current\$g`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `function(x) x+1`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textless{}environment: 0x0000000006124f60\textgreater{}`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `\textgreater{} c(env\$current\$z,env\$current\$r)`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `{[}1{]} 2 18`\{\{noshy\}\}% Row Count 7 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(*** Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{1}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scoping and Environments 2}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Dynamic lookup names}} that are not defined in the current \newline % Row Count 2 (+ 2) environment are searched in the parents \newline % Row Count 3 (+ 1) `\textgreater{} rm(list=ls())`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textgreater{} y \textless{}- 10`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `\textgreater{} f \textless{}- function(x) \{z\textless{}-2; r\textless{}-x+y+z`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `+ list(current = current\_env(), caller = caller\_env())`\{\{noshy\}\} \newline % Row Count 8 (+ 2) `+ \}`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `\textgreater{} env \textless{}- f(7)`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `\textgreater{} ls(env\$current)`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `{[}1{]} "r" "x" "z"`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `\textgreater{} env\$current\$y`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `NULL`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `\textgreater{} ls(env\$caller)`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `{[}1{]} "env" "f" "y"`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `\textgreater{} ls() \#`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `{[}1{]} "env" "f" "y"`\{\{noshy\}\} \newline % Row Count 18 (+ 1) `\textgreater{} codetools::findGlobals(f)`\{\{noshy\}\} \newline % Row Count 19 (+ 1) `{[}1{]} "\{" "+" "\textless{}-" "y"`\{\{noshy\}\}% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(***Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{1 (copy)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scoping and Environments 3}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{What happens in the function, stays in the function}} \newline % Row Count 2 (+ 2) `\textgreater{} rm(list=ls())`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\textgreater{} f \textless{}- function()\{a\textless{}-1; current\_env()\}`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textgreater{} f1 \textless{}- function()\{a\textless{}-2; x\textless{}\textless{}-a; current\_env()\}`\{\{noshy\}\} \newline % Row Count 6 (+ 2) `\textgreater{} f2 \textless{}- function()\{a\textless{}-3; a\textless{}\textless{}-a; current\_env()\}`\{\{noshy\}\} \newline % Row Count 8 (+ 2) `\textgreater{} env \textless{}- f(); ls(env); env\$a`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `{[}1{]} "a"`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `\textgreater{} ls()`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `{[}1{]} "env" "f" "f1" "f2"`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `\textgreater{} env1 \textless{}- f1(); ls(env1); env1\$a`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `{[}1{]} "a"`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `{[}1{]} 2`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `\textgreater{} ls(); x`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `{[}1{]} "env" "env1" "f" "f1" "f2" "x"`\{\{noshy\}\} \newline % Row Count 18 (+ 1) `{[}1{]} 2`\{\{noshy\}\} \newline % Row Count 19 (+ 1) `\textgreater{} env2 \textless{}- f2(); ls(env2); env2\$a`\{\{noshy\}\} \newline % Row Count 20 (+ 1) `{[}1{]} "a"`\{\{noshy\}\} \newline % Row Count 21 (+ 1) `{[}1{]} 3`\{\{noshy\}\} \newline % Row Count 22 (+ 1) `\textgreater{} ls(); a`\{\{noshy\}\} \newline % Row Count 23 (+ 1) `{[}1{]} "a" "env" "env1" "env2" "f" "f1" "f2" "x"`\{\{noshy\}\} \newline % Row Count 25 (+ 2) `{[}1{]} 3`\{\{noshy\}\}% Row Count 26 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(***Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scoping and Environments 4}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Non-functions objects are ignored in function calls}} \newline % Row Count 2 (+ 2) `\textgreater{} rm(list=ls())`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\textgreater{} x\textless{}-10; y\textless{}-20; g\textless{}-function(x) x+10`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textgreater{} f \textless{}- function(x) \{g\textless{}-1; y\textless{}-1; z\textless{}-2; r\textless{}-x+y+z+g(g)`\{\{noshy\}\} \newline % Row Count 6 (+ 2) `+ list(current = current\_env(), caller = caller\_env())`\{\{noshy\}\} \newline % Row Count 8 (+ 2) `+ \}`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `\textgreater{} env\textless{}-f(7)`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `\textgreater{} ls(env\$current)`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `{[}1{]} "g" "r" "x" "y" "z"`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `\textgreater{} ls(env\$caller) \#or simply ls()`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `{[}1{]} "env" "f" "g" "x" "y"`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `\textgreater{} env\$current\$g`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `\textgreater{} env\$caller\$g \#or simply g`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `function(x) x+10`\{\{noshy\}\} \newline % Row Count 18 (+ 1) `\textgreater{} fn\_env(g)`\{\{noshy\}\} \newline % Row Count 19 (+ 1) `\textless{}environment: R\_GlobalEnv\textgreater{}`\{\{noshy\}\}% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(***Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}