Skip to content

Instantly share code, notes, and snippets.

View petermeissner's full-sized avatar
💻
githubbing

Peter Meissner petermeissner

💻
githubbing
View GitHub Profile
@petermeissner
petermeissner / gist:6dc6cf6de97b0c9a6dc7
Last active August 29, 2015 14:13
Rmd snippet that translates itself into Rpres
[...]
```{r, include=FALSE}
RMDtoRpres <- function(fname){
require(stringr)
text <- readLines(fname, encoding="UTF-8")
text <- paste(text, collapse="\n")
author <- str_replace_all(str_extract(text,'author: ".*?\n'),'["\n]',"")
date <- str_replace_all(str_extract(text,"date: '.*?\n"),'["\n]',"")
title <- str_replace(str_replace_all(str_extract(text,'title: ".*?\n'),'["\n]',""),"title: ","")
#!/path/2/Rscript
# License: CC0 (just be nice and point others to where you got this) Author:
# Robert M Flight <rflight79@gmail.com>, github.com/rmflight
#
# This is a post-commit hook that after a successful commit subsequently
# increments the package version in DESCRIPTION and commits that. Analogous to
# the pre-commit at https://gist.github.com/rmflight/8863882, but useful if you
# only have good reasons for not doing it on the pre-commit.
#
require(httr)
## Loading required package: httr
@petermeissner
petermeissner / to_char_bars.md
Last active August 29, 2015 14:16
How to get numbers transformed to character bar charts ... to_char_bars()
@petermeissner
petermeissner / browse_df_as_html.r
Last active August 29, 2015 14:18
An R function allowing to browse R data frames as HTML files
### Description ###
# author: peter meissner (https://github.com/petermeissner | http://pmeissner.com)
# purpose: Browsing data frames in R is annoying.
# The function provided here allows to browse data frames within the
# browser using HTML and some JavaScript.
#' function for making html table
#' @param x data.frame or matrix to be transformed
@petermeissner
petermeissner / modus.r
Created April 23, 2015 15:51
function for calculating the modus
#' function for getting mode source: http://stackoverflow.com/a/8189441/1144966
#' @param x vector for which the mode should be returned
modus <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
@petermeissner
petermeissner / package.r
Last active August 29, 2015 14:21
function that loads packages and installs them if need be
# function that loads packages
package <- function(pkg, repo){
pkg <- as.character(as.list(match.call())$pkg)
repo <- as.character(as.list(match.call())$repo)
if ( !(pkg %in% as.data.frame(installed.packages())$Package) ){
if( length(repo)!=0 ){
package("devtools")
devtools::install_github(paste0(repo,"/",pkg))
}else{
install.packages(pkg)
@petermeissner
petermeissner / helloworld.r
Last active August 29, 2015 14:21
Hello World
hello_world <- function() {
message(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"* * * * HELLO WORLD * * * *\n",
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
)
}
hello_world()
@petermeissner
petermeissner / purrr_awesome.R
Last active December 7, 2016 21:03
the most awesome purrr feature?
# capturing failure with default values
library(purrr)
# a faulty function
f <- function(x){
# some nasty error occuring once in a while
if( x %% 2 == 0 ){
@petermeissner
petermeissner / is_type_x.js
Created January 5, 2018 09:45
checking for specific type / class in Javascript
// - check type of input
is_type_x = function(object, type){
// go through cases: undefined, object, primitive
if( typeof(object) === "undefined" ){
// undefined cannot be matched
return false;
} else if( typeof(object) === "object" ){
// objects have to be matched against constructor
if( window[type] === undefined ){