Skip to content

Instantly share code, notes, and snippets.

View jimhester's full-sized avatar

Jim Hester jimhester

View GitHub Profile
@jimhester
jimhester / gun-violence.R
Last active November 15, 2019 00:51
Gun Violence benchmark
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 October 22, 2019 21:08
places we use `$message` in r-lib packages
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 August 30, 2019 13:49
Get the previous commands as a text string in RStudio
# 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 August 29, 2019 20:30
GitHub vs GitLab for most recent 1000 R package releases
# 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))
}
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 December 13, 2018 21:12
Programmatically make a fix, fork and open a PR
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")
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/
@jimhester
jimhester / 12-startup-jims.R
Created October 5, 2018 16:34
my solution
# 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,

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.
#' Which libraries does R search for packages?
# try .libPaths(), .Library
#' Installed packages
## use installed.packages() to get all installed packages
## if you like working with data frame or tibble, make it so right away!
## remember to use View() or similar to inspect