Skip to content

Instantly share code, notes, and snippets.

View nassimhaddad's full-sized avatar

Nassim Haddad nassimhaddad

View GitHub Profile
@nassimhaddad
nassimhaddad / tryCatch.R
Created May 6, 2013 08:45
using trycatch
result = tryCatch({
    expr
}, warning = function(w) {
    warning-handler-code
}, error = function(e) {
    error-handler-code
}, finally {
    cleanup-code
}
@nassimhaddad
nassimhaddad / non-ascii.R
Created January 26, 2013 18:13
remove non-ascii characters
# remove non-ascii characters
df$text <- gsub("[^\x20-\x7E]", "", df$text)
@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 / qgraph_viz.R
Last active June 15, 2017 17:39
Good looking network or graph visualization, using qgraph. For network or node metrics, use igraph.
# notes: it's all about fine-tuning the details:
# good color palette (-> google 'color palette')
# white borders and font
# gray edges (even better when weighted)
#
library(qgraph)
adj=matrix(sample(0:1,10^2,TRUE,prob=c(0.8,0.2)),nrow=10,ncol=10)
groups <- list(group1 = 1:4,
group2 = 5:7,
group3 = 8:10)
#' 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 / 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 / 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 / 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)
cut_equal <- function(x, n = 3){
q <- quantile(x, prob= seq(0,1,length.out = n+1 ))
cut(x,
breaks = as.numeric(q),
include.lowest=TRUE, labels = 1:n)
}