Skip to content

Instantly share code, notes, and snippets.

View jennybc's full-sized avatar

Jennifer (Jenny) Bryan jennybc

View GitHub Profile
@jennybc
jennybc / 2014-08-22_rbind-and-store-as-var.Rmd
Last active August 8, 2016 03:26
Row bind a list of data.frames with a key
---
title: "Row bind a list of data.frames with a key"
author: "Jenny Bryan"
date: "22 August, 2014"
output:
html_document:
keep_md: TRUE
---
I posed a question on Twitter (click to see the figure!):
@jennybc
jennybc / knit-with-errors.rmd
Created September 8, 2014 06:53
Make knitr tolerate errors (snippet for reference)
```{r setup, include = FALSE, cache = FALSE}
knitr::opts_chunk$set(error = TRUE)
```
@jennybc
jennybc / yaml_frontmatter_r_github_document.yaml
Last active February 9, 2022 21:36
YAML frontmatter for R Markdown to cause rmarkdown::render() to retain intermediate Markdown file for .R and .Rmd files, respectively
#' ---
#' title: "Something fascinating"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
#' ---
@jennybc
jennybc / 2014-09-18_numeric-index-gotcha.md
Last active August 29, 2015 14:06
How quickly indexing with numeric can burn you

Inspired by recent @johnmyleswhite tweets on indexing (though they focus more on NA stuff)

My standard advice to R novices is "don't worry about numeric vs integer too much, for now", but I just created an indexing example that scares me.

I did not realize it was sooo easy to create this gotcha when indexing with numeric (which I know is not a great idea).

@jennybc
jennybc / 2014-09-18_verbatim-r-chunks-in rmd.rmd
Created September 19, 2014 05:57
How to get verbatim R chunks in R markdown. Again. Writing it down now.
---
title: "Get verbatim R chunks in R Markdown"
author: "Jenny Bryan"
date: "18 September, 2014"
output:
html_document:
keep_md: TRUE
---
My periodic revisitation of "how can I include a verbatim R chunk in `.rmd`"? This time I am writing it down! Various proposed solutions:
@jennybc
jennybc / 2014-09-29_knitr-treatment-errors.md
Created September 30, 2014 04:35
2014-09-29_knitr-treatment-errors

Demo errors in knitted doc vs. other

Jenny Bryan
29 September, 2014

Context: In a recent lesson, I claim (with some justification) that raising an error inside a function via if() ... stop() often gives more info than using stopifnot(). This is partly because you get to write your own error message. But also because you learn the name of the function that threw the error. Or so I thought.

Here's a toy function to generate an error.

@jennybc
jennybc / reverse-categorical-axis-ggplot2.r
Created October 10, 2014 01:06
Reverse the order of a categorical axis in ggplot2
scale_x_discrete(limits = rev(levels(the_factor)))
@jennybc
jennybc / 2014-10-12_stop-working-directory-insanity.md
Last active September 23, 2022 04:43
Stop the working directory insanity

There are packages for this now!

2017-08-03: Since I wrote this in 2014, the universe, specifically Kirill Müller (https://github.com/krlmlr), has provided better solutions to this problem. I now recommend that you use one of these two packages:

  • rprojroot: This is the main package with functions to help you express paths in a way that will "just work" when developing interactively in an RStudio Project and when you render your file.
  • here: A lightweight wrapper around rprojroot that anticipates the most likely scenario: you want to write paths relative to the top-level directory, defined as an RStudio project or Git repo. TRY THIS FIRST.

I love these packages so much I wrote an ode to here.

I use these packages now instead of what I describe below. I'll leave this gist up for historical interest. 😆

@jennybc
jennybc / 2014-11-05_dplyr-join-reprex.md
Created November 5, 2014 18:44
Demonstrate that dplyr::left_join() can mangle factor levels
suppressPackageStartupMessages(library(dplyr))

Get word counts for Character * Chapter in LOTR trilogy

if(!file.exists("lotr_clean.tsv")) {
  download.file(paste0("https://raw.githubusercontent.com/jennybc/",
 "lotr/master/lotr_clean.tsv"), 
@jennybc
jennybc / 2014-11-18_install-github-tmp-lib.r
Last active November 17, 2017 12:50
Install a package from, e.g., GitHub into a temporary library
## 2015-11-11 NOTE!
## with_libpaths() is one of the functions removed (well, deprecated, for now) from
## devtools
## it's in withr now!
## see newer script below!!!
library(devtools) # with_lib(), install_github()
tmp_lib <- "~/tmp/tmp_lib"
dir.create(tmp_lib)