Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

R Markdown Guide and Function Reference, Cheat Sheet of Data Mining

A comprehensive guide and reference for r markdown, a popular tool for creating dynamic and reproducible documents. It covers various options, chunk options, important chunk options, data manipulation functions, graph building templates, and general purpose scales. The document also introduces spark connection, spark dataframe, and sparklyr, an r interface for machine learning. It is a valuable resource for university students, high school students, and lifelong learners interested in data analysis, statistics, and machine learning.

Typology: Cheat Sheet

2022/2023

Uploaded on 02/15/2024

ram-dindu
ram-dindu 🇮🇳

1 document

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2019 / CHEATSHEETS
rstudio::conf
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download R Markdown Guide and Function Reference and more Cheat Sheet Data Mining in PDF only on Docsity!

2019 /^ CHEATSHEETS

rstudio::conf

RStudio IDE : : CHEAT SHEET

Write Code Pro Features

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.99.832 • Updated: 2016-

Turn project into package, Enable roxygen documentation with Tools > Project Options > Build Tools

Roxygen guide at Help > Roxygen Quick Reference

File > New Project > New Directory > R Package

Share Project with Collaborators

Active shared collaborators

Select R Version

Start new R Session in current project

Close R Session in project

TH J

RStudio saves the call history, workspace, and working directory associated with a project. It reloads each when you re-open a project.

Name of current project

View() opens spreadsheet like view of data set

Sort by values

Filter rows by value or value range

Search for value

Viewer Pane displays HTML content, such as Shiny apps, RMarkdown reports, and interactive visualizations

Stop Shiny app

Publish to shinyapps.io, rpubs, RSConnect, …

Refresh

RStudio opens documentation in a dedicated Help pane

Home page of helpful links

Search within help file

Search for help file

GUI Package manager lists every installed package

Click to load package with library(). Unclick to detach package with detach()

Delete from library

Install Packages

Update Packages

Create reproducible package library for your project

RStudio opens plots in a dedicated Plots pane

Navigate recent plots

Open in window

Export plot

Delete plot

Delete all plots

Package version installed

Examine variables in executing environment

Open with debug(), browser(), or a breakpoint. RStudio will open the debugger mode when it encounters a breakpoint while executing code.

Open traceback to examine the functions that R called before the error occurred

Launch debugger mode from origin of error

Click next to line number to add/remove a breakpoint.

Select function in traceback to debug

Highlighted line shows where execution has paused

Run commands in environment where execution has paused

Step through code one line at a time

Step into and out of functions to run

Resume execution

Quit debug mode

Open Shiny, R Markdown, knitr, Sweave, LaTeX, .Rd files and more in Source Pane

Check spelling

Render output

Choose output format

Choose output location

Insert code chunk

Jump to previous chunk

Jump to next chunk

Run selected lines

Publish to server

Show file outline

Set knitr chunk options

Run this and all previous code chunks

Run this code chunk

Jump to chunk

RStudio recognizes that files named app.R , server.R , ui.R , and global.R belong to a shiny app

Run app

Choose location to view app

Publish to shinyapps.io or server

Manage publish accounts

Access markdown guide at Help > Markdown Quick Reference

Stage files:

Show file diff

Commit staged files

Push/Pull to remote

View History

current branch

  • Added
  • Deleted
  • Modified
  • Renamed
  • Untracked

Turn on at Tools > Project Options > Git/SVN

Open shell to type commands

A D M R ?

Search inside environment

Syntax highlighting based on your file's extension

Code diagnostics that appear in the margin. Hover over diagnostic symbols for details.

Tab completion to finish function names, file paths, arguments, and more.

Multi-language code snippets to quickly use common blocks of code.

Open in new window

Save Find and replace

Compile as notebook

Run selected code

Re-run previous code

Source with or without Echo

Show file outline

Jump to function in file Change file type

Navigate tabs

A File browser keyed to your working directory. Click on file or directory name to open.

Path to displayed directory

Upload file

Create folder

Delete file

Rename file

Change directory

Displays saved objects by type with short description

View function source code

View in data viewer

Load workspace

Save workspace

Import data with wizard

Delete all saved objects

Display objects as list or grid

Choose environment to display from list of parent environments

History of past commands to run/copy

Display .RPres slideshows File > New File > R Presentation

Working Directory

Maximize, minimize panes

Drag pane boundaries

TH J

Cursors of shared users

File > New Project

Press  to see command history

Multiple cursors/column selection with Alt + mouse drag.

Documents and Apps R Support

PROJECT SYSTEM

Debug Mode Version Control with Git or SVN

Package Writing

RStudio IDE : : CHEAT SHEET

Write Code Pro Features

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.99.832 • Updated: 2016-

Turn project into package, Enable roxygen documentation with Tools > Project Options > Build Tools

Roxygen guide at Help > Roxygen Quick Reference

File > New Project > New Directory > R Package

Share Project with Collaborators

Active shared collaborators

Select R Version

Start new R Session in current project

Close R Session in project

TH J

RStudio saves the call history, workspace, and working directory associated with a project. It reloads each when you re-open a project.

Name of current project

View() opens spreadsheet like view of data set

Sort by values

Filter rows by value or value range

Search for value

Viewer Pane displays HTML content, such as Shiny apps, RMarkdown reports, and interactive visualizations

Stop Shiny app

Publish to shinyapps.io, rpubs, RSConnect, …

Refresh

RStudio opens documentation in a dedicated Help pane

Home page of helpful links

Search within help file

Search for help file

GUI Package manager lists every installed package

Click to load package with library(). Unclick to detach package with detach()

Delete from library

Install Packages

Update Packages

Create reproducible package library for your project

RStudio opens plots in a dedicated Plots pane

Navigate recent plots

Open in window

Export plot

Delete plot

Delete all plots

Package version installed

Examine variables in executing environment

Open with debug(), browser(), or a breakpoint. RStudio will open the debugger mode when it encounters a breakpoint while executing code.

Open traceback to examine the functions that R called before the error occurred

Launch debugger mode from origin of error

Click next to line number to add/remove a breakpoint.

Select function in traceback to debug

Highlighted line shows where execution has paused

Run commands in environment where execution has paused

Step through code one line at a time

Step into and out of functions to run

Resume execution

Quit debug mode

Open Shiny, R Markdown, knitr, Sweave, LaTeX, .Rd files and more in Source Pane

Check spelling

Render output

Choose output format

Choose output location

Insert code chunk

Jump to previous chunk

Jump to next chunk

Run selected lines

Publish to server

Show file outline

Set knitr chunk options

Run this and all previous code chunks

Run this code chunk

Jump to chunk

RStudio recognizes that files named app.R , server.R , ui.R , and global.R belong to a shiny app

Run app

Choose location to view app

Publish to shinyapps.io or server

Manage publish accounts

Access markdown guide at Help > Markdown Quick Reference

Stage files:

Show file diff

Commit staged files

Push/Pull to remote

View History

current branch

  • Added
  • Deleted
  • Modified
  • Renamed
  • Untracked

Turn on at Tools > Project Options > Git/SVN

Open shell to type commands

A D M R ?

Search inside environment

Syntax highlighting based on your file's extension

Code diagnostics that appear in the margin. Hover over diagnostic symbols for details.

Tab completion to finish function names, file paths, arguments, and more.

Multi-language code snippets to quickly use common blocks of code.

Open in new window

Save Find and replace

Compile as notebook

Run selected code

Re-run previous code

Source with or without Echo

Show file outline

Jump to function in file Change file type

Navigate tabs

A File browser keyed to your working directory. Click on file or directory name to open.

Path to displayed directory

Upload file

Create folder

Delete file

Rename file

Change directory

Displays saved objects by type with short description

View function source code

View in data viewer

Load workspace

Save workspace

Import data with wizard

Delete all saved objects

Display objects as list or grid

Choose environment to display from list of parent environments

History of past commands to run/copy

Display .RPres slideshows File > New File > R Presentation

Working Directory

Maximize, minimize panes

Drag pane boundaries

TH J

Cursors of shared users

File > New Project

Press  to see command history

Multiple cursors/column selection with Alt + mouse drag.

Documents and Apps R Support

PROJECT SYSTEM

Debug Mode Version Control with Git or SVN

Package Writing

RStudio IDE : : CHEAT SHEET

Write Code Pro Features

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.99.832 • Updated: 2016-

Turn project into package, Enable roxygen documentation with Tools > Project Options > Build Tools

Roxygen guide at Help > Roxygen Quick Reference

File > New Project > New Directory > R Package

Share Project with Collaborators

Active shared collaborators

Select R Version

Start new R Session in current project Close R Session in project

TH J

RStudio saves the call history, workspace, and working directory associated with a project. It reloads each when you re-open a project.

Name of current project

View() opens spreadsheet like view of data set

Sort by values

Filter rows by value or value range

Search for value

Viewer Pane displays HTML content, such as Shiny apps, RMarkdown reports, and interactive visualizations

Stop Shiny app

Publish to shinyapps.io, rpubs, RSConnect, …

Refresh

RStudio opens documentation in a dedicated Help pane

Home page of helpful links

Search within help file

Search for help file

GUI Package manager lists every installed package

Click to load package with library(). Unclick to detach package with detach()

Delete from library

Install Packages

Update Packages

Create reproducible package library for your project

RStudio opens plots in a dedicated Plots pane

Navigate recent plots

Open in window

Export plot

Delete plot

Delete all plots

Package version installed

Examine variables in executing environment

Open with debug(), browser(), or a breakpoint. RStudio will open the debugger mode when it encounters a breakpoint while executing code.

Open traceback to examine the functions that R called before the error occurred

Launch debugger mode from origin of error

Click next to line number to add/remove a breakpoint.

Select function in traceback to debug

Highlighted line shows where execution has paused

Run commands in environment where execution has paused

Step through code one line at a time

Step into and out of functions to run

Resume execution

Quit debug mode

Open Shiny, R Markdown, knitr, Sweave, LaTeX, .Rd files and more in Source Pane

Check spelling

Render output

Choose output format

Choose output location

Insert code chunk

Jump to previous chunk

Jump to next chunk

Run selected lines

Publish to server

Show file outline

Set knitr chunk options

Run this and all previous code chunks

Run this code chunk

Jump to chunk

RStudio recognizes that files named app.R , server.R , ui.R , and global.R belong to a shiny app

Run app

Choose location to view app

Publish to shinyapps.io or server

Manage publish accounts

Access markdown guide at Help > Markdown Quick Reference

Stage files:

Show file diff

Commit staged files

Push/Pull to remote

View History

current branch

  • Added
  • Deleted
  • Modified
  • Renamed
  • Untracked

