Skip to content

Instantly share code, notes, and snippets.

View nutterb's full-sized avatar

Benjamin nutterb

  • Battelle Memorial Institute
  • Kentucky
View GitHub Profile
@nutterb
nutterb / filerepo_example.R
Last active March 21, 2023 16:00 — forked from couthcommander/filerepo_example.R
fileRepository API functionality
library(redcapAPI)
key <- ''
rcon <- redcapConnection("https://redcap.vanderbilt.edu/api/", key, conn, 'MatchedRandomization1')
fr_ls <- function(rcon, folder_id = NA, recursive = FALSE) {
args <- list(content = 'fileRepository', action = 'list', format = 'csv')
if(!is.na(folder_id)) {
args$folder_id <- folder_id
parent <- folder_id
@nutterb
nutterb / missingSummary.R
Last active March 7, 2023 21:47
Generate a Report of Fields with Missing Values in a REDCap Database Using the redcapAPI package
#### Intended for use with the `redcapAPI` package
#' @name missingSummary
#' @aliases missingSummary.redcapApiConnection
#' @aliases missingSummary.redcapDbConneciton
#' @aliases missingSummary_offline
#' @export missingSummary
#' @export missingSummary.redcapApiConnection
#' @export missingSummary.redcapDbConnection
#' @export missingSummary_offline
#'
@nutterb
nutterb / Example Input
Created April 8, 2016 00:47
Shiny Real-time markdown rendering
# Copy and paste the code below into the textArea input when the application is running.
# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
It would be really helpful to include a link to a markdown cheat sheet. [This is a good example](http://support.mashery.com/docs/read/customizing_your_portal/Markdown_Cheat_Sheet) of where to get a good rundown of basic markdown.
@nutterb
nutterb / convert_base
Created March 14, 2018 18:04
Convert between number systems. Still incomplete
convert_to_decimal_single <- function(x, from = 2){
coll <- checkmate::makeAssertCollection()
checkmate::assert_integerish(x,
add = coll)
checkmate::assert_integerish(from,
len = 1,
lower = 1,
upper = 36
@nutterb
nutterb / total_atomic_mass.R
Created February 28, 2018 17:44
Calculate total atomic mass of a compound.
atomic_mass <- c( H = 1.008, He = 4.0026, Li = 6.94, Be = 9.0122,
B = 10.81, C = 12.011, N = 14.007, O = 15.999,
F = 18.998, Ne = 20.180, Na = 22.990, Mg = 24.305,
Al = 26.982, Si = 28.085, P = 30.974, S = 32.06,
Cl = 35.45, Ar = 39.948, K = 39.098, Ca = 40.078,
Sc = 44.956, Ti = 47.867, V = 50.942, Cr = 51.996,
Mn = 54.938, Fe = 55.845, Co = 58.933, Ni = 58.693,
Cu = 63.546, Zn = 65.38, Ga = 69.732, Ge = 72.630,
As = 74.922, Se = 78.971, Br = 79.904, Kr = 83.798,
Rb = 85.468, Sr = 87.62, Y = 88.906, Zr = 91.224,
risk_attack_succeed_sim <- function(attack, defend){
while(attack >= 2 && defend > 0){
n_attack <- min(c(3, attack - 1))
n_defend <- min(c(2, defend))
roll_attack <- sample(1:6, n_attack, replace = TRUE)
roll_defend <- sample(1:6, n_defend, replace = TRUE)
n_compare <- min(c(n_attack, n_defend))
quick_summary <- function(df, vars, group = NULL){
group_is_null <- is.null(group)
coll <- checkmate::makeAssertCollection()
checkmate::assert_data_frame(x = df,
add = coll)
checkmate::assert_character(x = vars,
@nutterb
nutterb / tidy.microbenchmark
Created September 12, 2017 19:33
tidy.microbenchmark
tidy.microbenchmark <- function(x, unit, ...){
summary(x, unit = unit)
}
@nutterb
nutterb / massert.R
Created July 12, 2017 11:32
Comparing code efficiency of multiple assertions in checkmate
# Suppose we have arguments for power and sample size of a two-sample t-test
# (Similar to power.t.test, but vectorized. See https://github.com/nutterb/StudyPlanning/blob/devel/R/test_t2.R
# delta: Difference of means. (-Inf, Inf)
# delta0: Difference under the null hypothesis. (-Inf, Inf)
# se: Standard error (0, Inf)
# alpha: significance level (0, 1)
# power: power of the test (0, 1)
### STANDARD ARGUMENT CHECKS ###
# 19 lines of code (259 characters)
@nutterb
nutterb / query_varchar_max
Created May 30, 2017 12:11
Execute queries to SQL Server that involve VARCHAR(MAX) variable types.
#' @name query_varchar_max
#' @title Query a VARCHAR(MAX) Variable from SQL Server
#'
#' @importFrom RODBCext sqlExecute
#'
#' @description The RODBC driver to SQL Server (SQL Server Native Client 11.0)
#' reports the lenght of a VARCHAR(MAX) variable to be zero. This presents
#' difficulties in extracting long text values from the database. Often, the
#' ODBC will assume a length of 255 characters and truncate the text to that
#' many characters. The approach taken here searches the VARCHAR(MAX) variables