Skip to content

Instantly share code, notes, and snippets.

View HenrikBengtsson's full-sized avatar

Henrik Bengtsson HenrikBengtsson

View GitHub Profile
check_news <- function(pathname = c("NEWS", "NEWS.md")) {
keep <- utils::file_test("-f", pathname)
if (!any(keep)) {
stop("No such file: ", paste(sQuote(pathname), collapse = ", "))
}
pathname <- pathname[keep]
pathname <- pathname[1]
if (basename(pathname) == "NEWS") {
news <- tools:::.news_reader_default(pathname)
@HenrikBengtsson
HenrikBengtsson / cran_submission_limits.R
Last active December 15, 2021 04:16
Figuring out how long too wait to avoid the limit of maximum 7 CRAN releases last 180 days
cran_days_since_release <- function(pkg, now = Sys.time()) {
db <- rbind(tools:::CRAN_archive_db()[[pkg]], tools:::CRAN_current_db()[pkg,])
now - sort(db$mtime, decreasing = TRUE)
}
# The CRAN rule is maximum 7 existing releases within the last 180 days
# https://github.com/wch/r-source/blob/tags/R-4-1-2/src/library/tools/R/QC.R#L7990-L8007
cran_wait_needed <- function(pkg, max = 7L, limit = 180, now = Sys.time()) {
since <- cran_days_since_release(pkg, now = now)
too_new <- since[since <= limit]
@HenrikBengtsson
HenrikBengtsson / condition-color-annotation.R
Last active November 29, 2021 21:23
An attempt to automatically color annotate messages, warnings, and errors in R
if (getRversion() >= "4.0.0" && requireNamespace("crayon", quietly = TRUE) &&
utils::packageVersion("startup") >= "0.16.0-9001") {
startup::on_session_start({
globalCallingHandlers(
message = function(c) {
if (inherits(c, "tweaked_condition")) return()
c$message <- cli::col_blue(conditionMessage(c))
class(c) <- c("tweaked_condition", class(c))
message(c)
invokeRestart("muffleMessage")
read_nyt_vote_table <- function(state, force = FALSE) {
library(rvest) ## CRAN package 'rvest'
states <- c(AZ = "arizona", GA = "georgia", NC = "north-carolina", NV = "nevada", PA = "pennsylvania")
stopifnot(state %in% names(states))
file <- sprintf("results-%s-president.html", states[state])
url <- file.path("https://www.nytimes.com/interactive/2020/11/03/us/elections", file)
if (force || !file_test("-f", file)) {
message("Downloading: ", file)
download.file(url, destfile = file, quiet = TRUE)
}
@HenrikBengtsson
HenrikBengtsson / us2020.R
Created November 6, 2020 01:55
US Election 2020 Swing States
library(rvest) ## CRAN package 'rvest'
read_nyt_vote_table <- function(state, force = FALSE) {
states <- c(AZ = "arizona", GA = "georgia", NC = "north-carolina", NV = "nevada", PA = "pennsylvania")
stopifnot(state %in% names(states))
file <- sprintf("results-%s-president.html", states[state])
url <- file.path("https://www.nytimes.com/interactive/2020/11/03/us/elections", file)
if (force || !file_test("-f", file)) {
message("Downloading: ", file)
download.file(url, destfile = file, quiet = TRUE)
@HenrikBengtsson
HenrikBengtsson / scan_examples.R
Last active October 23, 2020 20:32
Detect sprintf() argument mistakes in R (>= 4.1.0)
#' Condition Handler Detecting Sprintf Mistakes
#'
#' @param w A condition
#'
#' @param action Should the sprintf mistake be escalated to an error,
#' or should it display the warning and prompt the user?
#'
#' @examples
#' globalCallingHandlers(warning = handle_sprintf_warning)
#'
@HenrikBengtsson
HenrikBengtsson / print.function.R
Created October 22, 2020 04:52
Prints a function with a roxygen-style header comments
name_of <- function(obj, private = FALSE) {
env <- environment(obj)
if (private) {
env_name <- environmentName(env)
names <- tryCatch({
ns <- asNamespace(env_name)
names(.getNamespaceInfo(ns, "exports"))
}, error = function() names(env))
} else {
@HenrikBengtsson
HenrikBengtsson / future.svg
Created January 14, 2020 17:34 — forked from dlksmc/future.svg
Hex sticker for the future R package
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(ggplot2)
dd <- data.frame(x = rnorm(5e5), y = rnorm(5e5))
gg <- ggplot(dd, aes(x,y)) + geom_point()
## Render directively
t0 <- system.time(print(gg))
print(t0)
# user system elapsed
# 7.013 0.046 7.094
@HenrikBengtsson
HenrikBengtsson / README.md
Last active December 19, 2017 21:11
Shell/batch scripts calling R

Example of using myscript

Below is an example showing how the exact same call can be done in a Unix shell as in a Windows shell. All that is required is that the myscript.R is in the same folder as myscript and myscript.bat. This makes it easier to document and easier to give examples.

Note, for it to work on Unix without having to specify ./myscript when calling it from the same directory, the current diectory has be part of the PATH, e.g. export PATH=".:$PATH". If myscript is placed in a folder that is already in the PATH, the below will work out of the box.