Instantly share code, notes, and snippets.

# Tom Hoppertomhopper

• Michigan, United States
• Sort options
Created Jul 20, 2019
Sort a grouping variable by a second numeric variable, in the presence of a faceting variable
View sorting_vars.R
 # 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. library(tidyverse) # Create a dataset set.seed(10001) my_df <- tibble(facet_var = rep(letters[1:4], each = 5*10), group_var = rep(rep(letters[13:17], each = 10), times = 4),
Last active Jan 16, 2019
Create a code chunk in an RMarkdown document that is wider than the page and scrolls horizontally.
View knitr_Wide_Output.Rmd
 First-thing at the top of the Rmd file, after the YAML header, is the following css style block.
Created Nov 27, 2018
Produce a single string from a character vector of strings in R
View concatenation.R
 # Set up some test data my_names <- letters[1:5] my_names # [1] "a" "b" "c" "d" "e" # We want a list, like "a, b, c, d, e" # Paste the names together paste(my_names) # [1] "a" "b" "c" "d" "e"
Last active Apr 29, 2018
Demonstration of creating automatically-numbered equations in RMarkdown documents.
View rmarkdown_num_equations.rmd
 At the top of your R markdown, beneath the yaml title block, add the following 
Last active Apr 29, 2018
Example of creating captions and automatic numbering in an RMarkdown document.
View captioning_rmarkdown.RMD
 The _captioner_ library makes this work. {r libraries} library(captioner)  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")
Created Feb 21, 2018
Returns a data frame of selected information on all packages on CRAN
View getCRANPackages.R
 #' @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{https://stackoverflow.com/a/11561793}. #' @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",
Created Feb 13, 2018
Copy and paste data into R
View copy_paste.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,
Created Feb 9, 2018
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.
View limit_legend.R
 # 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
Created Oct 30, 2017
Demonstration of central limit theorem
View central_limit_theorem.R
 ## Demonstration of central limit theorem ## Based on code in an anonymous comment to the blog post at \url{https://sas-and-r.blogspot.com/2012/01/example-919-demonstrating-central-limit.html} ## Libraries #### library(nortest) library(dplyr) library(ggplot2) ## Data used #### # right-triangle distribution (peak at 0; minimum at 1)
Created Oct 30, 2017
Programmatically access data provided by packages in R
View data_from_packages.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])
You can’t perform that action at this time.