Learning R and RStudio

 

These notes have evolved from a presentation at Li & Fung in the Fall of 2016. Revision inspired by Allan Miller & I leading a discussion on “How to Learn R” at the Berkeley R Beginners meetup.

A quick start to R and data science.

In the olden days, 10 years ago, I spent a lot of time build links and making tricks & tips list. Now I just point to RStudio resources and a couple of my favorite books.

The classical way to learn R is to work through the all basic ideas in “base R” and then learn the modern enhancements and packages. If you like this approach see the just released 832 page tome The Book of R: A First Course in Programming and Statistics will appeal to you. It is actually a pretty good book. But…

My prefered way to teach R today is to leverage the new way of working in R – much of which has been developed by folks now on the RStudio team. In particular Hadley Wickham and his students who have created the “tidyverse” (formerly known as the “Hadleyverse”, but Hadley is getting modest in his old age).

Jim’s Principles for Getting Started in R

  1. Use RStudio!!!
  2. Live in the tidyverse
  3. Use RStudio’s project framework
  4. Invest in learning ggplot2 concepts (don’t start with qplot()!)
  5. Deliver your results via RMarkdown
    1. And use R Notebooks for development and EDA.
  6. Use git & GitHub.  See:
    1. Josh’s Version Control with Git (& SVN)
    2. Git/GitHub chapter in Hadley’s R Packages
    3. Jenny Bryan’s complete notes http://happygitwithr.com/ from here useR! 2016 tutorial
  7. Package up your tools

Step-by-step guide

  1. Load R
  2. Load RStudio Desktop (IDE)
  3. See RStudio’s guide to on-line learning, in particular
    1. Their archive of excellent webinars, and
    2. Garrett & friends have great cheat sheets
  4. Read Garrett & Hadley’s R for Data Sciencea work in progress now complete & in print (AKA The Tidyverse Guide)
    1. Hadley just finished the Graphics for Communication chapter in which he mentions the ggplot2 extensions site.
  5. Practice on real data! Perhaps while finishing R for Data Science. You now know enough to do real work.
  6. Catch up with ggplot2 developments
    1. Winston’s Cookbook for R (with link to his R Graphics Cookbook)
  7. Read & follow advice in Hadley’s R Packages book
  8. For the brave programmers: Hadley’s Advanced R 

Other Resources

Favorite Books

(In addition to above, these predate Tideverse)

Tricks & Tips

dplyr & tidyr Quick Start

I like Brad Boehmke’s Data Processing with dplyr & tidyr

Google Drive Access

Google Sheets in R

Use the package googlesheets (duh!) see Jenny’s GitHub https://github.com/jennybc/googlesheets

Interesting Projects

Posted in R, Tricks & Tips

WordPress Posts via R Markdown

I’m totally (re-)inspired with RMarkdown at useR! 2016. A lot going on
including bookdown and markdown documents. The latter making WordPress
blogging reasonable. Here is my first R Markdown generated post.

BTW, I’m putting the .Rmd’s up on my GitHub
here

The trick is WordPress will optionally accept .md files as input to the
blog post edit window. To enable, follow the directions on the
WordPress Markdown page.

R Markdown can produce plain markdown files with variants for different
dialects. See the RStudio Markdown Documents
page

For WordPress, the YAML output parameter should read

output: 
  md_document:
    variant: markdown_phpextra+backtick_code_blocks

The workflow is to knit your .Rmd, then copy the generated markdown and
paste into your WordPress Blog Posts|Add|HTML tab.

There are two gotchas:

  • “Note however that if you have embedded plots or other images you’ll
    need to upload them separately and fix up their URLs to point to the
    uploaded location.” (from above RStudio page)
  • WordPress does not seem to pick up .md title in its Title box. You
    will need to manually cut & paste the title.

Default RStudio Boilerplate

As an example we use the RStudio boilerplate. The workflow is:

  1. RStudio: Knit the .Rmd
  2. WordPress|Dashboard|Media: Add image(s) from the _files
    folder to the WordPress Media Library.
  3. WordPress|Dashboard|Posts|Add New: Copy the md text from RStudio &
    paste into HTML tab.
  4. Copy title text from md to Text & delete title lines from the body.
  5. For each image placeholder pointing to R project folder:
    • Delete image placeholder
    • Insert
      <img src="...File URL from media library..." alt="...whatever..." />
  6. Don’t forget to select categories and add tags.

  7. WordPress: Publish & View Post

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see .

When you click the Knit button a document will be generated that
includes both content as well as the output of any embedded R code
chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Pressure by Temperature

Note that the echo = FALSE parameter was added to the code chunk to
prevent printing of the R code that generated the plot.

Tagged with: ,
Posted in R

Why Customer Insights?

what is CI & why is it important?

More coming!

-Jim

Posted in Uncategorized
Recent Comments
Archives
Categories
A Portuguese Affair

A Blog About Portugal

R-bloggers

R news and tutorials contributed by hundreds of R bloggers