Turn on at Tools > Project Options > Git/SVN

Open shell to type commands

A D M R ?

Search inside environment

Syntax highlighting based on your file's extension

Code diagnostics that appear in the margin. Hover over diagnostic symbols for details.

Tab completion to finish function names, file paths, arguments, and more.

Multi-language code snippets to quickly use common blocks of code.

Open in new window

Save Find and replace

Compile as notebook

Run selected code

Re-run previous code

Source with or without Echo

Show file outline

Jump to function in file Change file type

Navigate tabs

A File browser keyed to your working directory. Click on file or directory name to open.

Path to displayed directory

Upload file

Create folder

Delete file

Rename file

Change directory

Displays saved objects by type with short description

View function source code

View in data viewer

Load workspace

Save workspace

Import data with wizard

Delete all saved objects

Display objects as list or grid

Choose environment to display from list of parent environments

History of past commands to run/copy

Display .RPres slideshows File > New File > R Presentation

Working Directory

Maximize, minimize panes

Drag pane boundaries

TH J

Cursors of shared users

File > New Project

Press  to see command history

Multiple cursors/column selection with Alt + mouse drag.

Documents and Apps R Support

PROJECT SYSTEM

Debug Mode Version Control with Git or SVN

Package Writing

RStudio IDE : : CHEAT SHEET

Write Code Pro Features

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.99.832 • Updated: 2016-

Turn project into package, Enable roxygen documentation with Tools > Project Options > Build Tools

Roxygen guide at Help > Roxygen Quick Reference

File > New Project > New Directory > R Package

Share Project with Collaborators

Active shared collaborators

Select R Version

Start new R Session in current project

Close R Session in project

TH J

RStudio saves the call history, workspace, and working directory associated with a project. It reloads each when you re-open a project.

Name of current project

View() opens spreadsheet like view of data set

Sort by values

Filter rows by value or value range

Search for value

Viewer Pane displays HTML content, such as Shiny apps, RMarkdown reports, and interactive visualizations

Stop Shiny app

Publish to shinyapps.io, rpubs, RSConnect, …

Refresh

RStudio opens documentation in a dedicated Help pane

Home page of helpful links

Search within help file

Search for help file

GUI Package manager lists every installed package

Click to load package with library(). Unclick to detach package with detach()

Delete from library

Install Packages

Update Packages

Create reproducible package library for your project

RStudio opens plots in a dedicated Plots pane

Navigate recent plots

Open in window

Export plot

Delete plot

Delete all plots

Package version installed

Examine variables in executing environment

Open with debug(), browser(), or a breakpoint. RStudio will open the debugger mode when it encounters a breakpoint while executing code.

Open traceback to examine the functions that R called before the error occurred

Launch debugger mode from origin of error

Click next to line number to add/remove a breakpoint.

Select function in traceback to debug

Highlighted line shows where execution has paused

Run commands in environment where execution has paused

Step through code one line at a time

Step into and out of functions to run

Resume execution

Quit debug mode

Open Shiny, R Markdown, knitr, Sweave, LaTeX, .Rd files and more in Source Pane

Check spelling

Render output

Choose output format

Choose output location

Insert code chunk

Jump to previous chunk

Jump to next chunk

Run selected lines

Publish to server

Show file outline

Set knitr chunk options

Run this and all previous code chunks

Run this code chunk

Jump to chunk

RStudio recognizes that files named app.R , server.R , ui.R , and global.R belong to a shiny app

Run app

Choose location to view app

Publish to shinyapps.io or server

Manage publish accounts

Access markdown guide at Help > Markdown Quick Reference

Stage files:

Show file diff

Commit staged files

Push/Pull to remote

View History

current branch

  • Added
  • Deleted
  • Modified
  • Renamed
  • Untracked

Turn on at Tools > Project Options > Git/SVN

Open shell to type commands

A D M R ?

Search inside environment

Syntax highlighting based on your file's extension

Code diagnostics that appear in the margin. Hover over diagnostic symbols for details.

Tab completion to finish function names, file paths, arguments, and more.

Multi-language code snippets to quickly use common blocks of code.

Open in new window

Save Find and replace

Compile as notebook

Run selected code

Re-run previous code

Source with or without Echo

Show file outline

Jump to function in file Change file type

Navigate tabs

A File browser keyed to your working directory. Click on file or directory name to open.

Path to displayed directory

Upload file

Create folder

Delete file

Rename file

Change directory

Displays saved objects by type with short description

View function source code

View in data viewer

Load workspace

Save workspace

Import data with wizard

Delete all saved objects

Display objects as list or grid

Choose environment to display from list of parent environments

History of past commands to run/copy

Display .RPres slideshows File > New File > R Presentation

Working Directory

Maximize, minimize panes

Drag pane boundaries

TH J

Cursors of shared users

File > New Project

Press  to see command history

Multiple cursors/column selection with Alt + mouse drag.

Documents and Apps R Support

PROJECT SYSTEM

Debug Mode Version Control with Git or SVN

Package Writing

Shiny : : CHEAT SHEET

Complete the template by adding arguments to fluidPage() and a

body to the server function.

app-name

app.R
DESCRIPTION
README
www

.r

library(shiny)

ui <- fluidPage( numericInput( inputId = "n" , "Sample size", value = 25), plotOutput( outputId = "hist" ) )

server <- function(input, output) { output$hist <- renderPlot ({ hist(rnorm( input$n )) }) }

shinyApp(ui = ui, server = server)

Add inputs to the UI with *Input() functions
Add outputs with *Output() functions
Tell server how to render outputs with R in
the server function. To do this:
1. Refer to outputs with output$
2. Refer to inputs with input$
3. Wrap code in a render*() function before
saving to output
Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.

library(shiny) ui <- fluidPage( numericInput( inputId = "n" , "Sample size", value = 25), plotOutput( outputId = "hist" ) ) server <- function(input, output) { output$hist <- renderPlot ({ hist(rnorm( input$n )) }) }

shinyApp(ui = ui, server = server)

ui.R

fluidPage( numericInput( inputId = "n" , "Sample size", value = 25), plotOutput( outputId = "hist" ) )

server.R

function(input, output) { output$hist <- renderPlot ({ hist(rnorm( input$n )) }) }

ui.R contains everything
you would save to ui.
server.R ends with the
function you would save
to server.
No need to call
shinyApp().
Save each app as a directory that holds an app.R f ile (or a server.R file and a ui.R file) plus optional extra files.
The directory name is the name of the app
(optional) defines objects available to both
ui.R and server.R
(optional) used in showcase mode
(optional) data, scripts, etc.
(optional) directory of files to share with web
browsers (images, CSS, .js, etc.) Must be named " www "
Launch apps with

runApp()

^ global.R

Basics Building an App

Outputs - render*() and *Output() functions work together to add R output to the UI

Inputs

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at shiny.rstudio.com • shiny 0.12.0 • Updated: 2016-

Build or purchase your own Shiny Server

at www.rstudio.com/products/shiny-server/

A Shiny app is a web page ( UI ) connected to a

computer running a live R session ( Server )

Users can manipulate the UI, which will cause

the server to update the UI’s displays (by

running R code).

library(shiny)

ui <- fluidPage()

server <- function(input, output){}

shinyApp(ui = ui, server = server)

Begin writing a new app with this template.

Preview the app by running the code at the R

command line.

The easiest way to share your app

is to host it on shinyapps.io, a

cloud based service from RStudio

1. Create a free or professional account at

http://shinyapps.io

2. Click the Publish icon in the RStudio IDE

or run:

rsconnect::deployApp( "" )

  • ui^ - nested R functions that assemble an

HTML user interface for your app

  • server^ - a function with instructions on how

to build and rebuild the R objects displayed

in the UI

  • shinyApp^ - combines^ ui^ and^ server^ into

an app. Wrap with runApp() if calling from a

sourced script or inside a function.

APP TEMPLATE

SHARE YOUR APP

DT:: renderDataTable( expr, options,

callback, escape, env, quoted )

renderImage (expr, env, quoted,

deleteFile)

renderPlot (expr, width, height, res, …,

env, quoted, func)

renderPrint (expr, env, quoted, func,

width)

renderTable (expr,…, env, quoted, func)

renderText (expr, env, quoted, func)

renderUI (expr, env, quoted, func)

dataTableOutput (outputId, icon, …)

imageOutput (outputId, width, height,

click, dblclick, hover, hoverDelay, inline,

hoverDelayType, brush, clickId, hoverId)

plotOutput (outputId, width, height, click,

dblclick, hover, hoverDelay, inline,

hoverDelayType, brush, clickId, hoverId)

verbatimTextOutput (outputId)

tableOutput (outputId)

textOutput (outputId, container, inline)

uiOutput (outputId, inline, container, …)

htmlOutput (outputId, inline, container, …)

&

works with

collect values from the user

