<Headline>## R Script Sample File</Headline><br/>
<Headline>## Source: http://www.rexamples.com</Headline><br/>
<Normal Text></Normal Text><br/>
<Headline>## Basics</Headline><br/>
<Normal Text>a </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Int>42</Int><br/>
<Normal Text>A </Normal Text><Assign><-</Assign><Normal Text> a </Normal Text><Operator>*</Operator><Normal Text> </Normal Text><Int>2</Int><Normal Text>  </Normal Text><Comment># R is case sensitive</Comment><br/>
<Keyword>print</Keyword><Symbol>(</Symbol><Normal Text>a</Normal Text><Symbol>)</Symbol><br/>
<Keyword>cat</Keyword><Symbol>(</Symbol><Normal Text>A, </Normal Text><String>"</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><Normal Text> </Normal Text><Comment># "84" is concatenated with "\n"</Comment><br/>
<Control Structure>if</Control Structure><Symbol>(</Symbol><Normal Text>A</Normal Text><Operator>></Operator><Normal Text>a</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Comment># true, 84 > 42</Comment><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  </Normal Text><Keyword>cat</Keyword><Symbol>(</Symbol><Normal Text>A, </Normal Text><String>">"</String><Normal Text>, a, </Normal Text><String>"</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><br/>
<Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Functions</Headline><br/>
<Normal Text>Square </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Control Structure>function</Control Structure><Symbol>(</Symbol><Normal Text>x</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>  </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Normal Text>x</Normal Text><Operator>^</Operator><Int>2</Int><Symbol>)</Symbol><br/>
<Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Keyword>print</Keyword><Symbol>(</Symbol><Keyword>Square</Keyword><Symbol>(</Symbol><Int>4</Int><Symbol>))</Symbol><br/>
<Keyword>print</Keyword><Symbol>(</Symbol><Keyword>Square</Keyword><Symbol>(</Symbol><Identifier>x=</Identifier><Int>4</Int><Symbol>))</Symbol><Normal Text> </Normal Text><Comment># same thing</Comment><br/>
<Normal Text></Normal Text><br/>
<Keyword>DoSomething</Keyword><Symbol>(</Symbol><Identifier>color=</Identifier><String>"red"</String><Normal Text>,</Normal Text><Identifier>number=</Identifier><Int>55</Int><Symbol>)</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Countdown</Headline><br/>
<Normal Text>countdown </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Control Structure>function</Control Structure><Symbol>(</Symbol><Normal Text>from</Normal Text><Symbol>)</Symbol><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  </Normal Text><Keyword>print</Keyword><Symbol>(</Symbol><Normal Text>from</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>while</Control Structure><Symbol>(</Symbol><Normal Text>from</Normal Text><Operator>!=</Operator><Int>0</Int><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>Sys.sleep</Keyword><Symbol>(</Symbol><Int>1</Int><Symbol>)</Symbol><br/>
<Normal Text>    from </Normal Text><Assign><-</Assign><Normal Text> from </Normal Text><Operator>-</Operator><Normal Text> </Normal Text><Int>1</Int><br/>
<Normal Text>    </Normal Text><Keyword>print</Keyword><Symbol>(</Symbol><Normal Text>from</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Keyword>countdown</Keyword><Symbol>(</Symbol><Int>5</Int><Symbol>)</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Reading user input</Headline><br/>
<Normal Text>readinteger </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Control Structure>function</Control Structure><Symbol>()</Symbol><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  n </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>readline</Keyword><Symbol>(</Symbol><Identifier>prompt=</Identifier><String>"Enter an integer: "</String><Symbol>)</Symbol><br/>
<Normal Text>  n </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>as.integer</Keyword><Symbol>(</Symbol><Normal Text>n</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>if</Control Structure><Normal Text> </Normal Text><Symbol>(</Symbol><Keyword>is.na</Keyword><Symbol>(</Symbol><Normal Text>n</Normal Text><Symbol>)){</Symbol><br/>
<Normal Text>    n </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>readinteger</Keyword><Symbol>()</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Normal Text>  </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Normal Text>n</Normal Text><Symbol>)</Symbol><br/>
<Symbol>}</Symbol><br/>
<Keyword>print</Keyword><Symbol>(</Symbol><Keyword>readinteger</Keyword><Symbol>())</Symbol><br/>
<Normal Text></Normal Text><br/>
<Normal Text>readinteger </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Control Structure>function</Control Structure><Symbol>()</Symbol><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  n </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>readline</Keyword><Symbol>(</Symbol><Identifier>prompt=</Identifier><String>"Enter an integer: "</String><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>if</Control Structure><Symbol>(</Symbol><Operator>!</Operator><Keyword>grepl</Keyword><Symbol>(</Symbol><String>"^[0-9]+$"</String><Normal Text>,n</Normal Text><Symbol>))</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Keyword>readinteger</Keyword><Symbol>())</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Normal Text>  </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Keyword>as.integer</Keyword><Symbol>(</Symbol><Normal Text>n</Normal Text><Symbol>))</Symbol><br/>
<Symbol>}</Symbol><br/>
<Keyword>print</Keyword><Symbol>(</Symbol><Keyword>readinteger</Keyword><Symbol>())</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Guess a random number game</Headline><br/>
<Normal Text>readinteger </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Control Structure>function</Control Structure><Symbol>()</Symbol><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  n </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>readline</Keyword><Symbol>(</Symbol><Identifier>prompt=</Identifier><String>"Enter an integer: "</String><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>if</Control Structure><Symbol>(</Symbol><Operator>!</Operator><Keyword>grepl</Keyword><Symbol>(</Symbol><String>"^[0-9]+$"</String><Normal Text>,n</Normal Text><Symbol>))</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Keyword>readinteger</Keyword><Symbol>())</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Normal Text>  </Normal Text><Keyword>return</Keyword><Symbol>(</Symbol><Keyword>as.integer</Keyword><Symbol>(</Symbol><Normal Text>n</Normal Text><Symbol>))</Symbol><br/>
<Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Comment># real program start here</Comment><br/>
<Normal Text></Normal Text><br/>
<Normal Text>num </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>round</Keyword><Symbol>(</Symbol><Keyword>runif</Keyword><Symbol>(</Symbol><Int>1</Int><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>*</Operator><Normal Text> </Normal Text><Int>100</Int><Normal Text>, </Normal Text><Identifier>digits =</Identifier><Normal Text> </Normal Text><Int>0</Int><Symbol>)</Symbol><br/>
<Normal Text>guess </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Operator>-</Operator><Int>1</Int><br/>
<Normal Text></Normal Text><br/>
<Keyword>cat</Keyword><Symbol>(</Symbol><String>"Guess a number between 0 and 100.</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><br/>
<Normal Text></Normal Text><br/>
<Control Structure>while</Control Structure><Symbol>(</Symbol><Normal Text>guess </Normal Text><Operator>!=</Operator><Normal Text> num</Normal Text><Symbol>)</Symbol><br/>
<Symbol>{</Symbol><br/>
<Normal Text>  guess </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>readinteger</Keyword><Symbol>()</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>if</Control Structure><Normal Text> </Normal Text><Symbol>(</Symbol><Normal Text>guess </Normal Text><Operator>==</Operator><Normal Text> num</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>cat</Keyword><Symbol>(</Symbol><String>"Congratulations,"</String><Normal Text>, num, </Normal Text><String>"is right.</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>else</Control Structure><Normal Text> </Normal Text><Control Structure>if</Control Structure><Normal Text> </Normal Text><Symbol>(</Symbol><Normal Text>guess </Normal Text><Operator><</Operator><Normal Text> num</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>cat</Keyword><Symbol>(</Symbol><String>"It's bigger!</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Normal Text>  </Normal Text><Control Structure>else</Control Structure><Normal Text> </Normal Text><Control Structure>if</Control Structure><Symbol>(</Symbol><Normal Text>guess </Normal Text><Operator>></Operator><Normal Text> num</Normal Text><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>{</Symbol><br/>
<Normal Text>    </Normal Text><Keyword>cat</Keyword><Symbol>(</Symbol><String>"It's smaller!</String><String Char>\n</String Char><String>"</String><Symbol>)</Symbol><br/>
<Normal Text>  </Normal Text><Symbol>}</Symbol><br/>
<Symbol>}</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Lists</Headline><br/>
<Keyword>sum</Keyword><Symbol>(</Symbol><Int>0</Int><Operator>:</Operator><Int>9</Int><Symbol>)</Symbol><br/>
<Keyword>append</Keyword><Symbol>(</Symbol><Normal Text>LETTERS</Normal Text><Symbol>[</Symbol><Int>1</Int><Operator>:</Operator><Int>13</Int><Symbol>]</Symbol><Normal Text>,letters</Normal Text><Symbol>[</Symbol><Int>14</Int><Operator>:</Operator><Int>26</Int><Symbol>])</Symbol><br/>
<Keyword>c</Keyword><Symbol>(</Symbol><Int>1</Int><Normal Text>,</Normal Text><Int>6</Int><Normal Text>,</Normal Text><Int>4</Int><Normal Text>,</Normal Text><Int>9</Int><Symbol>)</Symbol><Operator>*</Operator><Int>2</Int><br/>
<Normal Text>something </Normal Text><Assign><-</Assign><Normal Text> </Normal Text><Keyword>c</Keyword><Symbol>(</Symbol><Int>1</Int><Normal Text>,</Normal Text><Int>4</Int><Normal Text>,letters</Normal Text><Symbol>[</Symbol><Int>2</Int><Symbol>])</Symbol><Normal Text>  </Normal Text><Comment># indices start at one, you get (1,4,"b")</Comment><br/>
<Keyword>length</Keyword><Symbol>(</Symbol><Normal Text>something</Normal Text><Symbol>)</Symbol><br/>
<Normal Text></Normal Text><br/>
<Headline>## Pipe</Headline><br/>
<Normal Text>diamonds </Normal Text><Operator>%>%</Operator><br/>
<Normal Text>  </Normal Text><Keyword>filter</Keyword><Symbol>(</Symbol><Normal Text>price </Normal Text><Operator>></Operator><Normal Text> </Normal Text><Int>1000</Int><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>%>%</Operator><br/>
<Normal Text>  </Normal Text><Keyword>group_by</Keyword><Symbol>(</Symbol><Normal Text>cut</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>%>%</Operator><br/>
<Normal Text>  </Normal Text><Keyword>tally</Keyword><Symbol>()</Symbol><Normal Text> </Normal Text><Operator>%>%</Operator><Normal Text> </Normal Text><br/>
<Normal Text>  </Normal Text><Keyword>rename</Keyword><Symbol>(</Symbol><Identifier>tally =</Identifier><Normal Text> n</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>%>%</Operator><Normal Text> </Normal Text><br/>
<Normal Text>  </Normal Text><Keyword>arrange</Keyword><Symbol>(</Symbol><Operator>-</Operator><Normal Text>tally</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>%>%</Operator><Normal Text> </Normal Text><br/>
<Normal Text>  </Normal Text><Keyword>mutate</Keyword><Symbol>(</Symbol><Identifier>pct =</Identifier><Normal Text> tally </Normal Text><Operator>/</Operator><Normal Text> </Normal Text><Keyword>sum</Keyword><Symbol>(</Symbol><Normal Text>tally</Normal Text><Symbol>))</Symbol><Normal Text> </Normal Text><Assign>-></Assign><Normal Text> df</Normal Text><br/>
<Normal Text></Normal Text><br/>
<Headline>## ggplot2 </Headline><br/>
<Normal Text>plot </Normal Text><Assign>=</Assign><Normal Text> </Normal Text><Keyword>ggplot</Keyword><Symbol>(</Symbol><Normal Text>diamonds, </Normal Text><Keyword>aes</Keyword><Symbol>(</Symbol><Identifier>x =</Identifier><Normal Text> price, </Normal Text><Identifier>y =</Identifier><Normal Text> carat</Normal Text><Symbol>))</Symbol><Normal Text> </Normal Text><Operator>+</Operator><br/>
<Normal Text>  </Normal Text><Keyword>geom_point</Keyword><Symbol>(</Symbol><Identifier>alpha =</Identifier><Normal Text> </Normal Text><Float>0.3</Float><Normal Text>, </Normal Text><Identifier>colour =</Identifier><Normal Text> </Normal Text><String>'steelblue'</String><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>+</Operator><br/>
<Normal Text>  </Normal Text><Keyword>labs</Keyword><Symbol>(</Symbol><br/>
<Normal Text>    </Normal Text><Identifier>title =</Identifier><Normal Text> </Normal Text><String>"ggplot diamonds"</String><Normal Text>,</Normal Text><br/>
<Normal Text>    </Normal Text><Identifier>x =</Identifier><Normal Text> </Normal Text><String>"Price, $"</String><Normal Text>,</Normal Text><br/>
<Normal Text>    </Normal Text><Identifier>y =</Identifier><Normal Text> </Normal Text><String>"Carat"</String><br/>
<Normal Text>  </Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>+</Operator><br/>
<Normal Text>  </Normal Text><Keyword>facet_wrap</Keyword><Symbol>(</Symbol><Operator>~</Operator><Normal Text> cut</Normal Text><Symbol>)</Symbol><Normal Text> </Normal Text><Operator>+</Operator><br/>
<Normal Text>  </Normal Text><Keyword>theme_minimal</Keyword><Symbol>()</Symbol><br/>
<Normal Text></Normal Text><br/>
<Normal Text>plot </Normal Text><Operator>+</Operator><Normal Text> </Normal Text><Keyword>coord_flip</Keyword><Symbol>()</Symbol><br/>
