Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
@nassimhaddad
nassimhaddad / dplyr-backends.md
Last active August 4, 2016 14:01 — forked from piccolbo/dplyr-backends.md
Dplyr backends: the ultimate collection

Dplyr is a well known R package to work on structured data, either in memory or in DB and, more recently, in cluster. The in memory implementations have in general capabilities that are not found in the others, so the notion of backend is used with a bit of a poetic license. Even the different DB and cluster backends differ in subtle ways. But it sure is better than writing SQL directly! Here I provide a list of backends with links to the packages that implement them when necessary. I've done my best to provide links to active projects, but I am not endorsing any of them. Do your own testing. Enjoy and please contribute any corrections or additions, in the comments.

Backend Package
data.frame builtin
data.table builtin
arrays builtin
SQLite builtin
PostgreSQL/Redshift builtin
@nassimhaddad
nassimhaddad / openxlsx.R
Created July 13, 2016 11:35
getting openxlsx to work on linux
library(openxlsx)
Sys.setenv(R_ZIPCMD= "/usr/bin/zip")
openxlsx::write.xlsx(res, "mytable.xlsx", row.names = FALSE)
@nassimhaddad
nassimhaddad / mutate_paste.R
Created July 7, 2016 09:08
non standard evaluation with dplyr::mutate_ line by line #dplyr
cols_to_paste <- c("col1", "col2")
concatenation <- paste0("paste(", paste(cols_to_paste, collapse = ","), ")")
text_table <- the_data %>% mutate_(verbatim = concatenation)
@nassimhaddad
nassimhaddad / nonNA.R
Created May 18, 2016 09:23
function to extract non NA elements. vectorized.
nonNa <- function(x)Filter(Negate(is.na), x)
#' Turn a function into a new function that helps debugging upon exception
#'
#' @param fn the function to transform
#' @param saveFile an optional path to save RDS to, if NULL output will be in global variable '.problem'
#' @return new function that behaves like fn(...) normally, but if fn(...) throws an exception saves to variable or saveFile RDS of list .problem such that do.call(.problem$fn_name,.problem$args) repeats the call to fn with args.
#'
#' @examples
#' sum_of_log <- function(x, y){
#' stopifnot(x>=0)
@nassimhaddad
nassimhaddad / handsontable_heatmap.R
Created November 25, 2015 12:06
coloring cells using rhandsontable
DF = data.frame(
title = 1:4,
dummy = rep(NA, 4),
desc = c("1", "2", "3",
'<span style="width:100px;height:100px;background:#354B96;display:inline-block;color:white;">5</span>'),
stringsAsFactors = FALSE
)
library(rhandsontable)
@nassimhaddad
nassimhaddad / get_call.R
Created June 23, 2015 15:23
Here's how to capture the call to a function - from within that function. And to get it into a string using deparse().
# source: http://r.789695.n4.nabble.com/get-arguments-passed-to-function-inside-a-function-td3783894.html
tmpfun2 <- function(x,y,z,...) {
( match.call() )
}
temp <- (tmpfun2(1,2,3))
temp <- deparse(temp)
class(temp)
@nassimhaddad
nassimhaddad / server.R
Last active March 19, 2020 18:08
automatic resize of rChart objects in shiny
require(rCharts)
require(shiny)
smpl<-data.frame(gene = c("gene1","gene2","gene3","gene4","gene5",
"gene6","gene7","gene8","gene9","gene10"),
direction = c("up","down","up","up","up",
"up","up","down","down","down"),
type = c("norm","norm","tum","tum","norm",
"tum","tum","norm","tum","tum"),
foldChange = c(1.3, 0.4, 1.3, 3.0, 1.6,
2.9, 1.3, 0.5, 0.5, 0.6))
@nassimhaddad
nassimhaddad / glove.R
Last active August 29, 2015 14:12
Minimal example of how to load and use word vectors trained with nlp.stanford's GloVe (http://nlp.stanford.edu/projects/glove/), a text mining methodology similar to word2vec.
# download the trained word vectors (~100mb)
download_to <- tempfile()
download.file('http://www-nlp.stanford.edu/data/glove.6B.50d.txt.gz',
download_to)
# prepare the data
data <- read.table(download_to, sep = " ", header = FALSE,
quote = NULL, comment.char = "", row.names = 1,
nrows = -1)
data <- as.matrix(data)
@nassimhaddad
nassimhaddad / build.bash
Created November 25, 2014 08:54
Scripts that build and checks packages (source: https://github.com/jwijffels/ETLUtils)
#!/bin/bash
echo "Removing building information..."
rm -rf output
echo "Generate documentation..."
R -q -f roxygen.R
mkdir output