Skip to content

Instantly share code, notes, and snippets.

@r2evans
r2evans / coord_cartesian_panels.R
Last active Sep 10, 2020
per-facet panel range clipping
View coord_cartesian_panels.R
#' Cartesian coordinates per facet-panel
#'
#' This function mimics the behavior of [ggplot2::coord_cartesian()],
#' while supporting per-panel limits when faceted.
#'
#' @details
#'
#' A 'panel_limits' data frame may contain:
#'
#' - zero or more faceting variables, all of which must be found
@r2evans
r2evans / DBI RODBC comparison.R
Created Aug 15, 2020
Comparison of DBI/odbc, DBI/RODBCDBI, and RODBC db access for R
View DBI RODBC comparison.R
## ------------------------------------------------------------------
## SETUP
# docker run -d -p "31433:1433" --name testss \
# -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Mysecretpassword1" \
# -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE \
# microsoft/mssql-server-linux
# ... and add 'testss' as a DSN in the system- or user- odbc configuration
View multiplex-clipboard.md

Clipboard selection over remote (ssh) and optionally multi-plexed (tmux) connections. This is primarily from a windows machine using git-for-windows, mintty, and tmux remotely. Untested otherwise.

Other (non-Windows) solutions/recommendations include xclip on the remote end; these will not work unless there is an X-windows server running on the local (Windows) machine.

The remainder is effectively the results of my trial-and-error process.

mintty and OSC-52

Instead, this uses OSC-52 escape sequences. Because they can be considered a security risk (the remote end might be able to see the local end's clipboard values arbitrarily), the capability is disabled by default.

@r2evans
r2evans / tryCatchPatterns.R
Last active Aug 15, 2019
exception handling by pattern (or sub-class)
View tryCatchPatterns.R
#' Pattern-matching tryCatch
#'
#' Catch only specific types of errors at the appropriate level.
#' Supports nested use, where errors not matched by inner calls will
#' be passed to outer calls that may (or may not) catch them
#' separately.
#'
#' @details
#'
#' Exception handling with a little finer control over *where* a
@r2evans
r2evans / MapReduce.R
Last active Jun 15, 2019
Combination of R's Reduce function with Map's k-ary arguments
View MapReduce.R
#' 'Reduce' with arbitrary number of arguments
#'
#' Applies a function to the corresponding elements of given vectors,
#' in a reductionist way. (This is *not* related to the [Apache Hadoop
#' MapReduce](https://hadoop.apache.org/) project ... while this may
#' suggest the name 'MapReduce' is a poor choice for this function, it
#' is a logical combination of R's [Map()] and [Reduce()] functions.)
#'
#' @details
#'
View divide_repo_subdirs.sh
#!/bin/bash
# changing a single repo with subdirs like this:
#
# origrepo
# +-- subdir1
# +-- subdir2
#
# into a different/new repo with rearranged dirs (and discarded files),
# editing the full history
View wrap_frame.R
#' Wrap a frame across multiple columns
#'
#' https://stackoverflow.com/a/52669757/3358272
#' @param x `data.frame`
#' @param nr,nc `integer`; specify only one of these, the number of
#' rows or columns to be fixed
#' @param rownames if `NULL` (default), row names (if found) are
#' discarded; if `character`, then a column is added (on the left of
#' `x`) with this as its title
#' @param byrow `logical`; if `FALSE`, the first column will match the
@r2evans
r2evans / pipe-funcs.R
Last active Jul 30, 2018
pipe functions for mid-pipe messages or debugging (under-tested! not for production!)
View pipe-funcs.R
#' Mid-pipe assertions
#'
#' Test assertions mid-pipe. Each assertion is executed individually
#' on each group (if present) of the piped data. Any failures indicate
#' the group that caused the fail, terminating on the first failure.
#'
#' If `.debug`, then the interpreter enters the `browser()`, allowing
#' you to look at the specific data, stored as `x` (just the grouped
#' data if `is.grouped_df(.x)`, all data otherwise). If the data is
#' changed, then the altered data will be sent forward in the pipeline
View NDC_cache.R
#' NDC caching for cross-facet plot elements
#'
#' Useful when using [layout()] and drawing things *between* different
#' plots. Motivated by https://stackoverflow.com/q/49464991/3358272.
#'
#' @details
#'
#' When adding plot "things" (lines, points, arrows) between facets of
#' a `layout` combination, in addition to `x` and `y`, you simply
#' provide the plot number within the sequence. (Since this is the
View fake-tribble.R
#' Programmatically produce row-independent `tribble`s
#'
#' @param x data.frame
#' @param max_width integer, the widest normalized width; if data or a
#' header is longer than this, it will not be truncated, but it will
#' also not be aligned with the others; can be single number (for
#' all) or a vector (for each column)
#' @param collapse character, the separation text, defaults to `", "`
#' @param compact logical, "no undue spaces"; if `TRUE` then sets
#' `max_width=0`, `collapse=","`, and disallows prepending