Skip to content

Instantly share code, notes, and snippets.

View tjmahr's full-sized avatar
🍍
sampling

TJ Mahr tjmahr

🍍
sampling
View GitHub Profile
@tjmahr
tjmahr / compute_bias.R
Last active January 21, 2016 15:31
find the most viewed image in a time window
compute_bias <- function(looks, window, t_col = "Time", aoi_col = "GazeByImageAOI", sample_rate = NULL) {
# Create filtering expression by plugging in the name of the "Time" column
interp <- lazyeval::interp
pred <- interp(~ between(x, min(w), max(w)), x = as.name(t_col), w = window)
# Keep just looks in range defined by window
subtrial <- looks %>% filter_(pred)
# Make sure that there is only set of looks in the data
window_times <- subtrial[[t_col]]
sample_n_groups <- function(df, n, group_keys = as.character(groups(df))) {
# Remember original grouping
group_keys_orig <- as.character(groups(df))
stopifnot(length(group_keys) != 0)
df_sub <- df %>%
# Keep just the grouping columns
group_by_(.dots = group_keys) %>%
select(one_of(group_keys)) %>%
distinct %>%
@tjmahr
tjmahr / gantt.R
Last active January 26, 2016 18:58
gantt diagram of a experimental trials
# quick gantt diagrams of a trial of an eyetracking experiment
library("dplyr")
library("ggplot2")
# describe each event
trial_events <- list()
trial_events$warmup <- list(
duration = 2000,
see = "four images",
@tjmahr
tjmahr / in-todo.Rmd
Last active February 25, 2016 20:32
knitr inline todos
---
title: "knitr todos"
author: "Tristan Mahr"
date: "February 25, 2016"
output:
html_document:
keep_md: yes
---
```{r}
@tjmahr
tjmahr / test.Rmd
Last active June 27, 2016 19:27
old dplyr
``` r
library("dplyr", warn.conflicts = FALSE)
iris %>% select(Species, starts_with("Color")) %>% head
#> Species
#> 1 setosa
#> 2 setosa
#> 3 setosa
#> 4 setosa
#> 5 setosa
@tjmahr
tjmahr / linting_demo.md
Last active July 29, 2016 01:33
Quick R Script to Lint a Manuscript

Quick Script to Lint a Manuscript

Tristan Mahr July 28, 2016

I am finalizing a manuscript. I wrote the whole thing in RMarkdown. Here is some of the first four paragraphs of the outputted Markdown file.

library("stringr")
all_lines <- readr::read_lines("reports/00_report.md")
@tjmahr
tjmahr / density.R
Last active September 20, 2016 04:03
Density curve annotations
library("ggplot2")
library("dplyr")
# Some toy data
davis <- car::Davis %>%
filter(100 < height)
# Fit a model and estimate mean at five points
m <- lm(weight ~ height, davis)
newdata <- data_frame(height = c(15:19 * 10))
library("stringr")
# # Maybe unescape brackets?
# str_replace("\\[", "\\\\\\[", "[")
# str_replace_all(lines_to_check[139], "\\\\\\[", "[")
# # enforce_nonbreaking?
# lines_to_check %>%
# str_replace_all(" = ", "&nbsp;=&nbsp;") %>%
@tjmahr
tjmahr / only_if.R
Created October 11, 2016 20:52
only_if.R
library("dplyr")
only_if <- function(data, condition, pipeline) {
if (condition) {
pipeline(data)
} else {
data
}
}
@tjmahr
tjmahr / purrr.R
Created November 29, 2016 18:41
reading files with purrr
my_dir <- tempdir()
readr::write_csv(iris, file.path(my_dir, "iris1.csv"))
readr::write_csv(iris, file.path(my_dir, "iris2.csv"))
readr::write_csv(iris, file.path(my_dir, "iris3.csv"))
readr::write_csv(iris, file.path(my_dir, "iris4.csv"))
file_paths <- list.files(my_dir, pattern = "iris", full.names = TRUE)
# Force an error so safely() does something
file.remove(file.path(my_dir, "iris3.csv"))