Skip to content

Instantly share code, notes, and snippets.

Jim Hester jimhester

Block or report user

Report or block jimhester

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jimhester
jimhester / dates.R
Last active Jan 2, 2020
Parsing dates with readr
View dates.R
library(readr)
the_dates <- c("2020-02-04", "04 February 2020", "2/4/20" , "4/2/20" , "04 Février 2020")
the_formats <- c("%Y-%m-%d", "%d %B %Y", "%m/%d/%y", "%d/%m/%y", "%d %B %Y")
the_locales <- list(locale("en"), locale("en"), locale("en"), locale("en"), locale("fr"))
purrr::pmap(
list(the_dates, the_formats, the_locales),
function(date, format, locale) {
parse_date(date, format = format, locale = locale)
}
@jimhester
jimhester / gun-violence.R
Last active Nov 15, 2019
Gun Violence benchmark
View gun-violence.R
library(dtplyr)
library(readr)
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(data.table))
# It is generally better not to benchmark the print methods to avoid misleading
# results, also vroom is faster on this particular dataset than
# either readr or data.table
bench::mark(
@jimhester
jimhester / gist:9ba97fb3a090c7c182db6effbfceca50
Created Oct 22, 2019
places we use `$message` in r-lib packages
View gist:9ba97fb3a090c7c182db6effbfceca50
callr/README.md: #> $message
installgithub.app/install-github.R: msg$message
installgithub.app/install-github.R: if (methods::is(response, "error") || !is.null(response$message)) {
remotes/install-github.R: error_details <- json$parse(rawToChar(res$content))$message
remotes/install-github.R: response$message)
remotes/install-github.R: if (methods::is(response, "error") || !is.null(response$message)) {
remotes/install-github.R: response$message)
gh/R/gh_response.R: paste0("Message: ", res$message)
callr/R/utils.R: exp <- tryCatch(parse(text = "1+"), error = function(e) e$message)
callr/R/result.R: err[[2]]$message <- err[[2]]$message %||% "interrupt"
@jimhester
jimhester / previous_commands.R
Created Aug 30, 2019
Get the previous commands as a text string in RStudio
View previous_commands.R
# RStudio overrides `history()`, `loadhistory()` and `savehistory()`,
# so they work somewhat differently than on the console version of R.
# This saves the current history to a temporary file then reads the last
# line of it (or more depending on the `n` argument)
#
# It does not handle multi-line commands, which would be more tricky to handle...
previous_commands <- function(n = 1) {
tf <- tempfile()
on.exit(unlink(tf))
savehistory(tf)
@jimhester
jimhester / github_vs_gitlab.R
Created Aug 29, 2019
GitHub vs GitLab for most recent 1000 R package releases
View github_vs_gitlab.R
# Measure usage of GitHub vs GitLab in R packages. This likely undercounts both GitHub and GitLab,
# becuase not every package puts a link to the development repository in their URL or BugReports fields.
# remotes::install_github("r-hub/crandb")
last_1000 <- crandb::events(archivals = FALSE, limit = 1000)
has_url <- function(x, url) {
any(grepl(url, c(tolower(x$package$URL), tolower(x$package$BugReports)), fixed = TRUE))
}
View 0001-Avoid-using-eval-parse.patch
From c2808f4d0f20682d9185d0e68bd33fe98d2dc7c2 Mon Sep 17 00:00:00 2001
From: Jim Hester <james.f.hester@gmail.com>
Date: Mon, 22 Apr 2019 07:43:48 -0400
Subject: [PATCH] Avoid using eval-parse
---
R/factory.R | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/R/factory.R b/R/factory.R
@jimhester
jimhester / fix-fork-and-pr.R
Created Dec 13, 2018
Programmatically make a fix, fork and open a PR
View fix-fork-and-pr.R
library(gh)
library(tidyverse)
library(processx)
me <- "jimhester"
title <- "Remove default assets"
branch_name <- "remove-default_assets"
search <- gh("GET /search/code", q = "default_assets user:tidyverse")
View steps.sh
cp /c/Users/Administrator/RStudio/rarrow/configure.win .
cp -r /c/Users/Administrator/RStudio/rarrow/tools .
cp /c/Users/Administrator/RStudio/rarrow/src/Makevars.win src/
# Change version in Makevars.win to 0.11.0
## this assumes arrow is installed via `make install`
mkdir -p windows/arrow-0.11.0/lib/x64
cp -r /c/Program\ Files\ \(x86\)/arrow/include windows/arrow-0.11.0/
cp /c/Program\ Files\ \(x86\)/arrow/lib/libarrow.a windows/arrow-0.11.0/lib/x64/
View 12-startup-jims.R
# Open your R profile
# Hint: use the usethis package
usethis::edit_r_profile()
# Change the default CRAN mirror
# Hint: The repos option takes a named list of repositories
# A good default repo is https://cran.rstudio.com
options(repos = c(CRAN = "https://cran.rstudio.com"))
# Change the default startup message to something using the cowsay package,
View README.md

my-packages

  • Have a look around the files here. Where are the R scripts? What are the directories data and figs for? Do the names and structure help you find things?
  • Open each R script, finish it, and run it. Remember to restart R as you go, so you are certain each file is complete, i.e. data flows through explicit write/read, not the global workspace.
    • R/01_write-installed-packages.R
    • R/02_wrangle-packages.R
    • R/03_barchart-packages-built.R
  • It's OK if you don't finish! We can keep working on this later.
  • If you finish quickly, write an R script to run the whole analysis and, perhaps, another script that does a make clean style reset.
You can’t perform that action at this time.