Skip to content

Instantly share code, notes, and snippets.

View malcolmbarrett's full-sized avatar

Malcolm Barrett malcolmbarrett

View GitHub Profile
@malcolmbarrett
malcolmbarrett / pdag.r
Created April 2, 2018 16:15
pDAG hack in ggdag
library(ggdag)
library(ggraph)
library(dplyr)
dag <- confounder_triangle(x_y_associated = TRUE) %>%
tidy_dagitty() %>%
# add marker for undirected
mutate(undirected = ifelse(name == "x" & to == "y", TRUE, FALSE))
# create a closure function to filter data
filter_undirected <- function(...) {
# install.packages(c("devtools", "tidyverse"))
# devtools::install_github("malcolmbarrett/ggdag")
library(ggdag)
library(tidyverse)
dag <- dagify(D ~ ABU1 + AEU2 + BEU3,
ABU1 ~ A + B + U1,
AEU2 ~ A + E + U2,
BEU3 ~ B + E + U3)
library(tidyverse)
mtcars %>%
select(starts_with("c")) %>%
summarise_all(mean)
mega_lm <- function(var) {
fmla <- as.formula(paste("mpg ~", var))
lm(fmla, data = mtcars) %>%
tidy()
@malcolmbarrett
malcolmbarrett / get_retweets.R
Last active January 27, 2019 17:07
A short script to sample from retweets and quote tweets
retweets <- rtweet::get_retweets("1088462271175708672", token = token)
quote_tweets <- rtweet::search_tweets(
"https://twitter.com/EpiEllie/status/1088462271175708672",
n = 100,
type = "recent",
include_rts = FALSE,
token = token
)
dplyr::bind_rows(retweets, quote_tweets) %>%
library(dplyr)
library(rtweet)
abi_likes <- rtweet::get_favorites("GoAbiAryan", token = your_token)
abi_likes %>%
dplyr::sample_n(10) %>%
dplyr::pull(text)
abi_likes %>%
dplyr::select(user_id, status_id, created_at, screen_name, text) %>%
library(tidyverse)
patient_db <- tibble(
patient = c("a", "b", "c"),
RecordAxisCondition1 = c("T400", "I560", "T401"),
RecordAxisCondition2 = c("T402", "N179", "R111"),
RecordAxisCondition3 = c("N180", "X46", "N179")
)
search_codes <- c("T400", "T401", "T402", "T403", "T404", "T406")

Prepare for release:

  • devtools::check()
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • rhub::check(platform = 'ubuntu-rchk')
  • rhub::check_with_sanitizers()
  • revdepcheck::revdep_check(num_workers = 4) (1 issue reported)
  • Polish NEWS
  • Polish pkgdown reference index
@malcolmbarrett
malcolmbarrett / move_slides_to_web.R
Last active April 2, 2019 10:28
Move R Markdown HTML slides to Blogdown and Push to Web
# install.packages(c("here", "fs", "stringr", "purrr", "git2r"))
# to add invisibly in your R profile, open with usethis::edit_r_profile()
# then define it in an environment, e.g.
# .env <- new.env()
# .env$move_slides_to_web <- {function definition}
move_slides_to_web <- function(folder = NULL, index = NULL) {
if (is.null(folder)) {
library(ggdag)
confounder_triangle() %>%
tidy_dagitty() %>%
dplyr::mutate(dashed = ifelse(to == "y", "dashed", "solid")) %>%
ggplot(aes(x, y, xend = xend, yend = yend)) +
geom_dag_point() +
geom_dag_text() +
geom_dag_edges_link(
aes(label = to, edge_linetype = dashed),
angle_calc = 'along',
library(ggplot2)
df <- data.frame(
y = c(42, 71, 76, 79),
ci_lower = c(40, 69, 74, 77),
ci_upper = c(44, 73, 78, 81),
year = c("baseline", "year 1", "year 2", "year 3")
)
ggplot(df, aes(x = year, y = y)) +
geom_col(fill = "#0072B2", width = .7) +