Skip to content

Instantly share code, notes, and snippets.

View jimjam-slam's full-sized avatar

James Goldie jimjam-slam

View GitHub Profile
@jimjam-slam
jimjam-slam / himawari-wallpaper.ps1
Last active August 1, 2018 14:45
Download (nearly) the latest Himawari Day + Night satellite imagery, crop it around Australia/NZ (for a 3:2 screen) and set the Windows desktop wallpaper with it.
# powershell script to get latest himawari imagery as a wallpaper.
# either run manually or use with task scheduler (but not *too* often!) like:
# powershell.exe -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File C:\Users\rensa\himawari-wallpaper.ps1 -wallpaperdir C:\Users\rensa\Pictures\wallpapers
# create a himawari subfolder, change the windows wallpaper to slideshow and point it to this subfolder
# remember to respect the bom's anon ftp policy! http://www.bom.gov.au/catalogue/anon-ftp.shtml
param (
[String]$wallpaperdir = "C:\Users\Public\Pictures\wallpapers"
)
@jimjam-slam
jimjam-slam / conditional-filter.md
Last active April 7, 2022 13:40
Use purrr::when to conditionally operate in a pipe (for example, to only filter if an argument is given a defined value) #rstatstips
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.6.1

conditional_filter <- function(l_disp = NULL, u_disp = NULL, l_hp = NULL, u_hp = NULL) {
  mtcars %>%
    # tidy up mtcars
    rownames_to_column() %>%
    as_tibble() %>%
    select(rowname, disp, hp) %>%
@jimjam-slam
jimjam-slam / parse_time_dimensions.r
Last active February 13, 2022 22:56
Parse a vector of numeric times, given element-wise CF-complaint time dimension units (eg. "days since 1901-01-01") #rstatstips
# https://carbon.now.sh/iYHN1JgadkF76T9rXC7q
# parse_time_dimensions: given a numeric vector of dates or datetimes, and
# a character vector of the form "[units] since [epoch]", return a POSIXct
# vector of date-times that are parsed element-wise. useful if you have,
# say, a list of netcdfs with varied epochs and units.
# example:
# > test %>% mutate(target = parse_time_dimensions(n, t))
# # A tibble: 3 x 3
# n t target
# <int> <chr> <dttm>
@jimjam-slam
jimjam-slam / fetch-error-body.js
Last active May 21, 2020 02:35
Generic fetch handler that throws non-200 responses as objects that include status code, status text and body
// handleJSONRequest: a generic function for ensuring that non-ok (200)
// responses get thrown as errors. use this after fetches and catch with a
// StatusBanner
export function handleJSONRequest(res) {
if (!res.ok) {
return res.text().then(body => {
throw {
status: res.status,
statusText: res.statusText,
message: body
@jimjam-slam
jimjam-slam / side-by-side-ggplot2-title.r
Last active February 13, 2022 22:55
Use duplicate axes (and, here, ggtext for title wrapping) to have a ggplot2 title/subtitle to the side of the plot #rstatstips
library(ggplot2)
library(ggtext)
ggplot(mtcars) +
aes(x = mpg, y = hp) +
geom_point() +
labs(
title = 'This plot has a delightfully long title',
subtitle = 'And a very important message that you should definitely take to heart') +
scale_x_continuous(sec.axis = dup_axis())
@jimjam-slam
jimjam-slam / ggplot2-gradient-tests.r
Created May 26, 2021 05:44
Ggplot2 gradient tests
library(tidyverse)
my_gradient <- grid::linearGradient(
colours = c("#020024", "#102b81", "#833ab4"),
stops = c(0, 0.4, 1))
# works!
myplot <-
ggplot(mtcars) +
@jimjam-slam
jimjam-slam / migrate.r
Last active June 10, 2021 02:01
Google Drive folder migration
# this script recursively transfers the contents of a folder to another
# (including a team/shared drive)
# configure it with the ids of your source and target folders below!
# james goldie, june 2021
library(tidyverse)
library(googledrive)
library(collateral)
library(tictoc)
@jimjam-slam
jimjam-slam / download-groups.r
Last active February 13, 2022 22:55
Download grouped remote files using friendlier group names #rstatstips
library(tidyverse)
# based on {searchable} and https://stackoverflow.com/a/64931927/3246758
# stats::setNames is fine too!
invert <- function(x) {
set_names(names(x), x)
}
# let's map the coded names to our friendly ones!
fruit <- c(
@jimjam-slam
jimjam-slam / dockerfile
Last active February 13, 2022 22:55
Sample command line app using rocker #rstatstips
# start with the rocker base
FROM rocker/r-ver:latest
# using littler, install some extra packages
RUN install2.r jsonlite
# add our script
COPY myprogram.r /myprogram.r
# myprogram.r is the entrypoint. when you run the image, ENTRYPOINT + CMD run by default...
@jimjam-slam
jimjam-slam / r-vscode-keybinds.json
Created February 1, 2022 03:56
Keybindings for writing R in VSCode
[
// open r help
{
"key": "shift+cmd+h",
"command": "r.helpPanel.openForSelection"
},
// insert a magrittr pipe %>% in editor or terminal
{
"key": "shift+cmd+m",
"command": "editor.action.insertSnippet",