Thanks to Brendan O’Connor, this cheatsheet aims to be a quick reference of Scala syntactic constructions. Licensed by Brendan O’Connor under a CC-BY-SA 3.0 license.
| variables | |
Good | Variable. |
Bad | Constant. |
| Explicit type. | |
| functions | |
| Good Bad | Define function. Hidden error: without = it’s a procedure returning Unit; causes havoc. Deprecated in Scala 2.13. |
| Good Bad | Define function. Syntax error: need types for every arg. |
| Type alias. | |
| vs. | Call-by-value. Call-by-name (lazy parameters). |
| Anonymous function. | |
| vs. | Anonymous function: underscore is positionally matched arg. |
| Anonymous function: to use an arg twice, have to name it. | |
| Anonymous function: block style returns last expression. | |
| Anonymous functions: pipeline style (or parens too). | |
| Anonymous functions: to pass in multiple blocks, need outer parens. | |
| Currying, obvious syntax. | |
| Currying, obvious syntax. | |
| Currying, sugar syntax. But then: | |
| Need trailing underscore to get the partial, only for the sugar version. | |
| Generic type. | |
| Infix sugar. | |
| Varargs. | |
| packages | |
| Wildcard import. | |
| Selective import. | |
| Renaming import. | |
Import all from java.util except Date. | |
| At start of file: Packaging by scope: Package singleton: | Declare a package. |
| data structures | |
Tuple literal (Tuple3). | |
| Destructuring bind: tuple unpacking via pattern matching. | |
| Bad | Hidden error: each assigned to the entire tuple. |
| List (immutable). | |
| Paren indexing (slides). | |
| Cons. | |
| same as | Range sugar. |
| Empty parens is singleton value of the Unit type. Equivalent to void in C and Java. | |
| control constructs | |
| Conditional. | |
same as | Conditional sugar. |
| While loop. | |
| Do-while loop. | |
| Break (slides). | |
same as | For-comprehension: filter/map. |
same as | For-comprehension: destructuring bind. |
same as | For-comprehension: cross product. |
For-comprehension: imperative-ish.sprintf style. | |
| For-comprehension: iterate including the upper bound. | |
| For-comprehension: iterate omitting the upper bound. | |
| pattern matching | |
| Good Bad | Use case in function args for pattern matching. |
| Bad | v42 is interpreted as a name matching any Int value, and “42” is printed. |
| Good | `v42` with backticks is interpreted as the existing val v42, and “Not 42” is printed. |
| Good | UppercaseVal is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within UppercaseVal is checked against 3, and “Not 42” is printed. |
| object orientation | |
Constructor params - x is only available in class body. | |
| Constructor params - automatic public member defined. | |
| Constructor is class body. Declare a public member. Declare a gettable but not settable member. Declare a private member. Alternative constructor. | |
| Anonymous class. | |
| Define an abstract class (non-createable). | |
| Define an inherited class. | |
| Inheritance and constructor params (wishlist: automatically pass-up params by default). | |
| Define a singleton (module-like). | |
| Traits. Interfaces-with-implementation. No constructor params. mixin-able. | |
| Multiple traits. | |
| Must declare method overrides. | |
| Create object. | |
| Bad Good | Type error: abstract type. Instead, convention: callable factory shadowing the type. |
| Class literal. | |
| Type check (runtime). | |
| Type cast (runtime). | |
| Ascription (compile time). | |
| options | |
| Construct a non empty optional value. | |
| The singleton empty optional value. | |
| but | Null-safe optional value factory. |
| same as | Explicit type for empty optional value. Factory for empty optional value. |
| Pipeline style. | |
| For-comprehension syntax. | |
| same as | Apply a function on the optional value. |
| same as | Same as map but function must return an optional value. |
| same as | Extract nested option. |
| same as | Apply a procedure on optional value. |
| same as | Apply function on optional value, return default if empty. |
| same as | Apply partial pattern match on optional value. |
| same as | true if not empty. |
| same as | true if empty. |
| same as | true if not empty. |
| same as | 0 if empty, otherwise 1. |
| same as | Evaluate and return alternate optional value if empty. |
| same as | Evaluate and return default value if empty. |
| same as | Return value, throw exception if empty. |
| same as | Return value, null if empty. |
| same as | Optional value satisfies predicate. |
| same as | Optional value doesn't satisfy predicate. |
| same as | Apply predicate on optional value or false if empty. |
| same as | Apply predicate on optional value or true if empty. |
| same as | Checks if value equals optional value or false if empty. |
Cheat Sheet RStudio® is a trademark of RStudio, Inc. CC BY RStudio. info@rstudio.com. 844-448-1212. rstudio.com Syntax - Helpful conventions for wrangling dplyr::tbldf(iris) w Converts data to tbl class. Tbl’s are easier to examine than data frames. R displays only the data that fits onscreen: dplyr::glimpse(iris). R cheat sheet By @MaryJoWebster March 2019. Data frame – This is from Base R; this is used for storing data tables. Tidyverse makes a “tibble” which is supposed to be a slightly better version of a data frame.
I reproduce some of the plots from Rstudio’s ggplot2 cheat sheet using Base R graphics. I didn’t try to pretty up these plots, but you should.
I use this dataset
The main functions that I generally use for plotting are

plot: Makes scatterplots, line plots, among other plots.lines: Adds lines to an already-made plot.par: Change plotting options.hist: Makes a histogram.boxplot: Makes a boxplot.text: Adds text to an already-made plot.legend: Adds a legend to an already-made plot.mosaicplot: Makes a mosaic plot.barplot: Makes a bar plot.jitter: Adds a small value to data (so points don’t overlap on a plot).rug: Adds a rugplot to an already-made plot.polygon: Adds a shape to an already-made plot.points: Adds a scatterplot to an already-made plot.mtext: Adds text on the edges of an already-made plot.table: Builds frequency and two-way tables.density: Calculates the density.loess: Calculates a smooth line.predict: Predicts new values based on a model.All of the plotting functions have arguments that control the way the plot looks. You should read about these arguments. In particular, read carefully the help page ?plot.default. Useful ones are:

main: This controls the title.xlab, ylab: These control the x and y axis labels.col: This will control the color of the lines/points/areas.cex: This will control the size of points.pch: The type of point (circle, dot, triangle, etc…)lwd: Line width.lty: Line type (solid, dashed, dotted, etc…).Barplot
Different type of bar plot
Scatterplot
Jitter points to account for overlaying points.
Add a rug plot
Add a Loess Smoother
Loess smoother with upper and lower 95% confidence bands
Loess smoother with upper and lower 95% confidence bands and that fancy shading from ggplot2.
Add text to a plot
Mosaic Plot

Color code a scatterplot by a categorical variable and add a legend.
par sets the graphics options, where mfrow is the parameter controling the facets.
The first line sets the new options and saves the old options in the list old_options. The last line reinstates the old options.
This R Markdown site was created with workflowr
