Skip to content

Instantly share code, notes, and snippets.

@datalove
datalove / devtols_install_github_behind_proxy.R
Last active July 30, 2021 08:48
How get devtools::install_github() working behind a proxy that messes with the SSL certs
library(httr)
library(devtools)
# make httr set CURL to ignore SSL verification problems
# (needed if the SSL proxy replaces certs with its own)
set_config(config(ssl.verifypeer = 0L))
# set proxy details
set_config(use_proxy("10.10.10.10",8080))
@datalove
datalove / spark_pi_cluster.md
Last active September 16, 2020 09:49
Getting Spark up and running on RPi

Before starting

  • Download Spark 1.4 to your local machine (laptop, or PC)
  • Go to 192.168.1.1 to get local IPs for newly connected RPis

Configure each Raspberry Pi

Log into the new Raspberry Pi from your machine

  • ssh pi@192.168.1.XXX (default password for pi user is raspberry)
@datalove
datalove / ROracle_OSX.md
Last active December 2, 2017 02:01
ROracle on OS X

Running ROracle on OSX

Installing Oracle Instant Client

Go to http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html and look for something like this:

Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications Download instantclient-basic-macos.x64-11.2.0.4.0.zip (62,794,903 bytes)

Once the file is downloaded, follow the instructions at the bottom of the download page (reproduced below)

@datalove
datalove / terr_RinR.r
Created August 14, 2014 01:06
How to use TERR's RinR package inside TERR/Spotfire
# Download: http://tap.tibco.com/ -> "Samples" tab -> RinR -> "Try Now" button
# Docs: http://docs.tibco.com/pub/enterprise-runtime-for-R/2.5.0/doc/html/RinR/RinR-package.html
########################################
# Setup
########################################
library(RinR)
library(Sdatasets)
@datalove
datalove / non_generic_function
Last active January 31, 2017 18:18
How to take an R function that isn't generic, make it generic and add method for your class without breaking the standard functionality
identical <- function(x) UseMethod("identical")
identical.my_class <- function(x) do_something(x)
identical.default <- base::identical
@datalove
datalove / preso.R
Created September 9, 2015 22:54
R preso for WARG
```{r, echo = FALSE}
library(knitr)
library(plyr)
library(data.table)
library(dplyr)
gears <- mtcars$gear
mtcars <- mtcars[,1:6]
mtcars$gear <- gears
@datalove
datalove / vectorised_hash_tables.R
Created August 25, 2015 03:21
Fast lookups using R's evironments, which are implemented using hash tables (vectorised)
# inspired by https://stackoverflow.com/questions/16782518/is-this-a-good-way-to-make-a-multi-dimensional-dictionary-in-r/16782745#16782745
# and http://broadcast.oreilly.com/2010/03/lookup-performance-in-r.html
hash <- function( ) {
new.env( hash = TRUE, parent = emptyenv() )
}
set <- function(key, val, hash) {
invisible(mapply(assign, key, val, MoreArgs = list(envir = hash)))
}
@datalove
datalove / xl_col_to_num.R
Last active August 29, 2015 14:28
R function to convert excel column names ('Q','AD', etc) to numbers
xl_col_to_num <- function(column) {
# calc value for a letter given its position i
# eg for'CZ', C's contrib is 26^(1-1)*3 and Z's contrib is 26^(2-1)*26
calc_pos_val <- function(x,i) 26^(i-1) * which(LETTERS == x)
# for a single column like 'CZ', calc each positional value and sum the total
single_col <- function(x) calc_pos_val %>% mapply(x, rev(seq_along(x)) ) %>% sum()
column %>%
@datalove
datalove / xlsxToR.r
Last active August 29, 2015 14:27 — forked from schaunwheeler/xlsxToR.r
Import an xlsx file into R by parsing the file's XML structure.
# The MIT License (MIT)
#
# Copyright (c) 2012 Schaun Jacob Wheeler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@datalove
datalove / in_place_sub.R
Last active August 29, 2015 14:25
In-place string substitution in R. Example: s("my name is $x and time is $y")
s <- function(str) {
require(stringr)
require(magrittr)
# helper functions
get_vars <- function(str) str %>% str_extract_all("\\$[a-zA-Z0-9_\\.]+") %>% unlist %>% str_replace_all("\\$","")
eval_var <- function(var) eval(parse(text = var)))
add_token <- function(var) paste0("\\$", var)