Skip to content

Instantly share code, notes, and snippets.

View cars.csv
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
@noamross
noamross / afile.txt
Created December 1, 2023 23:55
This is a text file
View afile.txt
This is a text file on the web
@noamross
noamross / afile.txt
Created December 1, 2023 23:55
This is a text file
View afile.txt
This is a text file on the web"
@noamross
noamross / put64.R
Last active November 17, 2023 22:22
Simple R object to base64 conversion. Free to a good home in your R package! If you put it in your package, let me know 🙂
View put64.R
# Copyright 2018 Noam Ross
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
@noamross
noamross / dput_binary.R
Last active November 17, 2023 03:22
dput() but with compressed binary serialized to 7-bit character text ¯\_(ツ)_/¯
View dput_binary.R
dput_binary <- function(object, compression = "xz") {
encoded <- strsplit(rawToChar(serialize(memCompress(serialize(object, NULL), type = compression), NULL, ascii = TRUE)), "\n")[[1]]
header <- gsub(" ", "", capture.output(dput(encoded[c(1:8)])))
body <- paste0("\"", paste(encoded[-c(1:8)], collapse = ""), "\"")
#The `gsub()` call here is to remove any whitespace or newlines introduced in
#printing and copy/paste
all <- paste0(
'unserialize(memDecompress(unserialize(textConnection(c(gsub("[\\\\s\\\\n]","",',
@noamross
noamross / tar_load_version.R
Last active November 3, 2023 18:55
Read previous versions of targets from git repository
View tar_load_version.R
# Requires a development branch of git2r for now. Do `remotes::install_github('ropensci/git2r@raw-blob-content')` or
# `renv::install('ropensci/git2r@raw-blob-content')`. This installs from source.
# TODO:
# - Add a function to extract an arbitrary file/folder, e.g., copy_git_file(path, ref, repo)
# - Have tar_*_version check if the target is a local file and in that case extract it and return a temporary path (turn on/off with arguments)
# - Guardrails and informative error messages cases such as: Not a git repository, target not present at the given reference, remote version not available. What happens with shallow clones?
# - Maybe only extract stuff in the `objects/` directory when needed
library(git2r)
@noamross
noamross / base_r_plotting_tutorial_koontz_d-rug.r
Created April 24, 2015 17:34
Back to basics: High quality plots using base R graphics
View base_r_plotting_tutorial_koontz_d-rug.r
### Title: Back to basics: High quality plots using base R graphics
### An interactive tutorial for the Davis R Users Group meeting on April 24, 2015
###
### Date created: 20150418
### Last updated: 20150423
###
### Author: Michael Koontz
### Email: mikoontz@gmail.com
### Twitter: @michaeljkoontz
###
@noamross
noamross / criticmarkup.lua
Last active August 30, 2023 12:08
A pandoc filter for MS Word track changes to criticmarkup
View criticmarkup.lua
-- a lua filter for panodoc
-- run pandoc your_word_doc.docx --track-change=all -t markdown --lua-filter=criticmarkup.lua
-- TODO: Detect substitutions in adjacent insertion/deletions
-- TODO: capture whole comment hightlight rather than just start point of comment
function Span(elem)
if elem.classes[1] and elem.classes[1] == "insertion" then
local opener = { pandoc.RawInline(FORMAT, "{++ ") }
local closer = { pandoc.RawInline(FORMAT, " ++}") }
return opener .. elem.content .. closer
elseif
@noamross
noamross / toggle-radio-layer-legends.R
Created August 13, 2021 20:22
A quick hack to have the legends associated with radio-button "Base Groups" in R leaflet maps toggle along with layers
View toggle-radio-layer-legends.R
library(leaflet)
library(tidyverse)
outline <- quakes[chull(quakes$long, quakes$lat),]
map <- leaflet(quakes) %>%
addTiles(group = "OSM (default)") %>%
# Overlay groups
addCircles(~long, ~lat, ~10^mag/5, stroke = F, group = "Quakes") %>%
addPolygons(data = outline, lng = ~long, lat = ~lat,
@noamross
noamross / fix.csv.R
Created November 19, 2012 21:49
Use R's native data editor to edit a CSV
View fix.csv.R
#' Use the data editor for a CSV file
#'
#' This function loads a CSV file, lets the user edit it in the native data
#' editor, then re-saves it, prompting the user for a new file name if desired.
#'
fix.csv <- function(file, new.name=TRUE, sep=",", comment.char="") {
tmpframe <- read.csv(file, sep=sep,quote="", colClasses="character",
stringsAsFactors=FALSE, comment.char="",
blank.lines.skip=FALSE, na.strings="")
tmpframe <- edit(tmpframe)