Skip to content

Instantly share code, notes, and snippets.

View revodavid's full-sized avatar

David Smith revodavid

View GitHub Profile
@revodavid
revodavid / holehiders.R
Created June 2, 2017 14:54
Function to detect when a Minecraft player is standing in a hole. (Required miner package.)
findMyId <- function() {
# Requires https://github.com/ROpenSciLabs/miner
# dig a hole one block deep and stand in it to be found
# returns the IDs of players standing in a hole
ids <- getPlayerIds()
holehiders <- NULL
for (id in ids) {
pos <- getPlayerPos(id, tile=TRUE)
surround <- getBlocks(pos[1]-1,pos[2],pos[3]-1, pos[1]+1, pos[2], pos[3]+1)[,1,]
inhole <- all(c(surround[1,2],surround[2,1],surround[2,3], surround[3,2])!=0)
library(doAzureParallel)
# generate a credentials json file "cluster", then...
registerDoAzureParallel(cluster)
# Run 5 million option pricing simulations
closingPrices <- foreach(i = 1:50, .combine='c') %dopar% {
replicate(100000, getClosingPrice())
}
@revodavid
revodavid / birthdaysim.R
Last active January 24, 2018 23:39
R function to simulate the Birthday paradox
pbirthdaysim <- function(n, nsims=100000, feb29=TRUE) {
## Using nsims simulations, estimate the probability
## that a room of n people includes a shared birthday
bdays <- 1:366
## Feb 29 represented as day 366
## We'll sample other days 4 times as often
## compared to day 366
probs <- c(rep(4,365),1)
library(doParallel)
cl <- makeCluster(2)
registerDoParallel(cl)
bdayp <- foreach(n=1:100) %dopar% pbirthdaysim(n)
bdayp <- unlist(bdapy)
{
"name": "davidsmiDAP8",
"vmSize": "Standard_D2_v2",
"maxTasksPerNode": 8,
"poolSize": {
"dedicatedNodes": {
"min": 8,
"max": 8
},
"lowPriorityNodes": {
{
"name": "davidsmiDAP8p",
"vmSize": "Standard_D2_v2",
"maxTasksPerNode": 4,
"poolSize": {
"dedicatedNodes": {
"min": 0,
"max": 0
},
"lowPriorityNodes": {
---
title: "Outliers are a matter of opinion?"
author: "Antony Unwin"
date: ' '
output: html_document
---
```{r include=FALSE}
library(ggplot2)
library(ggthemes)
library(OutliersO3)
library(tidyverse)
library(stringr)
library(stringi)
simplify_name <- function(n)
## Extract just the name, removing any quotes, and normalize accented characters
stri_trans_general(str_trim(str_match(n,"^[\'\"]?([^\'\",(<]+).*<")[,2]),"latin-ascii")
pkglist <- tools::CRAN_package_db()
pkglist <- tbl_df(pkglist[-40]) ## remove duplicate MD5sum column
pkglist %>%
select(Maintainer, Author, Package) %>%
random_image <- function() {
## Return the URL random image in Wikimedia Commons
random_query <- paste0("https://commons.wikimedia.org/w/api.php?",
"action=query",
"&generator=random", # get a random page
"&grnlimit=1", # return 1 page
"&grnnamespace=6", # category: File
"&prop=imageinfo",
"&iiprop=url|size|extmetadata",
"&iiurlheight=1080", # limit images height (sometimes)
image_caption <- function(URL) {
reqURL = paste0(vision_api_endpoint,
"/analyze?visualFeatures=Description",
"&details=Celebrities,Landmarks")
APIresponse = POST(url = reqURL,
content_type('application/json'),
add_headers(.headers = c('Ocp-Apim-Subscription-Key' = vision_api_key)),
body=list(url = URL),
encode = "json")