Skip to content

Instantly share code, notes, and snippets.

View wrathematics's full-sized avatar

Drew Schmidt wrathematics

View GitHub Profile
@wrathematics
wrathematics / gist:af9d84a670ff961a935e
Created February 6, 2015 23:39
Executing code before title of Rmd, generated as pdf (not working)
```{r, echo=FALSE, eval=TRUE}
title <- "the title"
```
---
title: `r title`
output: pdf_document
---
In RStudio, this will automatically produce an html file, regardless of what is chosen. The html file that is produced renders correctly.
@wrathematics
wrathematics / gist:4d6900e719afd07ba699
Created February 8, 2015 21:27
Useful C99 feature for passing by reference
#include <stdio.h>
void foo(const int *bar)
{
printf("bar=%d\n", *bar);
}
int main()
{
foo(&(int){27});
@wrathematics
wrathematics / gist:ab7dd9e5a3b139c84418
Created February 12, 2015 11:48
C vs Rcpp for monte carlo pi approximation
Rcpp_code <- "
#include <Rcpp.h>
// [[Rcpp::export]]
double mcsim_rcpp(const int n){
int i, r = 0;
double u, v;
Rcpp::RNGScope scope;
for (i=0; i<n; i++){
@wrathematics
wrathematics / gist:c1ea629b8f21efc01adc
Last active August 29, 2015 14:16
factor to numeric
### This is free and unencumbered software released into the public domain.
code <- '
#include <Rcpp.h>
#include <string>
// [[Rcpp::export]]
Rcpp::NumericVector CXX_factor_to_numeric(Rcpp::IntegerVector factor, std::vector<std::string> levels)
{
Rcpp::NumericVector ret(factor.size());
@wrathematics
wrathematics / gist:3e1d08a70f7b0eb57a88
Created March 10, 2015 17:37
Auto fold roxygen comments in vim
$ cat ~/.vim/after/ftplugin/vim.vim
setlocal foldmethod=syntax
$ cat ~/.vim/after/syntax/r.vim
setlocal foldmethod=syntax
setlocal foldenable
syn sync fromstart
set foldnestmax=1
syn match vimFoldRoxygen "\v(^#'.*\n)+" fold containedin=ALLBUT,@vimNoFold
autocmd FileType r exe "normal zM``"
timezone <- function()
{
time <- Sys.time()
ret <- list(timezone=format(time, format="%Z"), UTC.offset=format(time, format="%z"))
class(ret) <- "tzlookup"
return(ret)
}
### This is ludicrously expensive, but I couldn't quickly find a way to do it with methods:: functions.
is_s3_generic <- function(obj)
{
m <- tryCatch(methods(obj), warning=identity, error=identity)
if (inherits(m, "warning") || inherits(m, "error")) FALSE else TRUE
}
find_generics <- function(pkg)
{
@wrathematics
wrathematics / gist:ac621cd583c4b9e3b5eb
Created March 20, 2015 00:36
fibonacci series by goto in fortran 77
SUBROUTINE FIB(N)
INTEGER N,I,F0,F1,TMP
I=0
1060 IF(N.GT.0)THEN
GOTO6129
ELSE
GOTO7290
ENDIF
6129 WRITE(*,3502) 1
N=N-1
@wrathematics
wrathematics / gist:d963efbd76715229118b
Created April 30, 2015 18:26
Behold the clarity of functional programming
cat(sapply(99:1, function(i) paste(i, container <- ifelse(i>1, "bottles", "bottle"), "of beer on the wall,", i, container, "of beer. Take one down, pass it around,", ifelse(i > 1, paste(i-1, ifelse(i==2, sub("s", "", container), container), "of beer on the wall\n"), "No bottles of beer on the wall!\n"))))
@online{,
author = {Gabor Csardi},
title = {{metacran}},
year = 2015,
url = {https://github.com/metacran},
urldate = {2015-5-10}
}