Skip to content

Instantly share code, notes, and snippets.

@crsh
crsh / variable_labels_apa_table.R
Created January 18, 2019 20:52
Use variable labels to italicize column headers with apa_table()
my_table <- data.frame(t(apply(cars, 2, function(x) # Create data
round(c(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x)), 2)
)))
variable_labels(my_table) <- c("Mean" = "$\\textit{Mean}$", "SD" = "$\\textit{SD}$")
apa_table(
my_table
, align = c("l", rep("r", 3))
, caption = "A summary table of the cars dataset."
@crsh
crsh / apa6-meta.csl
Last active November 11, 2018 10:50
APA6 CSL file that adds an asterisk in front of any reference that has an annotation field
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="never" page-range-format="expanded">
<!-- This style was edited with the Visual CSL Editor (http://editor.citationstyles.org/visualEditor/) -->
<info>
<title>American Psychological Association 6th edition</title>
<title-short>APA</title-short>
<id>http://www.zotero.org/styles/american-psychological-association-6th-edition</id>
<link href="http://www.zotero.org/styles/american-psychological-association-6th-edition" rel="self"/>
<link href="http://owl.english.purdue.edu/owl/resource/560/01/" rel="documentation"/>
<author>
@crsh
crsh / apa6_no_disambiguation.csl
Created May 28, 2018 16:02
APA6 CSL file without author disambiguation
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="never">
<!-- This style was edited with the Visual CSL Editor (http://editor.citationstyles.org/visualEditor/) -->
<info>
<title>American Psychological Association 6th edition (no disambiguation)</title>
<title-short>APA</title-short>
<id>http://www.zotero.org/styles/apa</id>
<link href="http://www.zotero.org/styles/apa" rel="self"/>
<link href="http://owl.english.purdue.edu/owl/resource/560/01/" rel="documentation"/>
<author>
@crsh
crsh / batch_download_github.R
Last active February 1, 2018 11:16
Download data from GitHub repository
batch_download_github <- function(url, pattern, path, ...) {
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)]
file_names <- file_names[grep(pattern, file_names)]
@crsh
crsh / sideways_figure_papaja.Rmd
Created November 7, 2017 09:31
Landscape figures in papaja documents
---
title : "The title"
shorttitle : "Title"
author:
- name : "First Author"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Postal address"
email : "my@email.com"
@crsh
crsh / fetch_zotero_refs_example.R
Created October 2, 2017 22:01
Working example of fetch_zotero_refs()
# The ID of a Zotero user or group can be found via Zotero.org. E.g. the public group "PSYCH 490 ARTICLES"
# can be found at https://www.zotero.org/groups/1738107/psych_490_articles/items. The URL contains the group ID 1738107.
# To access the group via the Zotero API you need an account and an API key. API keys can be created and accessed
# at https://www.zotero.org/settings/keys when you are logged into your user account.
#
# As of the current development version fetch_zotero_refs() has not been exported (to be honest, I don't know why).
# Hence, the papaja:::-prefix is required.
papaja:::fetch_zotero_refs(
x = "1738107"
@crsh
crsh / legal_statute.Rmd
Created July 19, 2017 06:21
Adding a legal statute reference in the YAML header
@crsh
crsh / minimizeError.R
Last active June 13, 2017 15:12
Function to minimize estimate error of Bayes factors in BayesFactorBF objects
minimizeError <- function(x, max_error, multicore = TRUE) {
while(any(x@bayesFactor$error > max_error)) {
message("Current error: ", x@bayesFactor$error)
x <- recompute(x, multicore = multicore)
}
x
}
@crsh
crsh / gglegend.R
Created April 30, 2017 13:52
Plot only legend of a ggplot
# Stolen from http://stackoverflow.com/a/12041779/914024
gglegend <- function(x){
tmp <- ggplot_gtable(ggplot_build(x))
leg <- which(sapply(tmp$grobs, function(y) y$name) == "guide-box")
tmp$grobs[[leg]]
}
@crsh
crsh / cohens_d_r.R
Last active May 12, 2020 15:17
Calculate Cohen's d_r and mean differences in measurement scale
mean_difference_r <- function(x, object) UseMethod("mean_difference_r", object)
mean_difference_r.lm <- function(x, object) {
x * sigma(object)
}
mean_difference_r.mlm <- function(x, object) {
x * sqrt(mean(sigma(object)^2))
}