Install package like this:

devtools::install_github("favstats/openmindR")
knitr::opts_chunk$set(message = F, 
                      warning = F)

Load package(s):

library(openmindR)
library(tidyverse)

OpenMind Cleaning Functions

library(DBI)
library(magrittr)
library(glue)
library(lubridate)

db_get_data <- function(tbl_dat) {
  con <- dbConnect(RSQLite::SQLite(), "../om_metrics_report/sql_data/omdata.db")

  out <- con %>%
    tbl(tbl_dat) %>%
    collect()

  dbDisconnect(con)

  return(out)
}


## Matching string for all Q variables
q_strings <- paste0(
  paste0("Q", 1:18, "P", collapse = "|"), "|", paste0("Q", 1:18, "F", collapse = "|")
  )
## Matching string for all (relevant) D variables
d_strings <- paste0("D", 1, collapse = "|")
## Matching string for all C variables
c_strings <- paste0("C", 1:3, collapse = "|")

## Matching string for all (relevant) D, Q and C variables
var_strings <- paste0(q_strings, "|", d_strings, "|", c_strings, collapse = "|")
## Matching string for all Q and C variables
q_c_strings <- paste0(q_strings, "|", c_strings, collapse = "|")
## Matching string for ranging vars from 0 to 1 
range01_strings <- str_c(str_c("Q", 3:12, "P", collapse = "|"), "|",
                         str_c("Q", 3:12, "F", collapse = "|"), 
                         str_c("|", c_strings, collapse = "|")
)


# Read in data
dat.acc <- db_get_data("dat.acc")
dat.par <- db_get_data("dat.par")
dat.ass4 <- db_get_data("dat.ass4")
dat.ass5 <- db_get_data("dat.ass5")

dat.ass <- dat.ass4 %>% 
  rename_at(vars(matches("Followup")), ~str_replace(., "Followup", "FollowUp")) %>%
  bind_rows(dat.ass5)

om_filter_data

Filter down Assessment data from AirTable by AssessmentsDone, AssessmentVersion and AccessCodes.

dat.ass %>% 
  # specify which number of assessment you want to have
  om_filter_data(n_assessments = 1:3,
             # assessment version?
             version = 4,
             # select Accesscode(s) to produce report for
             accesscode = "Wilkes"
             # "Wilkes" #try this out :)
  )

This dataset was filtered down to only AccessCodes that include "Wilkes". The accesscode argument is not case-sensitive and can both be used with vectors:

dat.ass %>% 
  # specify which number of assessment you want to have
  om_filter_data(n_assessments = 1:3,
             # assessment version?
             version = 4,
             # select Accesscode(s) to produce report for
             accesscode = c("SuszkoWilkesUF18", "KarimiWilkesUF18")
  )

And individual strings:

dat.ass %>% 
  # specify which number of assessment you want to have
  om_filter_data(n_assessments = 1:3,
             # assessment version?
             version = 4,
             # select Accesscode(s) to produce report for
             accesscode = c("suszko|karimi")
  )

om_clean_par

Cleans up ParticipantProgress data and creates several measures:

dat.par %>% 
  om_clean_par()

om_construct_measures

This is a higher-level function that uses both polar_measures and calc_ih to constuct various measures.

Creates the following variables:

Function automatically accounts for Assessment Version 4 and 5/5.1.

dat.ass %>% 
  om_construct_measures()

Error in polar_measures(., Q1Pre, Q2Pre) : Input data is missing column ppol_cat. Please make sure to run om_clean_ppol before you run om_construct_measures.

Uh oh! That didn't work! om_construct_measures needs the column ppol_cat to run which can be created with the function om_clean_ppol.

om_clean_ppol

Creates the following measures of Political Orientation

dat.ass <- dat.ass %>% 
  om_clean_ppol()

dat.ass

Now om_construct_measures will work!

dat.ass %>% 
  om_construct_measures()

remove_dups

This function is really important to clean up duplicated OMIDs that occasionally occur within AirTable.

dat.ass %>% 
  remove_dups()

OpenMind ggplot2 theme

There are three functions for the ggplot2 theme:

Make sure you have the Poppins font installed!

windowsFonts(`Poppins` = windowsFont("Poppins"))

Good tutorial on how to install custom fonts in R

Example

## Load tidyverse
library(tidyverse)

titanic_dat <- Titanic %>% as_tibble()

titanic_dat %>% 
  ggplot(aes(Sex, n)) +
  geom_col(aes(fill = Class), position = position_dodge()) +
  theme_om(legend_position = c(0.9, 0.75)) +
  scale_fill_om("Class") +
  facet_wrap(~Survived) +
  labs(title = "Titanic Survival by Age and Class") 

Adapt theme_om

titanic_dat %>% 
  ggplot(aes(Class, n, fill = Class)) +
  geom_col() +
  theme_om(legend_position = "bottom",
           axis_text_size = 10,
           axis_title_size = 15, 
           legend_text_size = 10,
           title_size = 20) +
  scale_fill_om() +
  facet_wrap(~Survived) +
  labs(title = "Titanic Survival by Class") 

Or all text sizes at once

titanic_dat %>% 
  ggplot(aes(Class, n, fill = Class)) +
  geom_col() +
  theme_om(legend_position = "top",
           overall_text_size = 15) +
  scale_fill_om() +
  facet_wrap(~Survived) +
  labs(title = "Titanic Survival by Class") 


favstats/openmindR documentation built on May 23, 2019, 8:03 p.m.