actionButton (inputId, label, icon,

actionLink (inputId, label, icon, …)

checkboxGroupInput (inputId, label,

choices, selected, inline)

checkboxInput (inputId, label,

value)

dateInput (inputId, label, value, min,

max, format, startview, weekstart,

language)

dateRangeInput (inputId, label,

start, end, min, max, format,

startview, weekstart, language,

separator)

fileInput (inputId, label, multiple,

accept)

numericInput (inputId, label, value,

min, max, step)

passwordInput (inputId, label,

value)

radioButtons (inputId, label,

choices, selected, inline)

selectInput (inputId, label, choices,

selected, multiple, selectize, width,

size) (also selectizeInput())

sliderInput (inputId, label, min, max,

value, step, round, format, locale,

ticks, animate, width, sep, pre,

post)

submitButton (text, icon)

(Prevents reactions across entire app)

textInput (inputId, label, value)

Access the current value of an input object with

input$. Input values are reactive.

wellPanel( dateInput("a", ""), submitButton() )

Reactivity

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at shiny.rstudio.com • shiny 0.12.0 • Updated: 2016-

Reactive values work together with reactive functions. Call a reactive value from within the arguments of one
of these functions to avoid the error Operation not allowed without an active reactive context.

example snippets

ui <- fluidPage( textInput("a","","A") )

server <- function(input,output){ rv <- reactiveValues() rv$number <- 5 }

library(shiny) ui <- fluidPage( textInput("a","","A"), textOutput("b") )

server <- function(input,output){ output$b <- renderText({ isolate({input$a}) }) }

shinyApp(ui, server)

library(shiny) ui <- fluidPage( textInput("a","","A"), textOutput("b") )

server <- function(input,output){ output$b <- renderText({ input$a }) }

shinyApp(ui, server)

library(shiny) ui <- fluidPage( textInput("a","","A"), actionButton("go","Go") )

server <- function(input,output){ observeEvent(input$go,{ print(input$a) }) }

shinyApp(ui, server)

library(shiny) ui <- fluidPage( textInput("a","","A"), actionButton("go","Go"), textOutput("b") )

server <- function(input,output){ re <- eventReactive( input$go,{input$a}) output$b <- renderText({ re() }) }

shinyApp(ui, server)

ui <- fluidPage( textInput("a","","A"), textInput("z","","Z"), textOutput("b")) server <- function(input,output){ re <- reactive({ paste(input$a,input$z)}) output$b <- renderText({ re() }) } shinyApp(ui, server)

render*() functions

(see front page)

observeEvent( eventExpr

, handlerExpr, event.env,

event.quoted, handler.env, handler.quoted, labe, suspended, priority, domain, autoDestroy, ignoreNULL )

eventReactive( eventExpr,

valueExpr, event.env,

event.quoted, value.env,

value.quoted, label,

domain, ignoreNULL )

*Input() functions

(see front page)

reactiveValues( … )

isolate( expr )

reactive( x, env, quoted,

label, domain )

Each input function
creates a reactive value
stored as input$
reactiveValues() creates a
list of reactive values
whose values you can set.
Builds an object to
display. Will rerun code in
body to rebuild the object
whenever a reactive value
in the code changes.
Save the results to
output$
Runs a code block.
Returns a non-reactive
copy of the results.
Runs code in 2nd
argument when reactive
values in 1st argument
change. See observe() for
alternative.

Creates a reactive expression that

  • caches its value to reduce computation
  • can be called by other code
  • notifies its dependencies when it ha been invalidated

Call the expression with function syntax, e.g. re()

Creates reactive
expression with code in
2nd argument that only
invalidates when reactive
values in 1st argument
change.

CREATE YOUR OWN REACTIVE VALUES

PREVENT REACTIONS

MODULARIZE REACTIONS

RENDER REACTIVE OUTPUT

TRIGGER ARBITRARY CODE

DELAY REACTIONS

UI - An app’s UI is an HTML document. Layouts

Add static HTML elements with tags , a list of functions that parallel common HTML tags, e.g. tags$a(). Unnamed arguments will be passed into the tag; named arguments will become tag attributes.

Use Shiny’s functions to assemble this HTML with R.

tags$a tags$abbr tags$address tags$area tags$article tags$aside tags$audio tags$b tags$base tags$bdi tags$bdo tags$blockquote tags$body tags$br tags$button tags$canvas tags$caption tags$cite tags$code tags$col tags$colgroup tags$command

tags$data tags$datalist tags$dd tags$del tags$details tags$dfn tags$div tags$dl tags$dt tags$em tags$embed tags$eventsource tags$fieldset tags$figcaption tags$figure tags$footer tags$form tags$h tags$h tags$h tags$h tags$h

tags$h tags$head tags$header tags$hgroup tags$hr tags$HTML tags$i tags$iframe tags$img tags$input tags$ins tags$kbd tags$keygen tags$label tags$legend tags$li tags$link tags$mark tags$map tags$menu tags$meta tags$meter

tags$nav tags$noscript tags$object tags$ol tags$optgroup tags$option tags$output tags$p tags$param tags$pre tags$progress tags$q tags$ruby tags$rp tags$rt tags$s tags$samp tags$script tags$section tags$select tags$small tags$source

tags$span tags$strong tags$style tags$sub tags$summary tags$sup tags$table tags$tbody tags$td tags$textarea tags$tfoot tags$th tags$thead tags$time tags$title tags$tr tags$track tags$u tags$ul tags$var tags$video tags$wbr

fluidPage( textInput("a","") )

<input id="a" type="text"

class="form-control" value=""/>

Returns HTML

The most common tags have wrapper functions. You
do not need to prefix their names with tags$
To include a CSS file, use includeCSS() , or
1. Place the file in the www subdirectory
2. Link to it with
To include JavaScript, use includeScript() or
1. Place the file in the www subdirectory
2. Link to it with

tags$head(tags$link(rel = "stylesheet", type = "text/css", href = " "))

tags$head(tags$script(src = ""))

To include an image
1. Place the file in the www subdirectory
2. Link to it with img(src="")
Combine multiple elements
into a "single element" that
has its own properties with
a panel function, e.g.

fluidRow()

row

column

column col

flowLayout()

object 1

object 2

object 3

object 3

splitLayout()

object

object

Organize panels and elements into a layout with a
layout function. Add elements as arguments of the
layout functions.
Layer tabPanels on top of each other,
and navigate between them, with:

side panel

main

panel

sidebarLayout()

verticalLayout()

object 1

object 2

object 3

ui <- fluidPage( fluidRow(column( width = 4 ), column( width = 2, offset = 3 )) , fluidRow(column( width = 12 )) )

ui <- fluidPage( flowLayout( # object 1,

object 2,

object 3

) )

ui <- fluidPage( sidebarLayout( sidebarPanel(), mainPanel() ) )

ui <- fluidPage( splitLayout( # object 1,

object 2

) )

ui <- fluidPage( verticalLayout( # object 1,

object 2,

object 3

) )

ui <- fluidPage( tabsetPanel( tabPanel("tab 1", "contents"), tabPanel("tab 2", "contents"), tabPanel("tab 3", "contents") ) )

ui <- fluidPage( navlistPanel( tabPanel("tab 1", "contents"), tabPanel("tab 2", "contents"), tabPanel("tab 3", "contents") ) )

ui <- navbarPage( title = "Page", tabPanel("tab 1", "contents"), tabPanel("tab 2", "contents"), tabPanel("tab 3", "contents") )

absolutePanel() conditionalPanel() fixedPanel() headerPanel() inputPanel() mainPanel()

navlistPanel() sidebarPanel() tabPanel() tabsetPanel() titlePanel() wellPanel()

IMAGES

ui <- fluidPage( h1("Header 1"), hr(), br(), p(strong("bold")), p(em("italic")), p(code("code")), a(href="", "link"), HTML("

Raw html

") )

knitr:: **kable(** data, caption = "Table with kable” **)** 
print(xtable:: **xtable(** data, caption = "Table with xtable” **)** , type = "html", html.table.attributes = "border=0")) 
stargazer:: **stargazer(** data, type = "html", title = "Table with stargazer" **)** 

Plain text End a line with two spaces to start a new paragraph. italics and bold verbatim code sub/superscript^2^~2~ strikethrough escaped: * _ \ endash: --, emdash: --- equation: $A = \pi*r^{2}$ equation block:

$$E = mc^{2}$$

block quote

Header1 {#anchor}

Header 2 {#css_id}

Header 3 {.css_class}

Header 4

Header 5
Header 6

\textbf{Tex ignored in HTML} HTML ignored in pdfs

http://www.rstudio.com link Jump to Header 1 image:

Caption

  • unordered list
  • sub-item 1
  • sub-item 2
  • sub-sub-item 1
  • item 2

Continued (indent 4 spaces)

  1. ordered list
  2. item 2 i) sub-item 1 A. sub-sub-item 1

(@) A list whose numbering

continues after

(@) an interruption

Term 1

: Definition 1

RightLeftDefaultCenter
12121212
123123123123
1111
  • slide bullet 1
  • slide bullet 2

(>- to have bullets appear on click)

horizontal rule/slide break:


A footnote [1]

Write with syntax on the left to create effect on right (after render)

Pandoc’s Markdown Set render options with YAML

When you render, R Markdown

  1. runs the R code, embeds results and text into .md file with knitr
  2. then converts the .md file into the finished format with pandoc

Create a Reusable Template

  1. Create a new package with a inst/rmarkdown/templates directory
  2. In the directory, Place a folder that contains: template.yaml (see below) skeleton.Rmd (contents of the template) any supporting files
  3. Install the package
  4. Access template in wizard at File ▶ New File ▶ R Markdown template.yaml

Set a document’s default output format in the YAML header:

output: html_document

Body

output value creates

html_document html pdf_document pdf (requires Tex ) word_document Microsoft Word (.docx) odt_document OpenDocument Text rtf_document Rich Text Format md_document Markdown github_document Github compatible markdown ioslides_presentation ioslides HTML slides slidy_presentation slidy HTML slides beamer_presentation Beamer pdf slides (requires Tex)

Customize output with sub-options (listed to the right):

output: html_document: code_folding: hide toc_float: TRUE


Body

Indent 4 spaces

Indent 2 spaces

html tabsets Use tablet css class to place sub-headers into tabs

Tabset {.tabset .tabset-fade .tabset-pills}

Tab 1

text 1

Tab 2

text 2

End tabset

Tabset

text 1 End tabset

Tab 1 Tab 2

name: My Template —

sub-option description

citation_package The LaTeX package to process citations, natbib, biblatex or none X X X code_folding Let readers to toggle the display of R code, "none", "hide", or "show" X colortheme Beamer color theme to use X

css CSS file to use to style document X X X dev Graphics device to use for figure output (e.g. "png") X X X X X X X duration Add a countdown timer (in minutes) to footer of slides X fig_caption Should figures be rendered with captions? X X X X X X X fig_height, fig_width Default figure height and width (in inches) for document X X X X X X X X X X highlight Syntax highlighting: "tango", "pygments", "kate","zenburn", "textmate" X X X X X

includes File of content to place in document (in_header, before_body, after_body) X X X X X X X X incremental Should bullets appear one at a time (on presenter mouse clicks)? X X X keep_md Save a copy of .md file that contains knitr output X X X X X X keep_tex Save a copy of .tex file that contains knitr output X X latex_engine Engine to render latex, "pdflatex", "xelatex", or "lualatex" X X

lib_dir Directory of dependency files to use (Bootstrap, MathJax, etc.) X X X mathjax Set to local or a URL to use a local/URL version of MathJax to render equations X X X md_extensions Markdown extensions to add to default definition or R Markdown X X X X X X X X X X number_sections Add section numbering to headers X X pandoc_args Additional arguments to pass to Pandoc X X X X X X X X X X preserve_yaml Preserve YAML front matter in final document? X

reference_docx docx file whose styles should be copied when producing docx output X self_contained Embed dependencies into the doc X X X slide_level The lowest heading level that defines individual slides X smaller Use the smaller font size in the presentation? X smart Convert straight quotes to curly, dashes to em-dashes, … to ellipses, etc. X X X

template Pandoc template to use when rendering file quarterly_report.html). X X X X X theme Bootswatch or Beamer theme to use for page X X toc Add a table of contents at start of document X X X X X X X toc_depth The lowest level of headings to add to table of contents X X X X X X toc_float Float the table of contents to the left of the main content X

htmlpdfwordodtrtfmdgituhbioslidesslidybeamer

Table Suggestions Citations and Bibliographies

Several functions format R data into tables

Learn more in the stargazer, xtable , and knitr packages.

Create citations with .bib, .bibtex, .copac, .enl, .json, .medline, .mods, .ris, .wos, and .xml files

  1. Set bibliography file and CSL 1. Style file (optional) in the YAML header
  2. Use citation keys in text
  3. Render. Bibliography will be added to end of document

Smith cited [@smith04]. Smith cited without author [-@smith04]. @smith04 cited in line.


bibliography: refs.bib csl: style.csl


data <- faithful[1:4, ]

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at rmarkdown.rstudio.com • rmarkdown 1.6 • Updated: 2016-

rmarkdown

read_*( file, col_names = TRUE, col_types = NULL, locale = default_locale(), na = c("", "NA"),

quoted_na = TRUE, comment = "", trim_ws = TRUE, skip = 0, n_max = Inf, guess_max = min(1000,

n_max), progress = interactive() )

Try one of the following packages to import

other types of files

- haven - SPSS, Stata, and SAS files

  • readxl - excel files (.xls and .xlsx)
  • DBI - databases
  • jsonlite - json
  • xml2 - XML
  • httr - Web APIs
  • rvest - HTML (Web Scraping)

Save Data

Data Import : : CHEAT SHEET

Read Tabular Data - These functions share the common arguments: Data types

USEFUL ARGUMENTS

OTHER TYPES OF DATA

Comma delimited file

write_csv( x, path, na = "NA", append = FALSE,

col_names = !append )

File with arbitrary delimiter

write_delim( x, path, delim = " ", na = "NA",

append = FALSE, col_names = !append )

CSV for excel

write_excel_csv( x, path, na = "NA", append =

FALSE, col_names = !append )

String to file

write_file( x, path, append = FALSE )

String vector to file, one element per line

write_lines( x,path, na = "NA", append = FALSE )

Object to RDS file

write_rds( x, path, compress = c("none", "gz",

"bz2", "xz"), ... )

Tab delimited files

write_tsv( x, path, na = "NA", append = FALSE,

col_names = !append )

Save x , an R object, to path , a file path, as:

Skip lines

read_csv(f, skip = 1 )

Read in a subset

read_csv(f, n_max = 1 )

Missing Values

read_csv(f, na = c("1", ".") )

Comma Delimited Files

read_csv( "file.csv" )

To make file.csv run:

write_file(x = "a,b,c\n1,2,3\n4,5,NA", path = "file.csv")

Semi-colon Delimited Files

read_csv2( "file2.csv" )

write_file(x = "a;b;c\n1;2;3\n4;5;NA", path = "file2.csv")

Files with Any Delimiter

read_delim( "file.txt", delim = "|" )

write_file(x = "a|b|c\n1|2|3\n4|5|NA", path = "file.txt")

Fixed Width Files

read_fwf( "file.fwf", col_positions = c(1, 3, 5) )

write_file(x = "a b c\n1 2 3\n4 5 NA", path = "file.fwf")

Tab Delimited Files

read_tsv( "file.tsv" ) Also read_table().

write_file(x = "a\tb\tc\n1\t2\t3\n4\t5\tNA", path = "file.tsv")

a,b,c
4,5,NA
a;b;c
4;5;NA
a|b|c
4|5|NA
a b c
4 5 NA
A B C
A B C
4 5 NA

x y z

A B C 1 2 3 4 5 NA

A B C
NA 2 3
4 5 NA
4 5 NA
A B C
4 5 NA
A B C
4 5 NA
A B C
4 5 NA
A B C
4 5 NA
a,b,c
4,5,NA

Example file

write_file("a,b,c\n1,2,3\n4,5,NA","file.csv")

f <- "file.csv"

No header

read_csv(f, col_names = FALSE )

Provide header

read_csv(f, col_names = c("x", "y", "z") )

Read a file into a single string

read_file( file, locale = default_locale() )

Read each line into its own string

read_lines( file, skip = 0, n_max = -1L, na = character(),

locale = default_locale(), progress = interactive() )

Read a file into a raw vector

read_file_raw( file )

Read each line into a raw vector

read_lines_raw( file, skip = 0, n_max = -1L,

progress = interactive() )

Read Non-Tabular Data

Read Apache style log files

read_log( file, col_names = FALSE, col_types = NULL, skip = 0, n_max = -1, progress = interactive() )

Parsed with column specification:

cols(

age = col_integer(),

sex = col_character(),

earn = col_double()

)

1. Use problems() to diagnose problems.
x <- read_csv("file.csv"); problems(x)
2. Use a col_ function to guide parsing.

- col_guess() - the default **- col_character()

  • col_double()** , **col_euro_double()
  • col_datetime(** format = "" ) Also
col_date( format = "" ) , col_time( format = "" )

- col_factor( levels, ordered = FALSE **)

  • col_integer()
  • col_logical()
  • col_number()** , **col_numeric()
  • col_skip()**
x <- read_csv("file.csv", col_types = cols(
A = col_double(),
B = col_logical(),
C = col_factor()))
3. Else, read in as character vectors then parse
with a parse_ function.

**- parse_guess()

  • parse_character()
  • parse_datetime()** Also parse_date() and
parse_time()

**- parse_double()

  • parse_factor()
  • parse_integer()
  • parse_logical()
  • parse_number()**
x$A <- parse_number(x$A)

readr functions guess

the types of each column and

convert types when appropriate (but will NOT

convert strings to factors automatically).

A message shows the type of each column in the

result.

earn is a double (numeric)
sex is a
character
age is an
integer

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at tidyverse.org • readr 1.1.0 • tibble 1.2.12 • tidyr 0.6.0 • Updated: 2017-

R’s tidyverse is built around tidy data stored

in tibbles , which are enhanced data frames.

The front side of this sheet shows

how to read text files into R with

readr.

The reverse side shows how to

create tibbles with tibble and to

layout tidy data with tidyr.

w

Summarise Cases

group_by( .data, ..., add =

FALSE )

Returns copy of table

grouped by …

g_iris <- group_by(iris, Species)

ungroup( x, … )

Returns ungrouped copy

of table.

ungroup(g_iris)

wwwwww

w

Use group_by() to create a "grouped" copy of a table.

dplyr functions will manipulate each "group" separately and

then combine the results.

mtcars %>%

group_by(cyl) %>%

summarise(avg = mean(mpg))

These apply summary functions to columns to create a new

table of summary statistics. Summary functions take vectors as

input and return one value (see back).

VARIATIONS

summarise_all() - Apply funs to every column.

summarise_at() - Apply funs to specific columns.

summarise_if() - Apply funs to all cols of one type.

www

www

summarise (.data, …)

Compute table of summaries.

summarise(mtcars, avg = mean(mpg))

count (x, ..., wt = NULL, sort = FALSE)

Count number of rows in each group defined

by the variables in … Also tally ().

count(iris, Species)

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more with browseVignettes(package = c("dplyr", "tibble")) • dplyr 0.7.0 • tibble 1.2.0 • Updated: 2017-

Each observation , or

case , is in its own row

Each variable is in

its own column

dplyr functions work with pipes and expect tidy data. In tidy data:

pipes

x %>% f(y)

becomes f(x, y)

filter( .data, … ) Extract rows that meet logical
criteria. filter(iris, Sepal.Length > 7)
distinct( .data, ..., .keep_all = FALSE ) Remove
rows with duplicate values.
distinct(iris, Species)
sample_frac( tbl, size = 1, replace = FALSE,
weight = NULL, .env = parent.frame() ) Randomly
select fraction of rows.
sample_frac(iris, 0.5, replace = TRUE)
sample_n( tbl, size, replace = FALSE, weight =
NULL, .env = parent.frame() ) Randomly select
size rows. sample_n(iris, 10, replace = TRUE)
slice( .data, … ) Select rows by position.
slice(iris, 10:15)
top_n( x, n, wt ) Select and order top n entries (by
group if grouped data). top_n(iris, 5, Sepal.Width)

Row functions return a subset of rows as a new table.

See ?base::logic and ?Comparison for help.

> >= !is.na()! &

< <= is.na() %in% | xor()

arrange( .data, … ) Order rows by values of a

column or columns (low to high), use with

desc() to order from high to low.

arrange(mtcars, mpg)

arrange(mtcars, desc(mpg))

add_row(. data, ..., .before = NULL, .after = NULL )

Add one or more rows to a table.

add_row(faithful, eruptions = 1, waiting = 1)

Group Cases

Manipulate Cases

EXTRACT VARIABLES

ADD CASES

ARRANGE CASES

Logical and boolean operators to use with filter()

Column functions return a set of columns as a new vector or table.

contains( match )

ends_with( match )

matches( match )

: , e.g. mpg:cyl

- , e.g, -Species

num_range( prefix, range )

one_of( … )

starts_with( match )

pull( .data, var = -1 ) Extract column values as

a vector. Choose by name or index.

pull(iris, Sepal.Length)

Manipulate Variables

Use these helpers with select (),

e.g. select(iris, starts_with("Sepal"))

These apply vectorized functions to columns. Vectorized funs take

vectors as input and return vectors of the same length as output

(see back).

mutate( .data, … )

Compute new column(s).

mutate(mtcars, gpm = 1/mpg)

transmute( .data, … )

Compute new column(s), drop others.

transmute(mtcars, gpm = 1/mpg)

mutate_all( .tbl, .funs, … ) Apply funs to every

column. Use with funs(). Also mutate_if().

mutate_all(faithful, funs(log(.), log2(.)))

mutate_if(iris, is.numeric, funs(log(.)))

mutate_at( .tbl, .cols, .funs, … ) Apply funs to

specific columns. Use with funs() , vars() and

the helper functions for select().

mutate_at(iris, vars( -Species), funs(log(.)))

add_column( .data, ..., .before = NULL, .after =

NULL ) Add new column(s). Also add_count() ,

add_tally(). add_column(mtcars, new = 1:32)

rename( .data, … ) Rename columns.

rename(iris, Length = Sepal.Length)

MAKE NEW VARIABLES

EXTRACT CASES

wwwwww

wwwwww

wwwwww

wwwwww

wwwwww

wwwwww

wwww

wwwww

wwwwww

www

wwww

w

wwwwww

summary function

vectorized function

Data Transformation with dplyr : : CHEAT SHEET

A B C A B C

select( .data, … )

Extract columns as a table. Also select_if().

select(iris, Sepal.Length, Species)

wwww

dplyr

OFFSETS

dplyr:: lag() - Offset elements by 1

dplyr:: lead() - Offset elements by -

CUMULATIVE AGGREGATES

dplyr:: cumall() - Cumulative all()

dplyr:: cumany() - Cumulative any()

cummax() - Cumulative max()

dplyr:: cummean() - Cumulative mean()

cummin() - Cumulative min()

cumprod() - Cumulative prod()

cumsum() - Cumulative sum()

RANKINGS

dplyr:: cume_dist() - Proportion of all values <=

dplyr:: dense_rank() - rank with ties = min, no

gaps

dplyr:: min_rank() - rank with ties = min

dplyr:: ntile() - bins into n bins

dplyr:: percent_rank() - min_rank scaled to [0,1]

dplyr:: row_number() - rank with ties = "first"

MATH

+, - , *, /, ^, %/%, %% - arithmetic ops

log(), log2(), log10() - logs

<, <=, >, >=, !=, == - logical comparisons

dplyr:: between() - x >= left & x <= right

dplyr:: near() - safe == for floating point

numbers

MISC

dplyr:: case_when() - multi-case if_else()

dplyr:: coalesce() - first non-NA values by

element across a set of vectors

dplyr:: if_else() - element-wise if() + else()

dplyr:: na_if() - replace specific values with NA

pmax() - element-wise max()

pmin() - element-wise min()

dplyr:: recode() - Vectorized switch()

dplyr:: recode_factor() - Vectorized switch()

for factors

mutate() and transmute() apply vectorized

functions to columns to create new columns.

Vectorized functions take vectors as input and

return vectors of the same length as output.

Vector Functions

TO USE WITH MUTATE ()

vectorized function

Summary Functions

TO USE WITH SUMMARISE ()

summarise() applies summary functions to

columns to create a new table. Summary

functions take vectors as input and return single

values as output.

COUNTS

dplyr:: n() - number of values/rows

dplyr:: n_distinct() - # of uniques

sum(!is.na()) - # of non-NA’s

LOCATION

mean() - mean, also mean(!is.na())

median() - median

LOGICALS

mean() - Proportion of TRUE’s

sum() - # of TRUE’s

POSITION/ORDER

dplyr:: first() - first value

dplyr:: last() - last value

dplyr:: nth() - value in nth location of vector

RANK

quantile() - nth quantile

min() - minimum value

max() - maximum value

SPREAD

IQR() - Inter-Quartile Range

mad() - median absolute deviation

sd() - standard deviation

var() - variance

Row Names

Tidy data does not use rownames, which store a
variable outside of the columns. To work with the
rownames, first move them into a column.

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more with browseVignettes(package = c("dplyr", "tibble")) • dplyr 0.7.0 • tibble 1.2.0 • Updated: 2017-

rownames_to_column()

Move row names into col.

a <- rownames_to_column(iris, var

= "C")

column_to_rownames()

Move col in row names.

column_to_rownames(a, var = "C")

summary function

C A B

Also has_rownames() , remove_rownames()

Combine Tables

COMBINE VARIABLES COMBINE CASES

Use bind_cols() to paste tables beside each

other as they are.

bind_cols(…) Returns tables placed side by

side as a single table.

BE SURE THAT ROWS ALIGN.

Use a " Mutating Join " to join one table to

columns from another, matching values with

the rows that they correspond to. Each join

retains a different combination of values from

the tables.

left_join( x, y, by = NULL,

copy=FALSE, suffix=c(“.x”,“.y”),… )

Join matching values from y to x.

right_join( x, y, by = NULL, copy =

FALSE, suffix=c(“.x”,“.y”),… )

Join matching values from x to y.

inner_join( x, y, by = NULL, copy =

FALSE, suffix=c(“.x”,“.y”),… )

Join data. Retain only rows with

matches.

full_join( x, y, by = NULL,

copy=FALSE, suffix=c(“.x”,“.y”),… )

Join data. Retain all values, all rows.

Use by = c("col1", "col2", …) to

specify one or more common

columns to match on.

left_join(x, y, by = "A")

Use a named vector, by = c("col1" =

"col2") , to match on columns that

have different names in each table.

left_join(x, y, by = c("C" = "D"))

Use suffix to specify the suffix to

give to unmatched columns that

have the same name in both tables.

left_join(x, y, by = c("C" = "D"), suffix =

c("1", "2"))

Use bind_rows() to paste tables below each

other as they are.

bind_rows( …, .id = NULL )

Returns tables one on top of the other

as a single table. Set .id to a column

name to add a column of the original

table names (as pictured)

intersect(x, y, …)

Rows that appear in both x and y.

setdiff(x, y, …)

Rows that appear in x but not y.

union(x, y, …)

Rows that appear in x or y.

(Duplicates removed). union_all()

retains duplicates.

Use a " Filtering Join " to filter one table against

the rows of another.

semi_join( x, y, by = NULL, … )

Return rows of x that have a match in y.

USEFUL TO SEE WHAT WILL BE JOINED.

anti_join( x, y, by = NULL, … )

Return rows of x that do not have a

match in y. USEFUL TO SEE WHAT WILL

NOT BE JOINED.

Use setequal() to test whether two data sets

contain the exact same rows (in any order).

EXTRACT ROWS

A B 1 a t 2 b u 3 c v

1 a t 2 b u 3 c v

A B 1 a t 2 b u 3 c v

A B C 1 a t 2 b u 3 c v

x y

A B C a t 1 b u 2 c v 3

A B D a t 3 b u 2 d w 1

  • =

A B C a t 1 b u 2 c v 3

A B D a t 3 b u 2 d w 1

A B C D a t 1 3 b u 2 2 c v 3 NA

A B C D a t 1 3 b u 2 2 d w N A 1

A B C D a t 1 3 b u 2 2

A B C D a t 1 3 b u 2 2 c v 3 NA d w NA 1

A B.x C B.y D a t 1 t 3 b u 2 u 2 c v 3 NA^ NA

A.x B.x C A.y B.y a t 1 d w b u 2 b u c v 3 a t

A1 B1 C A2 B a t 1 d w b u 2 b u c v 3 a t

x

y

A B C a t 1 b u 2 c v 3

A B C C v 3

  • d w^4

DF A B C x a t 1 x b u 2 x c v 3 z c v 3 z d w 4

A B C c v 3

A B C a t 1 b u 2 c v 3 d w 4

A B C a t 1 b u 2

x y

A B C a t 1 b u 2 c v 3

A B D a t 3 b u 2 d w 1

  • =

A B C c v 3

A B C a t 1 b u 2

dplyr

Scales Coordinate Systems

A stat builds new variables to plot (e.g., count, prop).

Stats An alternative way to build a layer

  • =

data geom x = x · y = ..count..

coordinate system

plot

fl cty cyl x ..count..

stat

Visualize a stat by changing the default stat of a geom function, geom_bar(stat="count") or by using a stat function, stat_count(geom="bar") , which calls a default geom to make a layer (equivalent to a geom function). Use ..name.. syntax to map stat variables to aesthetics.

i + stat_density2d (aes(fill = ..level..), geom = "polygon" )

stat function geommappings

variable created by stat

geom to use

c + stat_bin( binwidth = 1, origin = 10 ) x, y | ..count.., ..ncount.., ..density.., ..ndensity.. c + stat_count( width = 1 ) x, y, | ..count.., ..prop.. c + stat_density( adjust = 1, kernel = “gaussian" ) x, y, | ..count.., ..density.., ..scaled..

e + stat_bin_2d( bins = 30, drop = T ) x, y, fill | ..count.., ..density.. e + stat_bin_hex( bins=30 ) x, y, fill | ..count.., ..density.. e + stat_density_2d( contour = TRUE, n = 100 ) x, y, color, size | ..level.. e + stat_ellipse(l evel = 0.95, segments = 51, type = "t" )

l + stat_contour( aes(z = z) ) x, y, z, order | ..level.. l + stat_summary_hex( aes(z = z), bins = 30, fun = max ) x, y, z, fill | ..value.. l + stat_summary_2d( aes(z = z), bins = 30, fun = mean ) x, y, z, fill | ..value..

f + stat_boxplot( coef = 1.5 ) x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax..

f + stat_ydensity( kernel = "gaussian", scale = “area" ) x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width..

e + stat_ecdf( n = 40 ) x, y | ..x.., ..y.. e + stat_quantile( quantiles = c(0.1, 0.9), formula = y ~ log(x), method = "rq" ) x, y | ..quantile.. e + stat_smooth( method = "lm", formula = y ~ x, se=T, level=0.95 ) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax..

ggplot() + stat_function( aes(x = -3:3), n = 99, fun = dnorm, args = list(sd=0.5) ) x | ..x.., ..y..

e + stat_identity( na.rm = TRUE ) ggplot() + stat_qq( aes(sample=1:100), dist = qt, dparam=list(df=5) ) sample, x, y | ..sample.., ..theoretical..

e + stat_sum() x, y, size | ..n.., ..prop.. e + stat_summary(f un.data = "mean_cl_boot" ) h + stat_summary_bin( fun.y = "mean", geom = "bar" ) e + stat_unique()

Scales map data values to the visual values of an aesthetic. To change a mapping, add a new scale.

(n <- d + geom_bar(aes(fill = fl)))

n + scale_fill_manual( values = c("skyblue", "royalblue", "blue", “navy"), limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", “r"), name = "fuel", labels = c("D", "E", "P", "R") )

scale_

aesthetic to adjust

prepackaged scale to use

scale-specific arguments

title to use in legend/axis

labels to use in legend/axis

breaks to use in legend/axis

range of values to include in mapping

GENERAL PURPOSE SCALES

Use with most aesthetics scale__continuous()* - map cont’ values to visual ones scale__discrete()* - map discrete values to visual ones scale__identity()* - use data values as visual ones scale__manual(* values = c() ) - map discrete values to manually chosen visual ones scale__date(* date_labels = "%m/%d"), date_breaks = " weeks" ) - treat data values as dates. scale__datetime()* - treat data x values as date times. Use same arguments as scale_x_date(). See ?strptime for label formats.

X & Y LOCATION SCALES

Use with x or y aesthetics (x shown here) scale_x_log10() - Plot x on log10 scale scale_x_reverse() - Reverse direction of x axis scale_x_sqrt() - Plot x on square root scale

COLOR AND FILL SCALES (DISCRETE)

n <- d + geom_bar( aes(fill = fl) )

n + scale_fill_brewer( palette = "Blues" ) For palette choices: RColorBrewer::display.brewer.all() n + scale_fill_grey( start = 0.2, end = 0.8, na.value = "red" )

COLOR AND FILL SCALES (CONTINUOUS)

o <- c + geom_dotplot(aes(fill = ..x..))

o + scale_fill_distiller( palette = "Blues" )

o + scale_fill_gradient( low="red", high="yellow" )

o + scale_fill_gradient2( low="red", high=“blue", mid = "white", midpoint = 25)

o + scale_fill_gradientn( colo u rs=topo.colors(6) ) Also: rainbow(), heat.colors(), terrain.colors(), cm.colors(), RColorBrewer::brewer.pal()

SHAPE AND SIZE SCALES

p <- e + geom_point(aes(shape = fl, size = cyl)) p + scale_shape() + scale_size() p + scale_shape_manual( values = c(3:7) )

p + scale_radius( range = c(1,6) ) p + scale_size_area( max_size = 6 )

r <- d + geom_bar() r + coord_cartesian( xlim = c(0, 5) ) xlim, ylim The default cartesian coordinate system r + coord_fixed( ratio = 1/2 ) ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units r + coord_flip() xlim, ylim Flipped Cartesian coordinates r + coord_polar( theta = "x", direction=1 ) theta, start, direction Polar coordinates r + coord_trans( ytrans = “sqrt" ) xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set xtrans and ytrans to the name of a window function.

π + coord_quickmap() π + coord_map( projection = "ortho", orientation=c(41, -74, 0) ) projection, orienztation, xlim, ylim Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.)

Position Adjustments

Position adjustments determine how to arrange geoms that would otherwise occupy the same space.

s <- ggplot(mpg, aes(fl, fill = drv)) s + geom_bar(position = "dodge") Arrange elements side by side s + geom_bar(position = "fill") Stack elements on top of one another, normalize height e + geom_point(position = "jitter") Add random noise to X and Y position of each element to avoid overplotting e + geom_label(position = "nudge") Nudge labels away from points

s + geom_bar(position = "stack") Stack elements on top of one another

Each position adjustment can be recast as a function with manual width and height arguments s + geom_bar(position = position_dodge(width = 1))

A B

Themes

r + theme_bw() White background with grid lines r + theme_gray() Grey background (default theme) r + theme_dark() dark for contrast

r + theme_classic()

r + theme_light() r + theme_linedraw() r + theme_minimal() Minimal themes r + theme_void() Empty theme

Faceting

Facets divide a plot into subplots based on the values of one or more discrete variables.

t <- ggplot(mpg, aes(cty, hwy)) + geom_point()

t + facet_grid(. ~ fl) facet into columns based on fl t + facet_grid(year ~ .) facet into rows based on year t + facet_grid(year ~ fl) facet into both rows and columns t + facet_wrap(~ fl) wrap facets into a rectangular layout

Set scales to let axis limits vary across facets

t + facet_grid(drv ~ fl, scales = "free") x and y axis limits adjust to individual facets "free_x" - x axis limits adjust "free_y" - y axis limits adjust

Set labeller to adjust facet labels

t + facet_grid(. ~ fl, labeller = label_both)

t + facet_grid(fl ~ ., labeller = label_bquote( alpha ^ .(fl )))

t + facet_grid(. ~ fl, labeller = label_parsed)

fl: c fl: d fl: e fl: p fl: r

c d e p r

↵c^ ↵d^ ↵e^ ↵p^ ↵r

Labels

t + labs( x = "New x axis label", y = "New y axis label", title ="Add a title above the plot", subtitle = "Add a subtitle below title", caption = "Add a caption below plot", = "New legend title") t + annotate( geom = "text", x = 8, y = 9, label = "A")

Use scale functions to update legend labels

geom to place manual values for geom’s aesthetics

Legends

n + theme( legend.position = "bottom" ) Place legend at "bottom", "top", "left", or "right" n + guides( fill = "none" ) Set legend type for each aesthetic: colorbar, legend, or none (no legend) n + scale_fill_discrete( name = "Title", labels = c("A", "B", "C", "D", "E") ) Set legend title and labels with a scale function.

Zooming

Without clipping (preferred) t + coord_cartesian( xlim = c(0, 100), ylim = c(10, 20) ) With clipping (removes unseen data points)

t + xlim( 0, 100 ) + ylim( 10, 20 ) t + scale_x_continuous( limits = c(0, 100) ) + scale_y_continuous( limits = c(0, 100) )

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at http://ggplot2.tidyverse.org • ggplot2 2.1.0 • Updated: 2016-

60

long

lat

JOIN (TO) LISTS

append (x, values, after =
length(x)) Add to end of list.
append(x, list(d = 1))
prepend (x, values, before =
1) Add to start of list.
prepend(x, list(d = 1))
splice (…) Combine objects
into a list, storing S3 objects
as sub-lists. splice(x, y, "foo")

WORK WITH LISTS

array_tree (array, margin =
NULL) Turn array into list.
Also array_branch.
array_tree(x, margin = 3)
cross2 (.x, .y, .filter = NULL)
All combinations of .x
and .y. Also cross , cross3 ,
cross_df. cross2(1:3, 4:6)
set_names (x, nm = x) Set
the names of a vector/list
directly or with a function.
set_names(x, c("p", "q", "r"))
set_names(x, tolower)

p q r

a b c

a b c d

a b c d a b c d

a b c d

a b c d

a b c d

TRANSFORM LISTS

modify (.x, .f, ...) Apply
function to each element. Also
map , map_chr , map_dbl ,
map_dfc , map_dfr , map_int ,
map_lgl. modify(x, ~.+ 2)
modify_at (.x, .at, .f, ...) Apply
function to elements by name
or index. Also map_at.
modify_at(x, "b", ~.+ 2)
modify_if (.x, .p, .f, ...) Apply
function to elements that
pass a test. Also map_if.
modify_if(x, is.numeric,~.+2)
modify_depth (.x,.depth,.f,...)
Apply function to each
element at a given level of a
list. modify_depth(x, 1, ~.+ 2)
Map functions apply a function iteratively to each element of a list
or vector.

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at purrr.tidyverse.org • purrr 0.2.3 • Updated: 2017-

Reduce Lists

Work with Lists

Apply functions with purrr : : CHEAT SHEET

Modify function behavior

a b c d

b

a b c

a c

a b

a b c d

FILTER LISTS

pluck (.x, ..., .default=NULL)
Select an element by name
or index, pluck(x,"b") ,or its
attribute with attr_getter.
pluck(x,"b",attr_getter("n"))
keep (.x, .p, …) Select
elements that pass a
logical test. keep(x, is.na)
discard (.x, .p, …) Select
elements that do not pass a
logical test. discard(x, is.na)
compact (.x, .p = identity)
Drop empty elements.
compact(x)
head_while (.x, .p, …)
Return head elements
until one does not pass.
Also tail_while.
head_while(x, is.character)

a^ NULL b c NULL

b

RESHAPE LISTS

flatten (.x) Remove a level
of indexes from a list. Also
flatten_chr , flatten_dbl ,
flatten_dfc , flatten_dfr ,
flatten_int , flatten_lgl.
flatten(x)
transpose (.l, .names =
NULL) Transposes the index
order in a multi-level list.
transpose(x)

a b c

x y a b c

x y

a b c

a b c

x y z (^2)

a b c

FALSE

a b c

TRUE

a b c

TRUE

SUMMARISE LISTS

a b c

c

a b c

every (.x, .p, …) Do all
elements pass a test?
every(x, is.character)
some (.x, .p, …) Do some
elements pass a test?
some(x, is.character)
has_element (.x, .y) Does a
list contain an element?
has_element(x, "foo")
detect (.x, .f, ..., .right=FALSE,
.p) Find first element to pass.
detect(x, is.character)
detect_index (.x, .f, ..., .right
= FALSE, .p) Find index of
first element to pass.
detect_index(x, is.character)
vec_depth (x) Return depth
(number of levels of
indexes). vec_depth(x)

Apply Functions

map (.x, .f, …) Apply a
function to each
element of a list or
vector. map(x, is.logical)
map2 (.x, ,y, .f, …) Apply
a function to pairs of
elements from two lists,
vectors. map2(x, y, sum)
pmap (.l, .f, …) Apply a
function to groups of
elements from list of lists,
vectors. pmap(list(x, y, z),
sum, na.rm = TRUE)
invoke_map (.f, .x =
list(NULL), …, .env=NULL)
Run each function in a list.
Also invoke. l <- list(var,
sd); invoke_map(l, x = 1:9)
lmap (.x, .f, ...) Apply function to each list-element of a list or vector.
imap (.x, .f, ...) Apply .f to each element of a list or vector and its index.
reduce (.x, .f, ..., .init)
Apply function recursively
to each element of a list or
vector. Also reduce_right ,
reduce2 , reduce2_right.
reduce(x, sum)
accumulate (.x, .f, ..., .init)
Reduce, but also return
intermediate results. Also
accumulate_right.
accumulate(x, sum)
compose () Compose
multiple functions.
lift () Change the type
of input a function
takes. Also lift_dl ,
lift_dv , lift_ld , lift_lv ,
lift_vd , lift_vl.
rerun () Rerun
expression n times.
negate () Negate a
predicate function (a
pipe friendly !)
partial () Create a
version of a function
that has some args
preset to values.
safely () Modify func
to return list of
results and errors.
quietly () Modify
function to return
list of results,
output, messages,
warnings.
possibly () Modify
function to return
default value
whenever an error
occurs (instead of
error).

a b

func( , )
func( , )

c

func( , )

d

func + a^ b^ c^ d
func( , )
func( , )

c

func( , )

d

func + a^ b^ c^ d
fun ( ,… )
fun ( ,… )
fun ( ,… )
map( , fun, …)
fun ( , ,… )
fun ( , ,… )
fun ( , ,… )
map2( , ,fun,…)
fun ( , , ,… )
fun ( , , ,… )
fun ( , , ,… )
pmap ( ,fun, …)

fun

fun

fun

invoke_map ( , ,… )

fun

fun fun

~ .x becomes function(x) x ,
e.g. map(l, ~ 2 +.x) becomes
map(l, function(x) 2 + x )
"name" becomes
function(x) x[["name"]] ,
e.g. map(l, "a") extracts a
from each element of l
map(), map2(), pmap() ,
imap and invoke_map
each return a list. Use a
suffixed version to
return the results as a
specific type of flat
vector, e.g. map2_chr ,
pmap_lgl , etc.
Use walk , walk2 , and
pwalk to trigger side
effects. Each return its
input invisibly.

OUTPUT

SHORTCUTS - within a purrr function:

~ .x .y becomes
function(.x, .y) .x .y , e.g.
map2(l, p, ~ .x +.y ) becomes
map2(l, p, function(l, p) l + p )
~ ..1 ..2 etc becomes
function(..1, ..2, etc ) ..1 ..2 etc,
e.g. pmap(list(a, b, c), ~ ..3 + ..1 - ..2)
becomes pmap(list(a, b, c),
function(a, b, c) c + a - b)
function returns
map list
map_chr character vector
map_dbl double (numeric) vector
map_dfc data frame (column bind)
map_dfr data frame (row bind)
map_int integer vector
map_lgl logical vector
walk triggers side effects, returns
the input invisibly

a b c

b

Join and Split

str_c (..., sep = "", collapse = NULL) Join

multiple strings into a single string.

str_c(letters, LETTERS)

str_c (..., sep = "", collapse = NULL ) Collapse

a vector of strings into a single string.

str_c(letters, collapse = "")

str_dup (string, times) Repeat strings times

times. str_dup(fruit, times = 2)

str_split_fixed (string, pattern , n) Split a

vector of strings into a matrix of substrings

(splitting at occurrences of a pattern match).

Also str_split to return a list of substrings.

str_split_fixed(fruit, " ", n=2)

str_glue (…, .sep = "", .envir = parent.frame())

Create a string from strings and {expressions}

to evaluate. str_glue("Pi is {pi}")

str_glue_data (.x, ..., .sep = "", .envir =

parent.frame(), .na = "NA") Use a data frame,

list, or environment to create a string from

strings and {expressions} to evaluate.

str_glue_data(mtcars, "{rownames(mtcars)}

has {hp} hp")

a string {xx} {yy}

A STRING

A STRING

a string

Mutate Strings

str_sub () <- value. Replace substrings by

identifying the substrings with str_sub() and

assigning into the results.

str_sub(fruit, 1, 3) <- "str"

str_replace (string, pattern , replacement)

Replace the first matched pattern in each

string. str_replace(fruit, "a", "-")

str_replace_all (string, pattern ,

replacement) Replace all matched patterns

in each string. str_replace_all(fruit, "a", "-")

str_to_lower (string, locale = "en")^1 Convert

strings to lower case.

str_to_lower(sentences)

str_to_upper (string, locale = "en")^1 Convert

strings to upper case.

str_to_upper(sentences)

str_to_title (string, locale = "en")^1 Convert

strings to title case. str_to_title(sentences)

a string

A String

str_conv (string, encoding) Override the

encoding of a string. str_conv( fruit,"ISO-8859-1")

str_view (string, pattern , match = NA) View

HTML rendering of first regex match in each

string. str_view(fruit, "[aeiou]")

str_view_all (string, pattern , match = NA) View

HTML rendering of all regex matches.

str_view_all(fruit, "[aeiou]")

str_wrap (string, width = 80, indent = 0, exdent

= 0) Wrap strings into nicely formatted

paragraphs. str_wrap(sentences, 20)

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at stringr.tidyverse.org • Diagrams from @LVaudor  • stringr 1.2.0 • Updated: 2017-

String manipulation with stringr : : CHEAT SHEET

Detect Matches

str_detect (string, pattern ) Detect the

presence of a pattern match in a string.

str_detect(fruit, "a")

str_which (string, pattern ) Find the indexes of

strings that contain a pattern match.

str_which(fruit, "a")

str_count (string, pattern ) Count the number

of matches in a string.

str_count(fruit, "a")

str_locate (string, pattern ) Locate the

positions of pattern matches in a string. Also

str_locate_all. str_locate(fruit, "a")

Manage Lengths

TRUE TRUE FALSE TRUE

1 2 4 0 3 1 2

start end 2 4 4 7 NA NA 3 4

str_length (string) The width of strings (i.e.

number of code points, which generally equals

the number of characters). str_length(fruit)

str_pad (string, width, side = c("left", "right",

"both"), pad = " ") Pad strings to constant

width. str_pad(fruit, 17)

str_trunc (string, width, side = c("right", "left",

"center"), ellipsis = "...") Truncate the width of

strings, replacing content with ellipsis.

str_trunc(fruit, 3)

str_trim (string, side = c("both", "left", "right"))

Trim whitespace from the start and/or end of a

string. str_trim(fruit)

4 6 2 3

Helpers

str_order (x, decreasing = FALSE, na_last =
TRUE, locale = "en", numeric = FALSE, ...) 1 Return
the vector of indexes that sorts a character
vector. x[str_order(x)]
str_sort (x, decreasing = FALSE, na_last = TRUE,
locale = "en", numeric = FALSE, ...)^1 Sort a
character vector.
str_sort(x)

4 1 3 2

Order Strings

The stringr package provides a set of internally consistent tools for working with character strings, i.e. sequences of characters surrounded by quotation marks.

NA NA

Subset Strings

str_sub (string, start = 1L, end = -1L) Extract

substrings from a character vector.

str_sub(fruit, 1, 3); str_sub(fruit, -2)

str_subset (string, pattern ) Return only the

strings that contain a pattern match.

str_subset(fruit, "b")

str_extract (string, pattern ) Return the first

pattern match found in each string, as a vector.

Also str_extract_all to return every pattern

match. str_extract(fruit, "[aeiou]")

str_match (string, pattern ) Return the first

pattern match found in each string, as a

matrix with a column for each ( ) group in

pattern. Also str_match_all.

str_match(sentences, "(a|the) ([^ ]+)")

NA

1 See bit.ly/ISO639-1 for a complete list of locales.
1 2 ... n
n ... m

regexp matches example

a? zero or one quant("a?") .a.aa.aaa
a * zero or more quant("a*") .a.aa.aaa
a + one or more quant("a+") .a.aa.aaa
a {n} exactly n quant("a{2}") .a.aa.aaa
a {n, } n or more quant("a{2,}") .a.aa.aaa
a {n, m} between n and m quant("a{2,4}") .a.aa.aaa

string (type this)

regexp (to mean this)

matches (which matches this)

example (the result is the same as ref("abba"))

\1 \1 (etc.) first () group, etc. ref("(a)(b)\2\1") abbaab

regexp matches example

^ a start of string anchor("^a") aaa
a $ end of string anchor("a$") aaa

regexp matches example

ab | d or alt("ab|d") abcde
[ abe ] one of alt("[abe]") abcde
[^ abe ] anything but alt("[^abe]") abcde
[ a - c ] range alt("[a-c]") abcde
regex (pattern, ignore_case = FALSE, multiline =
FALSE, comments = FALSE, dotall = FALSE, ...)
Modifies a regex to ignore cases, match end of
lines as well of end of strings, allow R comments
within regex's , and/or to have. match everything
including \n.
str_detect("I", regex("i", TRUE))
fixed () Matches raw bytes but will miss some
characters that can be represented in multiple
ways (fast). str_detect("\u0130", fixed("i"))
coll () Matches raw bytes and will use locale
specific collation rules to recognize characters
that can be represented in multiple ways (slow).
str_detect("\u0130", coll("i", TRUE, locale = "tr"))
boundary () Matches boundaries between
characters, line_breaks, sentences, or words.
str_split(sentences, boundary("word"))

Special Character Represents

\ \
" "
\n new line

Need to Know Regular Expressions -

Pattern arguments in stringr are interpreted as
regular expressions after any special characters
have been parsed.
In R, you write regular expressions as strings,
sequences of characters surrounded by quotes
( "" ) or single quotes( '' ).
Some characters cannot be represented directly
in an R string. These must be represented as
special characters , sequences of characters that
have a specific meaning., e.g.

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at stringr.tidyverse.org • Diagrams from @LVaudor  • stringr 1.2.0 • Updated: 2017-

Run ?"'" to see a complete list
Because of this, whenever a \ appears in a regular
expression, you must write it as \ in the string
that represents the regular expression.
Use writeLines () to see how R views your string
after all special characters have been parsed.
writeLines("\.")
# .
writeLines("\ is a backslash")
# \ is a backslash

MATCH CHARACTERS

QUANTIFIERS^ quant <- function(rx) str_view_all(".a.aa.aaa", rx)

ANCHORS^ anchor <- function(rx) str_view_all("aaa", rx)

GROUPS

Use parentheses to set precedent (order of evaluation) and create groups
Use an escaped number to refer to and duplicate parentheses groups that occur
earlier in a pattern. Refer to each group by its order of appearance
ref <- function(rx) str_view_all("abbaab", rx)

ALTERNATES alt <- function(rx) str_view_all("abcde", rx)

LOOK AROUNDS^ look <- function(rx) str_view_all("bacad", rx)

INTERPRETATION

Patterns in stringr are interpreted as regexs To
change this default, wrap the pattern in one of:

regexp matches example

( ab | d ) e sets precedence alt("(ab|d)e") abcde
see <- function(rx) str_view_all("abc ABC 123\t.!?\(){}\n", rx)
Regular expressions, or regexps, are a concise language for
describing patterns in strings.

a b c d e f

g h i j k l

m n o p q r

s t u v w x

z

[:lower:]

A B C D E F

G H I J K L

M N O P Q R

S T U V W X

Z

[:upper:]

[:alpha:]

[:digit:]

[:alnum:]

[:punct:]

. , : ;?! \ | / ` = * + - ^

_ ~ " ' [ ] { } ( ) < > @ # $

[:graph:]

[:blank:].

[:space:]

space

tab

 new line

(^1) Many base R functions require classes to be wrapped in a second set of [ ] , e.g. [[:digit:]]

string (type this)

regexp (to mean this)

matches (which matches this)

example

a (etc.) a (etc.) see("a") abc ABC 123 .!?(){}
\. .. see("\.") abc ABC 123 .!?(){}
\! !! see("\!") abc ABC 123 .!?(){}
\? ?? see("\?") abc ABC 123 .!?(){}
\\ \ \ see("\\") abc ABC 123 .!?(){}
\( ( ( see("\(") abc ABC 123 .!?(){}
\) ) ) see("\)") abc ABC 123 .!?(){}
\{ { { see("\{") abc ABC 123 .!?(){}
\} } } see( "\}") abc ABC 123 .!?(){}
\n \n new line (return) see("\n") abc ABC 123 .!?(){}
\t \t tab see("\t") abc ABC 123 .!?(){}
\s \s any whitespace ( \S for non-whitespaces) see("\s") abc ABC 123 .!?(){}
\d \d any digit ( \D for non-digits) see("\d") abc ABC 123 .!?(){}
\w \w any word character ( \W for non-word chars) see("\w") abc ABC 123 .!?(){}
\b \b word boundaries see("\b") abc ABC 123 .!?(){}
[:digit:] digits see("[:digit:]") abc ABC 123 .!?(){}
[:alpha:] letters see("[:alpha:]") abc ABC 123 .!?(){}
[:lower:] lowercase letters see("[:lower:]") abc ABC 123 .!?(){}
[:upper:] uppercase letters see("[:upper:]") abc ABC 123 .!?(){}
[:alnum:] letters and numbers see("[:alnum:]") abc ABC 123 .!?(){}
[:punct:] punctuation see("[:punct:]") abc ABC 123 .!?(){}
[:graph:] letters, numbers, and punctuation see("[:graph:]") abc ABC 123 .!?(){}
[:space:] space characters (i.e. \s) see("[:space:]") abc ABC 123 .!?(){}
[:blank:] space and tab (but not new line) see("[:blank:]") abc ABC 123 .!?(){}
. every character except a new line see(".") abc ABC 123 .!?(){}

1 1 1 1 1 1 1 1 1

1 2 ... n

regexp matches example

a (?= c ) followed by look("a(?=c)") bacad
a (?! c ) not followed by look("a(?!c)") bacad
(?<= b ) a preceded by look("(?<=b)a") bacad
(?<! b ) a not preceded by look("(?<!b)a") bacad
Make an interval with interval () or %--% , e.g.
i <- interval (ymd("2017-01-01"), d) ## 2017-01-01 UTC--2017-11-28 UTC
j <- d %--% ymd("2017-12-31") ## 2017-11-28 UTC--2017-12-31 UTC

PERIODS DURATIONS

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at lubridate.tidyverse.org • lubridate 1.6.0 • Updated: 2017-

years (x = 1) x years.
months (x) x months.
weeks (x = 1) x weeks.
days (x = 1) x days.
hours (x = 1) x hours.
minutes (x = 1) x minutes.
seconds (x = 1) x seconds.
milliseconds (x = 1) x milliseconds.
microseconds (x = 1) x microseconds
nanoseconds (x = 1) x nanoseconds.
picoseconds (x = 1) x picoseconds.
period (num = NULL, units = "second", ...)
An automation friendly period constructor.
period(5, unit = "years")
as.period (x, unit) Coerce a timespan to a
period, optionally in the specified units.
Also is.period (). as.period(i)
period_to_seconds (x) Convert a period to
the "standard" number of seconds implied
by the period. Also seconds_to_period ().
period_to_seconds(p)
Add or subtract periods to model events that happen at specific clock
times, like the NYSE opening bell.
dyears (x = 1) 31536000x seconds.
dweeks (x = 1) 604800x seconds.
ddays (x = 1) 86400x seconds.
dhours (x = 1) 3600x seconds.
dminutes (x = 1) 60x seconds.
dseconds (x = 1) x seconds.
dmilliseconds (x = 1) x x^10 -3^ seconds.
dmicroseconds (x = 1) x x^10 -6^ seconds.
dnanoseconds (x = 1) x x^10 -9^ seconds.
dpicoseconds (x = 1) x x^10 -12^ seconds.
duration (num = NULL, units = "second", ...)
An automation friendly duration
constructor. duration(5, unit = "years")
as.duration (x, …) Coerce a timespan to a
duration. Also is.duration (), is.difftime ().
as.duration(i)
make_difftime (x) Make difftime with the
specified number of units.
make_difftime(99999)
Make a period with the name of a time unit pluralized , e.g.
p <- months(3) + days(12)
p
"3m 12d 0H 0M 0S"
Make a duration with the name of a period prefixed with a d , e.g.
dd <- ddays(14)
dd
"1209600s (~2 weeks)"
Add or subtract durations to model physical processes, like battery life.
Durations are stored as seconds, the only time unit with a consistent length.
Difftimes are a class of durations found in base R.

Number of days etc.

Number of months

Exact length in seconds

Equivalent in common units

INTERVALS

Divide an interval by a duration to determine its physical length, divide
an interval by a period to determine its implied length in clock time.

Math with Date-times — Lubridate provides three classes of timespans to facilitate math with dates and date-times

a %within% b Does interval or date-time a fall
within interval b? now() %within% i
int_start (int) Access/set the start date-time of
an interval. Also int_end (). int_start(i) <- now();
int_start(i)
int_aligns (int1, int2) Do two intervals share a
boundary? Also int_overlaps (). int_aligns(i, j)
int_diff (times) Make the intervals that occur
between the date-times in a vector.
v <-c(dt, dt + 100, dt + 1000); int_diff(v)
int_flip (int) Reverse the direction of an
interval. Also int_standardize (). int_flip(i)
int_length (int) Length in seconds. int_length(i)
int_shift (int, by) Shifts an interval up or down
the timeline by a timespan. int_shift(i, days(-1))
as.interval (x, start, …) Coerce a timespans to
an interval with the start date-time. Also
is.interval (). as.interval(days(1), start = now())
l

Start Date

End Date

Math with date-times relies on the timeline ,
which behaves inconsistently. Consider how
the timeline behaves during:
A normal day

nor <- ymd_hms("2018-01-01 01:30:00",tz="US/Eastern")

The start of daylight savings (spring forward)

gap <- ymd_hms("2018-03-11 01:30:00",tz="US/Eastern")

The end of daylight savings (fall back)

lap <- ymd_hms("2018-11-04 00:30:00",tz="US/Eastern")

Leap years and leap seconds

leap <- ymd("2019-03-01")

12:00 1:00 2:00 3:

2019 2020 2021

1:00 2:00 3:00 4:

1:00 2:00 3:00 4:

Durations track the passage of
physical time, which deviates from
clock time when irregularities occur.
nor + dminutes(90)
gap + dminutes(90)
lap + dminutes(90)
leap + dyears(1)

12:00 1:00 2:00 3:

2019 2020 2021

1:00 2:00 3:00 4:

1:00 2:00 3:00 4:

Periods track changes in clock times,
which ignore time line irregularities.
nor + minutes(90)
gap + minutes(90)
lap + minutes(90)
leap + years(1)

12:00 1:00 2:00 3:

1:00 2:00 3:00 4:

1:00 2:00 3:00 4:

2019 2020 2021

Intervals represent specific intervals
of the timeline, bounded by start and
end date-times.
interval(nor, nor + minutes(90))
interval(gap, gap + minutes(90))
interval(lap, lap + minutes(90))
interval(leap, leap + years(1))

12:00 1:00 2:00 3:

1:00 2:00 3:00 4:

1:00 2:00 3:00 4:

2019 2020 2021

Not all years
are 365 days
due to leap days.
Not all minutes
are 60 seconds due to
leap seconds.
It is possible to create an imaginary date
by adding months , e.g. February 31st
jan31 <- ymd(20180131)
jan31 + months(1)
## NA
%m+% and %m-% will roll imaginary
dates to the last day of the previous
month.
jan31 %m+% months(1)
add_with_rollback (e1, e2, roll_to_first =
TRUE) will roll imaginary dates to the
first day of the new month.
add_with_rollback(jan31, months(1),
roll_to_first = TRUE)

Package: mypackage Title: Title of Package Version: 0.1. Authors@R: person("Hadley", "Wickham", email = "hadley@me.com", role = c("aut", "cre")) Description: What the package does (one paragraph) Depends: R (>= 3.1.0) License: GPL- LazyData: true Imports: dplyr (>= 0.4.0), ggvis (>= 0.2) Suggests: knitr (>= 0.1.0)

Suggest packages that are not very essential to yours. Users can install them manually, or not, as they like.

Use � tests/ to store tests that will alert you if your code breaks.

Add a tests/ directory

Import testthat with devtools:: use_testthat() , which

sets up package to use automated tests with testthat

Write tests with context() , test() , and expect statements

Save your tests as .R files in tests/testthat/

1. Modify your code or tests.

2. Test your code with one of

devtools:: test()

Runs all tests in � tests/

Ctrl/Cmd + Shift + T

(keyboard shortcut)

3. Repeat until all tests pass

context("Arithmetic")

test_that("Math works", { expect_equal(1 + 1, 2) expect_equal(1 + 2, 3) expect_equal(1 + 3, 4) })

Package Structure

Package Development: : CHEAT SHEET

Setup (� DESCRIPTION)

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at http://r-pkgs.had.co.nz/ • devtools 1.5.1 • Updated: 2015-

The contents of a package can be stored on disk as a:

- source - a directory with sub-directories (as above) - bundle - a single compressed file (.tar.gz) - binary - a single compressed file optimized for a specific OS

Or installed into an R library (loaded into memory during an R

session) or archived online in a repository. Use the functions

below to move between these states.

install.packages() CRAN ○

install.packages( type = "source" ) CRAN ○

R CMD install ○ ○

devtools:: install() ○

devtools:: build() ○ ○

devtools:: install_github() github^ ○

devtools:: load_all() ○ ○

Build & Reload (RStudio) ○ ○ ○

library() ○ ○

Internet On disk library memory

RepositorySourceBundleBinaryInstalledIn memory

A package is a convention for organizing files into directories.

This sheet shows how to work with the 7 most common parts of

an R package:

� Package

� DESCRIPTION

� R/

� tests/

� man/

� vignettes/

� data/

� NAMESPACE

devtools:: use_build_ignore( "file" )

Adds file to .Rbuildignore, a list of files that will not be included

when package is built.

SETUP

WRITE CODE

TEST

DOCUMENT

TEACH

ADD DATA

ORGANIZE

You must have a DESCRIPTION file

Add the packages that yours relies on with

devtools:: use_package()

Adds a package to the Imports or Suggests field

MIT license applies to your code if re-shared.

MIT

Visit r-pkgs.had.co.nz to

learn much more about

writing and publishing

packages for R

All of the R code in your package goes in � R/. A package with just

an R/ directory is still a very useful package.

Create a new package project with

devtools:: create( "path/to/name" )

Create a template to develop into a package.

Save your code in � R/ as scripts (extension .R)

1. Edit your code.

2. Load your code with one of

devtools:: load_all()

Re-loads all saved files in � R/ into memory.

Ctrl/Cmd + Shift + L (keyboard shortcut)

Saves all open files then calls load_all().

3. Experiment in the console.

4. Repeat.

- Use consistent style with r-pkgs.had.co.nz/r.html#style - Click on a function and press F2 to open its definition - Search for a function with Ctrl +.

Import packages that your package must have to work. R will install them when it installs your package.

GPL-2 license applies to your code, and all code anyone bundles with it, if re-shared.

GPL-

No strings attached.

CC

WORKFLOW

Write Code ( � R/)

Expect statement Tests

expect_equal() is equal within small numerical tolerance? expect_identical() is exactly equal? expect_match() matches specified string or regular expect_output() expression?prints specified output? expect_message() displays specified message? expect_warning() displays specified warning?

expect_error() throws specified error? expect_is() output inherits from certain class? expect_false() returns FALSE? expect_true() returns TRUE?

Example Test

WORKFLOW

Test ( � tests/)

The � DESCRIPTION file describes your work, sets up how your

package will work with other packages, and applies a copyright.


  1. Here is the footnote.