Skip to content

Instantly share code, notes, and snippets.

@crsh
crsh / example.rmd
Last active August 29, 2015 14:06
Embedding data into RMarkdown documents
# Embedding data into RMarkdown documents
<details>
<summary>Show data</summary>
```{r}
cars <- structure(list(speed = c(4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11,
12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16,
16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20, 20, 20, 20, 20,
22, 23, 24, 24, 24, 24, 25), dist = c(2, 10, 4, 22, 16, 10, 18,
@crsh
crsh / gist:4f9ce67f408611bc3974
Created May 7, 2015 16:04
Add events with time frame to plot.gantt()
# Function definition
add_event <- function(start, end = NULL, label = NULL, line = 0.5, las = 1, col = scales::alpha("steelblue", 0.5)) {
if(is.null(end)) {
end <- paste(start, "23:59:59")
start <- paste(start, "00:00:00")
}
start <- as.POSIXct(start)
end <- as.POSIXct(end)
@crsh
crsh / variable_labels.R
Last active April 17, 2019 21:42
Demonstration of variable.labels attribute in RStudio Viewer and Hmisc::label
label_test <- cars
attr(label_test, "variable.labels") <- c("Speed (km/h)", "Distance traveled (km)")
View(label_test)
str(label_test) # data.frame attribute
# Alternative using Hmisc (does not show up in RStudio Viewer)
label_test <- cars
Hmisc::label(label_test$speed) <- "Speed (km/h)"
Hmisc::label(label_test$dist) <- "Distance traveled (km)"
@crsh
crsh / sim_uninformative_bf.R
Last active November 6, 2015 11:18
Simulation of uninformativ datasets for Bayesian t-tests
n_bf <- 50
# Simulate data
library("BayesFactor")
previous_data <- rnorm(2, mean = 0, sd = 3)
uninformative_bf <- function(x, previous_data = NULL, mu = 0) {
bf <- as.vector(ttestBF(c(previous_data, x), mu = mu))
(1 - bf)^2
}
@crsh
crsh / batch_read.R
Last active February 1, 2018 10:47
Function to quickly rbind multiple local data-files
batch_read <- function(path, pattern, recursive = FALSE, read_fun, ...) {
data.files <- list.files(path, pattern = pattern, recursive = recursive)
data <- lapply(paste0(path, data.files), read_fun, ...)
data <- do.call("rbind", data)
data
}
@crsh
crsh / batch_read_github.R
Last active January 31, 2018 17:05
Function to quickly rbind multiple data-files from a GitHub repository
batch_read_github <- function(url, pattern, read_fun, ...) {
if(!require("rvest")) stop("Please install the 'rvest' package.")
if(!require("RCurl")) stop("Please install the 'RCurl' package.")
# Fetch file names
github_page <- read_html(url)
file_nodes <- html_nodes(github_page, ".content .css-truncate-target .js-navigation-open")
file_names <- html_text(file_nodes)
file_url <- html_attr(file_nodes, "href")[grep(pattern, file_names)]
@crsh
crsh / repeated_measures_correlation.R
Last active April 5, 2017 18:46
Calculate correlation among repeated measures in a factorial design
# x data.frame. Aggregated data for the factorial design, e.g., from aggregate(measure ~ subject * factor1 * factor2, data, mean).
# measure Character. Name of the measure.
# formula Formula. A formula specifying the factor (combination) for which to calculate the correlation, e.g., `subject ~ factor1` for a
# main effect or `subject ~ factor1 + factor2` (yes, it needs to be a "+") for an interaction. Note that G*Power can be
# used to perform power analyses for up to two repeated measures factors as long as one of them has only two levels.
# To do so, enter the larger number of factor levels into the field "Number of measurements" and multiply the effect
# size f by √p, where p is the number of levels of the other factor). If both factor have more than two levels G*Power
# will underestimate the required sample size! (See http://goo.gl/RgciM4 [German] for details)
# type Character. Specifies the estimation app
@crsh
crsh / plot_dependencies.R
Last active January 15, 2021 08:35
Plots the network of package dependencies
#' Plot network of package dependencies
#'
#' @param pkg package description, can be path or package name. See \code{\link[devtools]{as.package}} for
#' more information.
#'
#' @details The resulting plot visualizes the network of package dependencies. If you are trying to cut down
#' on package dependencies look for big red dots that represent a lot of upstream but few downstream
#' dependencies.
#' @import ggplot2
#' @export
@crsh
crsh / plot_state_control.R
Created February 13, 2017 22:04
Plot control of both chambers of the legislature and the governor
library("dplyr")
library("tidyr")
library("likert")
library("papaja")
dat <- data.frame(
Year = factor(seq(2009, 2017, 2))
, Democrats = c(17, 11, 12, 7, 6)
, Mixed = c(24, 17, 14, 20, 19)
, Republicans = c(9, 22, 24, 23, 25)
@crsh
crsh / install_load_all.R
Last active June 15, 2020 14:39
Function to (install and) load all required packages
install_load_all <- function(x) {
.install_load_all <- function(x) {
if(!require(x, character.only = TRUE)) {
install.packages(x, repos = "http://cran.us.r-project.org")
library(x, character.only = TRUE)
}
}
invisible(sapply(x, .install_load_all))
}