Category: Resources

Code to Transfer Package Library

Want to update R AND keep all your packages? Here is the code I used, and this is the website I got it from:

1. Before you upgrade, build a temp file with all of your old packages.

tmp <- installed.packages()
installedpkgs <- as.vector(tmp[[,"Priority"]), 1])
save(installedpkgs, file="installed_old.rda")

2. Install the new version of R and let it do it’s thing.

3. Once you’ve got the new version up and running, reload the saved packages and re-install them from CRAN.

tmp <- installed.packages() <- as.vector(tmp[[,"Priority"]), 1])
missing <- setdiff(installedpkgs,

Wrangle yourself some data!


Your data is a scrambled Rubik’s Cube, and tidyr & dplyr are your hands.

Today’s R Club will start with tutorials on some of Hadley Wickham’s amazing packages (bolded, below), with some time at the end to workshop your own data. So: if you’ve got data to wrangle, bring it!

tidyr, “designed specifically for data tidying”

dplyr, “A fast, consistent tool for working with data frame like objects”

magrittr, “Ceci n’est pas un pipe.”

  • See the GitHub Readme for more info on the syntax augmentations for the above packages.

Putting it all together

Regular Expressions

We had a nice chat about the uses of regular expressions in R, and determined we use them mainly for dealing with messy data files, or mutating the file names of data files, and for doing some linguistics data analysis tasks. That doesn’t sound like much, but they’re really amazing, and once you’ve started to use them, you’ll wonder how you ever went without.

Check out some of the useful base R functions that make use of them with ?grep:


     ‘grep’, ‘grepl’, ‘regexpr’ and ‘gregexpr’ search for matches to
     argument ‘pattern’ within each element of a character vector: they
     differ in the format of and amount of detail in the results.

     ‘sub’ and ‘gsub’ perform replacement of the first and all matches

And if you use tidyr, you’ll love to use them with extract.

How do you get started? Check out RegexOne. Once you complete all the lessons you’ll be set for a good long while. There are many other resources on the Internet.

Note well for regular expression usage in R: You’ll learn that backslash (\) gets used a lot in regular expressions. Well, it’s also a special character in R (for example, newline is '\n'). For that reason, when you write regular expressions in R, you need to use 2 slashes – so '\w' should actually be '\\w'.

Plotly Update

When first hit the scene, I was like, woah, this is awesome, knitr support and everything!

I recently asked them about building better dashboards, and they sent me this info — apparently there is some support for integrating graphs into shiny apps, if you’re already comfortable building shiny apps.

I’m hoping they start building in functionality that will make it even easier to integrate custom sliders and drop-downs for messing with graphs on the fly. For now, though, it’s still a really nice way to make your R output more interactive.

Interactive Embedded Plots with Plotly and ggplot2

Largley lifted from this r-bloggers post

install.packages("devtools")  # so we can install from GitHub
devtools::install_github("ropensci/plotly")  # plotly is part of rOpenSci

py <- plotly(username="jflournoy", key="mg34ox914h")  # open plotly connection
# I'll change my key after this, but you can still use: plotly(username="r_user_guide", key="mw5isa4yqp")
# Or just sign up for your own account!

gg <- ggplot(iris) +
    geom_point(aes(Sepal.Length, Sepal.Width,color=Species,size=Petal.Length))

#This looks a little object-oriented like python  

You can embed code like this (which you get from the plotly ‘share’ dialogue):

<a href="" target="_blank" title="Sepal.Width vs Sepal.Length" style="display: block; text-align: center;"><img src="" alt="Sepal.Width vs Sepal.Length" style="max-width: 100%;width: 797px;"  width="797" onerror="this.onerror=null;this.src='';" /></a>
<script data-plotly="jflournoy:16" src="" async></script>
Sepal.Width vs Sepal.Length

You can also directly embed a plotly plot using a code chunk if you set plotly=TRUE for the chunk, and include session="knitr" in the call.

#Set `plotly=TRUE`
py$ggplotly(gg, session="knitr")

There’s a wide world of plotly fun just waiting out there.


So useful!

Hadley explains (at the above link):

The dplyr package makes each of these [data processing] steps as fast and easy as possible by:

  • Elucidating the most common data manipulation operations, so that your options are helpfully constrained when thinking about how to tackle a problem.
  • Providing simple functions that correspond to the most common data manipulation verbs, so that you can easily translate your thoughts into code.
  • Using efficient data storage backends, so that you spend as little time waiting for the computer as possible.

The dplyr debut blog post may also be of interest.

R Graph Catalog

This is Shahar’s distance contribution to R Club. (Thanks, Shahar!)

This catalog allows you to choose which graph you like best and gives you the code for that graph. You can also filter by things like Good vs. Bad graphs, type of graph, and different features that you might like to include (like subscripts and multiple plots).