Getting More from RStudio

Michael Clark
Statistician Lead


2016-07-13

Overview

Overview

Base R is an extremely powerful tool. However…

The syntax editor in Base R is just a simple text editor.

Rstudio is relatively more efficient and easy to use.

There are actually many options:

  • RStudio
  • General text editors like Notepad++, Sublime, Vim
  • Other R-based text editors e.g. Emacs Speaks Statistics
  • Other R-based IDE options e.g. Architect
  • Other IDEs with R functionality, e.g. Eclipse, Visual Studio

Overview

RStudio offers:

  • Code completion and snippets
  • Code diagnostics
  • Customizable shortcuts
  • Document generation (web, pdf, presentation, .doc)
  • Web publishing

Overview

RStudio offers:

  • Enhanced debugging, profiling
  • Navigable data frames
  • Version control
  • Interactive visualization
  • Addins

Overview

RStudio is also an excellent tool for reproducible research.

  • Project management
  • Package building and freezing
  • Document generation

Scripting

Scripting

Everyone who uses RStudio benefits from easier scripting:

  • Syntax highlighting
  • Autocomplete of object/function names, etc.
  • Autopairing of parenthesis, quotes etc.
  • Auto indent

It even makes working at the console viable.

  • Still not advised

Keyboard shortcuts: standard scripts

Knowing a few shortcuts can save a lot of time in the long run.

Examples (Windows and Linux):

Run current line: Ctrl+Entr

Copy up/down: Ctrl+Shft+up/down

Move up/down: Shft+up/down

Run everything: Ctrl+Shft+Entr

Insert section: Ctrl+Shft+R

Select Window: Ctrl+1:9

Alt+Shft+K

Some oft-used/favorite shortcuts

Multicursor: Ctrl + Alt + select

(Copy) Move lines: (shift+) alt + arrow

Clear console: Ctrl + L

Restart R: Ctrl + Shft + F10

Scripting/Console Window: Ctrl+1/Ctrl+2

Rerun previous: Ctrl + Shft + P

Run everything before: Ctrl + Alt + B

Run everything before: Ctrl + Alt + E

Knit: Ctrl + Shft + K

Keyboard shortcuts

Knowing just a dozen shortcuts could save a lot of time.

  • Bonus: Ctrl + Shft + A (tidy up your code)

You can customize them however you want.

Mac users: most of these would use Cmd and Option rather than Ctrl and Alt

Snippets

Snippets allow one to insert code of a certain form for commonly used functions.

You only have to type the first couple letters, the form of the rest of the code will fill out, then you can tab your way through the rest of it.

Good for:

  • starting functions
  • apply family of functions
  • even for other languages


Code Diagnostics

RStudio will note problems in your code in the margin.

  • Examples: hanging brackets, too many commas etc.

This works beyond just R scripts too!

Code Diagnostics

Customization

Customization

Customize various aspects of how RStudio

  • How it looks
  • How you interact with it.

Can apply to all RStudio sessions or for a particular project.

  • Tools/Global Options
  • Tools/Project Options

Customization

Start with the looks:

  • Code: indentation, highlighting etc.
  • Window pane locations

Customization

Customization

While you may certainly want to change things such as the look, to not save the workspace automatically etc., the main point is simply to be aware of what you can change.

Projects

Projects

Projects provide a self-contained ecosystem to work in.

  • Have their own working directory, workspace, etc.

You can easily jump between multiple projects.

  • Without losing your place

Projects

File/New Project

We’ll talk about version control later.

Projects

All tabs opened will remain open when you revisit the project.

You can have multiple projects running at the same time

  • i.e. multiple RStudio instances

Help you get more organized.

Help you get more reproducible.

Rmarkdown

Overview

What is R?

R is fast becoming a general programming environment

  • Not just for statistics anymore!

Markdown is a markup language.

  • Allows for easier web-based documentation.
  • Not necessary to know html.
  • Lots of things will use it.
  • Rmarkdown is a flavor.

Now one can intermingle R with markdown, html, css, JavaScript, \(\LaTeX\) and others resulting in a variety of products.

Rmarkdown

Rstudio and Rmarkdown make it easy to construct:

  • html, pdf, MS Word documents
  • presentations (like this one)
  • dashboards
  • notebooks
  • websites
  • other publications

Literate programming!

Reproducible research!

Example

R chunks are interspersed throughout the Rmd file.

Rmarkdown documents combine:

  • code
  • plain text
  • markdown
  • other

Rmarkdown

Once ready, knit the document into the format your want.

  • Ctrl/Cmd-Shft-K

An Rmarkdown workshop will be given with more details.

Examples

m-clark.github.io/docs/sem/

m-clark.github.io/docs/IntroBayes.html

Notebooks

Notebooks

Notebooks provide a different way to interact with R.

Previously one could save out some files as a ‘notebook’.

Now one can start an interactive R script.

  • chunks of R code can be executed independently as needed

Output is visible at the point where the code is written.

Notebooks

File/New File/R Noteboook…

Notebooks

You may still want to use (or prefer) the console

  • But you don’t have to.

The viewer will allow for previews of the final document.

Notebooks

Notebooks are a different flavor of Rmarkdown document.

Good for sharing your work.

See also, Jupyter.

Interactive and Visual Data Exploration

The Viewer

Along with the Plots pane, RStudio provides a Viewer pane.

Anything interactive will be displayed there.

Packages

ggplot2 is the most widely used package for visualization in R.

However, it does not produce interactive plots by nature.

Many packages use various Javascript libraries to provide interactive graphics.

  • d3
  • leaflet
  • vis
  • mermaid

Many utilize these packages via the htmlwidgets R package.

Packages

Some packages to note:

  • plotly
    • used also in Python, Matlab, Julia, aside from many interactive plots, can convert ggplot2 images to interactive ones.
  • ggvis
    • interactive successor to to ggplot though not currently actively developed
  • rbokeh
    • like plotly, also has cross program support
  • DT
    • interactive data tables

Example

Works in your presentations too.

Shiny

Shiny is a framework that can essentially allow you to build an interactive website.

  • Provided by RStudio developers

Most of the more recently developed visualization packages will work specifically within the shiny and rmarkdown settings.

Interactive and Visual Data Exploration

RStudio lets you take a deeper look at your data more easily.

Accessible interactivity.

Interactive graphics are more fun too!

Just a couple visualization packages can go a very long way.

Addins

Addins

One can create functions that can be used within RStudio with a click or keystroke.

These special functions are called addins.

Increase your productivity and efficiency when scripting.

They can be anything.

  • One of the more useful is text insertion/formatting.
  • Saves a lot of time in document creation

Creating Addins

Addins are in some sense nothing more than R functions that you can call interactively.

likeR()
I like R!

However, they can possibly do quite a bit.

Example: ColorPicker

Example: Radiant