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

A list

https://github.com/daattali/addinslist#readme

More Advanced

Debugging

Profiling

What is Debugging?

Debugging is merely finding and fixing problematic code.

  • Code will always have bugs

Debugging is an essential part of creating functions.

A note about functions

If you are doing anything more than twice, write a function!

  • It’s more generalizable
  • It’s more reproducible
  • It’s more efficient

Debugging can allow one to spot issues

RStudio can even help you get started transforming existing code to a function

  • Ctrl+Alt+X/Cmnd+Opt+X on highlighted code

Debugging in RStudio

There are numerous facilities within R to help you debug your code.

  • Break Points

  • browser

  • debug

  • debugonce

  • traceback

RStudio makes the process pretty easy.

Debug Mode Commands

There are commands that allow you to work through debugging:

  • Next (n)/Return: runs the next line

  • Step into (s): if the next line is a new function, it enters into the function

  • Careful with this one; you can get pretty far into other functions

  • Finish (f): finishes the function

  • Continue (c): stops debugging and runs the function

  • Stop (Q): stops debugging and does not run the function

Debug Mode Commands

Each of these also has a button in the debugging menu

Profiling

Code profiling allows one to see what parts of the code take most of the processing time and resources (memory)

Like debugging, there have always been tools in base R for this, but RStudio makes it easy to profile any code.

Furthermore, it doesn’t have to be an explicit function.

Profiling

Debugging and profiling

Debugging and profiling are important parts of advanced programming.

One should desire to make code as general and reproducible as possible.

  • Regardless of expertise!

RStudio makes the process more interactive and flexible.

Package Development

Package Development

RStudio makes package development easier too.

  • New Project/New Directory/R Package

R Package Dialog Box

“Create package based on source files”

  • allows you to include previously written functions in your new package.
  • functions you added at this step will have their own help files created.
  • You will still need to complete the help files.

What You Get

RStudio will automatically start you out with the following:

  • DESCRIPTION: Just like every R package

  • A ‘man’ folder: Contains .Rd files for each function

  • An ‘R’ folder: Contains your functions.

The roxygen2 package helps to properly format your documentation files.

Build, Reload, Check

After you have all of your files ready, build the package.

Packages tend to have a lot happening in them.

To help you make sure that the package has everything it needs, run the check function from devtools on it.

It will check package quality across many dimensions:

  • Ability to install package and its dependencies
  • Checking help file quality
  • Find errors in examples

Packages

RStudio has tools that make package creation a straight-forward process.

You should not be afraid to create your own packages

  • Even for just personal use!
  • Within a project greater reproducibility

Version Control

Overview

RStudio can integrate version control into your project.

  • Subversion
  • Git
  • Both are free and open (we’ll focus on Git)

Wait, Wait! What Is Version Control?

At its most basic, it is just a way to manage changes.

  • Documents, code, etc.

Especially useful when collaborating.

  • Track of who is making changes and what they are changing
  • Revert changes back to an earlier version
  • Merging multiple copies of a document into one
  • Branched development

Git

Git works on a distributed model

  • Users create their own local repositories

Tools that use Git to share code on the web

  • Bitbucket
  • GitLab
  • GitHub

Public vs. private repositories

GitHub

GitHub is a web-based hosting services for Git repositories.

Also a social Network for software and other developers.

Process (Briefly)

  • Commit changes made (including file creation)
  • Push up to repository
  • Pull from repository changes others have made

GUI vs. the Shell

RStudio makes it easy to commit, push, pull, revert, etc.

If you need other things, you can access the Git shell directly

Version Control

RStudio can integrate version control into your projects.

You have nothing to lose by keeping track of changes to files.

This is especially useful when collaborating.

Cheat Sheets

Cheat Sheets - RStudio Style

RStudio wants everything to be easy for us as R users.

They provide a series of cheat sheets as reference material.

https://www.rstudio.com/resources/cheatsheets/

RStudio

RStudio even has a cheatsheet for using RStudio!

Provides a high-level overview for many of the things discussed here.

Other Cheat Sheets

Data Visualization

Data Wrangling

R Markdown

Package Development

Shiny

Summary

RStudio can enhance and accelerate your R experience.

Not just for scripting!

It can serve you from the beginning of a project to whatever ends you take it.


THANKS!




*Special thanks to Seth Berry @ Notre Dame for loads of help with the content!