Skip to content

Instantly share code, notes, and snippets.

View stufield's full-sized avatar
:octocat:

Stu Field stufield

:octocat:
View GitHub Profile
@stufield
stufield / functional_pipe_assignment.R
Created January 23, 2019 14:30
Functional Assignment Trick
# Check this out; NSE functional assignment
# Works for functions that take a single `x` argument
# and linear pipe only downstream
myfun <- . %>%
sqrt() %>%
as.character() %>%
paste("is the coolest number ever!")
myfun(9)
@stufield
stufield / fira_symbol_bindings.R
Created January 23, 2019 14:33
Cool FiraCode symbol bindings
# Many RStudio engineers were using this font
# https://github.com/tonsky/FiraCode
a >= 10
a != b
a <- 4
a == 10
a <= 10
@stufield
stufield / debugger_trick.R
Created January 23, 2019 14:56
Add to `.Rprofile` to enter/exit debugging mode with `rlang::entrace()`
# Set up custom debugging environment
.customCommands <- new.env()
assign("bug", structure("", class = "debugger_class"), envir = .customCommands)
assign("print.debugger_class", function(debugger) {
if ( !identical(as.character(getOption("error")), "rlang::entrace") ) {
options(error = quote(rlang::entrace()),
rlang__backtrace_on_error = "full") # or "collapse"
message(
crayon::green(
cli::symbol$tick,
@stufield
stufield / rename_list.R
Created January 25, 2019 20:31
dplyr::rename() works for data frames only, this works for list renaming
foo <- list(a = 1:5, b = letters, c = data.frame(set = 1:4))
key <- c(b = "super", a = "awesome", c = "wicked")
nm <- . %>% purrr::map_chr(~ key[[.x]])
foo %>% purrr::set_names(nm)
@stufield
stufield / .Rprofile
Last active August 27, 2019 18:53
My .Rprofile for now ...
# General use suggested .Rprofile for env setup
# Include additional functionality as required
# 2019-01-24
# Stu Field, Bioinformatics, SomaLogic, Inc.
# --------------------------------------------------- #
# ------------------------- #
## session options
# ------------------------- #
options(
@stufield
stufield / pre-commit
Last active July 4, 2020 20:40
The `somaverse` pre-commit hook
#!/bin/sh
echo
echo "Thank you for contributing to the ..."
echo " ___ ___ _____ __ ___ _____ _______ ___ "
echo " (_-</ o \\/ \\ /o \\\\ |/ / -_) __(_-</ -_)"
echo "/___/\\___/__Y_Y__\\/_/\\_\\\\__/\\__/_/ /___/\\__/ "
# Read user input, assign stdin to keyboard
@stufield
stufield / feather_vs_base.R
Created January 26, 2019 04:31
Quick comparison of file seriation via feather vs base functionality
reprex::reprex({
library(magrittr)
library(SomaObjects)
library(tibble)
library(feather)
t_feather <- purrr::map_dbl(1:10, ~ {
t <- Sys.time()
write_feather(SurvData, "/tmp/surv.feather")
as.numeric(Sys.time() - t)
}) %>% mean()
@stufield
stufield / nest_grouped_df.R
Last active January 31, 2019 06:20
Possible nest() bug when acting on grouped_df object and passing nesting columns
reprex::reprex({
library(magrittr)
library(tibble)
suppressMessages(library(tidyr))
suppressMessages(library(dplyr))
# Option 1
# Proper nesting of cyl using group_by
df1 <- mtcars %>%
group_by(cyl) %>%
@stufield
stufield / nest_grouped.md
Last active May 1, 2019 08:01
But in tidyr::nest() where nested variables are dropped if acting on a 'grouped' df
library(magrittr)
library(tibble)
library(tidyr)
#> 
#> Attaching package: 'tidyr'
#> The following object is masked from 'package:magrittr':
#> 
#>     extract
library(dplyr)
@stufield
stufield / reprex_ssDAMN.md
Last active May 20, 2019 21:49
Rcpp of ssDAMN

Reprex output for Rcpp Single Sample DAMN Normalization


See the ssDAMN.cpp file is attached below

Note: you cannot reproduce entirely; SomaNormalization is private

The reprex output


library(SomaNormalization)