This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First-thing at the top of the Rmd file, after the YAML header, is the | |
following css style block. | |
<style> | |
pre { | |
overflow-x: auto; | |
} | |
pre code { | |
word-wrap: normal; | |
white-space: pre; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
At the top of your R markdown, beneath the yaml title block, add the following `<script>` section | |
--- | |
<script type="text/x-mathjax-config"> | |
MathJax.Hub.Config({ | |
TeX: { | |
equationNumbers: { | |
autoNumber: "all", | |
formatNumber: function (n) {return n} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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]) |
NewerOlder