Skip to content

Instantly share code, notes, and snippets.

View jmoggridge's full-sized avatar
🦭

J Moggridge jmoggridge

🦭
View GitHub Profile
@jmoggridge
jmoggridge / fmt_pval.R
Created August 23, 2024 03:57
`fmt_pval` formats p-values for display
fmt_pval <- function(pvalues) {
p <- format.pval(
pv = pvalues,
digits = 1,
eps = 0.001,
na.form = '',
nsmall = 3
)
@jmoggridge
jmoggridge / simple-form-useref.jsx
Created June 16, 2024 21:32
Simplest react form: useRef
// using a useRef hook to manage form state with uncontrolled text area component
import { useRef } from 'react';
export default function TextInputForm() {
const ref = useRef();
const text = 'input';
function submitForm() {
console.log(ref.current);
}
@jmoggridge
jmoggridge / ww_7d_endpt_avg.R
Created July 27, 2023 18:32
Waste-water 7 day endpoint averaging
# 7d endpoint average, ignoring missing values, equally weighted
endpoint_avg_7d <- function(x) {
RcppRoll::roll_mean(x, n = 7, align = 'right',
na.rm = T, fill = NA_real_)
}
@jmoggridge
jmoggridge / collect_wastewater.R
Created April 18, 2023 21:12
CUBE: Collect Ottawa regional waste-water surveillance results
collect_wastewater_data <- function(){
'https://raw.githubusercontent.com/Big-Life-Lab/PHESD/main/Wastewater/Ottawa/Data/wastewater_virus.csv' |>
read_csv(show_col_types = FALSE, guess_max = 1000) |>
janitor::clean_names() |>
janitor::remove_empty('cols')
}
@jmoggridge
jmoggridge / ottawa_ww.R
Created March 6, 2023 20:44
Ottawa Regional SARS-CoV-2 Waste Water Time-Series
# ottawa wastewater data: daily means
regional_ww <-
read_rds(here('data/ww_ottawa.rds')) |>
select(sample_date, starts_with('cov_')) |>
pivot_longer(contains('cov_')) |>
mutate(target = str_extract(name, 'cov_n.'),
stat = str_extract(name, 'mean|sd'),
) |>
select(-name) |>
pivot_wider(names_from = stat, values_from = value) |>
@jmoggridge
jmoggridge / cube_cq_to_copies.R
Created August 16, 2022 20:51
CUBE PCR Cq values to viral copies
convert_cq_to_copies <- function(cq) {
return(10 ** ((40.1 - cq) / 3.23))
}
@jmoggridge
jmoggridge / table1_transpose.R
Last active May 11, 2022 22:36
Transpose a summary table by pivoting longer, then wider, on the grouping variable
library(tidyverse)
iris |>
group_by(Species) |>
summarise(across(everything(), lst(median, min, max))) |>
pivot_longer(-Species) |>
pivot_wider(names_from = Species, values_from = value) |>
separate(name, sep = '_', into = c('var', 'stat'))
@jmoggridge
jmoggridge / javascript_dates.js
Created May 7, 2022 03:03
Get current date and parse yyyy mm dd
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
@jmoggridge
jmoggridge / override.aes.R
Created April 5, 2022 02:15
Override aes to change geom size in ggplot2 legend
my_plot +
guides(color = guide_legend(override.aes = list(size = 5)))
@jmoggridge
jmoggridge / abbrev_month_name_axis.R
Last active March 30, 2022 20:30
Make ggplot2 date axis labels show each month's abbreviated name
my_ggplot +
scale_x_date(
date_breaks = '1 month',
date_labels = "%b"
)