Skip to content

Instantly share code, notes, and snippets.

View lorenzwalthert's full-sized avatar

Lorenz Walthert lorenzwalthert

View GitHub Profile
@lorenzwalthert
lorenzwalthert / numbers2words.R
Created August 27, 2016 05:58 — forked from psychemedia/numbers2words.R
R function to convert numbers to words
#https://github.com/ateucher/useful_code/blob/master/R/numbers2words.r
numbers2words <- function(x){
## Function by John Fox found here:
## http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html
## Tweaks by AJH to add commas and "and"
helper <- function(x){
digits <- rev(strsplit(as.character(x), "")[[1]])
nDigits <- length(digits)
if (nDigits == 1) as.vector(ones[digits])
@lorenzwalthert
lorenzwalthert / abstract
Last active February 18, 2017 19:35
a concept of code structuring
# ________________________________________________________
# A title ####
# this is a level 1 section. l1 sections are are high-level
# blocks.
# [your code here]
## ........................................................
## A sub title ####
# this is a level 2 section. These sections are medium-level
@lorenzwalthert
lorenzwalthert / anchored_section
Created February 18, 2017 19:36
An anchored section
## ........ #< bdd0351ff9987e58fec1e1d3dbcdd8d1 ># ........
## An anchored section ####
#< 56f5139874167f4f5635b42c37fd6594 >#
this_is_a_super_important_but_hard_to_describe_line_so_let_me_anchor_it
@lorenzwalthert
lorenzwalthert / summary
Last active February 18, 2017 20:09
summary created with sum_str
Summarized structure of ~/Documents/short-term back-up/sum_str.R
line level section
1 # ________________________________________________________
2 # A title
8 ## ........................................................
9 ## A subtitle
15 ### . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
16 ### Another one
devtools::install_github("lorenzwalthert/strcode")
# ____________________________________________________________________________
# function test ####
test <- function(x) {
## ............................................................................
## A: pre-processing ####
### .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
### a: assertive tests ####
# x
if(missing(x) || is.null(x)){
x <- character()
@lorenzwalthert
lorenzwalthert / lm_with_flexible_variables
Last active July 19, 2017 14:30
access the formula interface
``` r
# with a formula
fit_something <- function(data, formula, ...) {
lm(as.formula(formula), data = data, ...)
}
fit_something(mtcars, "cyl ~mpg")
#>
#> Call:
#> lm(formula = as.formula(formula), data = data)
``` r
string_formula <- paste0(names(mtcars)[1], " ~", paste0(names(mtcars)[-1], collapse = "+"))
lm(as.formula(string_formula), data = mtcars)
#>
#> Call:
#> lm(formula = as.formula(string_formula), data = mtcars)
#>
#> Coefficients:
#> (Intercept) cyl disp hp drat
library(styler)
guide <- tidyverse_style(scope = "line_breaks", indent_by = 8)
guide_with_indention_rules_only <- purrr::modify_at(
guide, 3, ~.x[grepl("indent", names(.x))]
)
styler::style_text(
"call(
x = 2
)
a =3