Skip to content

Instantly share code, notes, and snippets.

View tomhopper's full-sized avatar

Tom Hopper tomhopper

  • Michigan, United States
View GitHub Profile
tomhopper / sorting_vars.R
Created July 20, 2019 00:01
Sort a grouping variable by a second numeric variable, in the presence of a faceting variable
# Sort a grouping variable by a second numeric variable, in the presence of a faceting variable
# Ostensibly so that the grouping variable plots in ascending (or descending) order of the
# central tendency of the numeric variable.
# Create a dataset
my_df <- tibble(facet_var = rep(letters[1:4], each = 5*10),
group_var = rep(rep(letters[13:17], each = 10), times = 4),
tomhopper / knitr_Wide_Output.Rmd
Last active January 16, 2019 23:53
Create a code chunk in an RMarkdown document that is wider than the page and scrolls horizontally.
First-thing at the top of the Rmd file, after the YAML header, is the
following css style block.
pre {
overflow-x: auto;
pre code {
word-wrap: normal;
white-space: pre;
tomhopper / concatenation.R
Created November 27, 2018 17:53
Produce a single string from a character vector of strings in R
# Set up some test data
my_names <- letters[1:5]
# [1] "a" "b" "c" "d" "e"
# We want a list, like "a, b, c, d, e"
# Paste the names together
# [1] "a" "b" "c" "d" "e"
tomhopper / rmarkdown_num_equations.rmd
Last active April 29, 2018 21:46
Demonstration of creating automatically-numbered equations in RMarkdown documents.
At the top of your R markdown, beneath the yaml title block, add the following `<script>` section
<script type="text/x-mathjax-config">
TeX: {
equationNumbers: {
autoNumber: "all",
formatNumber: function (n) {return n}
tomhopper / captioning_rmarkdown.RMD
Last active April 29, 2018 13:16
Example of creating captions and automatic numbering in an RMarkdown document.
The _captioner_ library makes this work.
```{r libraries}
Before captioning any tables, we have to set up the table captions and the numbering using `captioner::captioner()`. _captioner_ numbers tables in the order they appear in this code block, so _tab_curve_ will be table 1, and _tab_comp_ will be table 2, wherever they appear in the document.
```{r setup_captions}
table_nums <- captioner(prefix = "Table")
tomhopper / getCRANPackages.R
Created February 21, 2018 22:27
Returns a data frame of selected information on all packages on CRAN
#' @description Returns a list of all packages on CRAN
#' @param columns_list A character vector of field names to return from package DESCRIPTION files
#' @return A data frame containing all packages on CRAN
#' @details Function modified from StackOverflow answer at \url{}.
#' @importFrom magrittr %>%
#' @importFrom tibble as.tibble
#' @importFrom dplyr select_
getCRANPackages <- function(columns_list = c("Package", "Title", "Version", "Date", "Published", "URL")) {
contrib.url(getOption("repos")["CRAN"], "source")
description <- sprintf("%s/web/packages/packages.rds",
tomhopper / copy_paste.R
Created February 13, 2018 21:30
Copy and paste data into R
# Paste the data into a string:
data_string <- " Filename Fc
Q5161811.04 20.36
Q5161811.04 20.46
Q5161811.04 24.17
Q5161811.04 20.20 "
# Use a fileConnection to read the data into a data frame
the_data <- read.table(file = textConnection(object = data_string),
header = TRUE,
tomhopper / limit_legend.R
Created February 9, 2018 13:42
When using many factor levels, a ggplot2 legend can become too busy to be useful. This shows how to pair back the labels in the legend to something readable, with less detail.
# Limit factor levels displayed in ggplot2 legend
# When using many factor levels, the legend can become too large to be useful
# We want to pair back the legend to something readable, with less detail.
# For our minimally reproducible example, we will create a series of 40 sinusoidal
# curves that are slightly offset from each other, then plot each one in a different
# color. The resulting plot will have a somewhat rainbow look, but the legend will
# contain 40 labels; we want to reduce this to about 5 labels in the legend.
# Libraries used
tomhopper / central_limit_theorem.R
Created October 30, 2017 14:54
Demonstration of central limit theorem
## Demonstration of central limit theorem
## Based on code in an anonymous comment to the blog post at \url{}
## Libraries ####
## Data used ####
# right-triangle distribution (peak at 0; minimum at 1)
tomhopper / data_from_packages.R
Created October 30, 2017 11:56
Programmatically access data provided by packages in R
## Access data provided by packages
## List all available data sets, plus ancillary information ####
the_data <- data()
## Extract just the packages as a data frame ####
the_data_df <- data.frame(the_data$results, stringsAsFactors = FALSE)
## Extract specific data sets ####
specific_data <- get(the_data_df$Item[1])