Skip to content

Instantly share code, notes, and snippets.

library(grDevices)
rsnake <- function(x=8,y=8, auto=NULL, plot=TRUE, detailed=FALSE) {
bodycheck <- function(newhead) {
if(newhead[1] < 1 || newhead[1] > x || newhead[2] < 1 || newhead[2] > y) return(FALSE)
return(sum(sapply(1:length(snake), function(x){identical(snake[[x]], newhead)})) == 0)
}
newfood <- function() {
while(!bodycheck(food <<- c(round(runif(1, min=1, max=x)), round(runif(1,min=1, max=y))))) FALSE
# plusMonth(time, diff)
# POSIXltに変換可能な型で与えられた日付を表すx に対して、diff月増減させた日付をPOSIXctで返す。
# 月だけを増減させ、日付はそのままにするので、検査スケジュールなどを扱うのに適しているか。
# diffは負でもよい。
plusMonth <- function(x, diff=1) { # diff を省略すると1としたことになる
cur <- as.POSIXlt(x)
mon <- cur$mon
cur$year <- cur$year + (mon+diff)%/% 12
cur$mon <- (mon+diff) %% 12
@mokjpn
mokjpn / Konashi-Grove-Temperature
Last active February 18, 2016 23:47
Read Temperature from GROVE analog Temperature Module.
(to name gist)
@mokjpn
mokjpn / Konashi-Grove-Barometer
Last active August 29, 2015 14:13
Read Barometer from GROVE I2C Barometer Module.
( for name )
serial = require('serialport')
mqtt = require 'mqtt'
SerialPort = serial.SerialPort
sp = new SerialPort("/dev/tty.usbserial-XXXXXXXX", { # replace this to your serial port name.
baudrate: 115200,
parser: serial.parsers.readline("\n")
})
atdesk = 0
## modified from http://www.r-bloggers.com/how-to-download-complete-xml-records-from-pubmed-and-extract-data/
# Install XML and RCrul package and call library(XML) and library(RCurl) before use this.
searchPubMed <- function(query.term) {
# change spaces to + in query
query.gsub <- gsub(" ", "+", query.term)
# change single-quotes to URL-friendly %22
query.gsub <- gsub("'","%22", query.gsub)
# Perform search and save history, this will save PMIDS in history
pub.esearch <- getURL(paste("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=",
@mokjpn
mokjpn / Espruino-BME280-WiFi-MQTT.js
Created September 13, 2015 08:37
Using Espruino board, obtain pressure/humidity/temperature data by BME280 sensor, then send it to MQTT server by ESP-WROOM-02(ESP8266) Wi-Fi module.
// Main
console.log("Start!");
// Espruinoのピンのうち, B8とB9ピンをI2C用として使う
I2C1.setup({scl:B8,sda:B9});
console.log("Initialize BME280...");
// BME280用モジュールを読み込んで初期化
var bme = require("BME280").connect(I2C1);
// MQTTサーバにつながるまではデータを送らないためのフラグ
var mqttconnected = false;
@mokjpn
mokjpn / nquiz.R
Created December 9, 2015 12:41
早押しクイズでn問先取の時,問題をx問用意した時にどのくらいの確率で勝ち抜け者が出るか.ただし一人も正解がない場合はないものとする.
# 早押しクイズでn問先取の時,問題をx問用意した時にどのくらいの確率で勝ち抜け者が出るか.ただし一人も正解がない場合はないものとする.
nquiz <- function(x,n) {
if(n > x) {
stop("n must be smaller than or equal to x")
}
sum(sapply(n:x, function(xx) { ncol(combn(x,xx))})) / (2^x)
}
/* SpeedButton Sketch */
/* NeoPixel */
#include <Adafruit_NeoPixel.h>
// Digital pin to connect NeoPixel
#define PIN 0
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
@mokjpn
mokjpn / WikiTable2Dataframe.R
Created February 23, 2016 06:54
Read MediaWiki's WikiTable text and convert it into a R dataframe
# Read MediaWiki's WikiTable text and convert it into a dataframe
lines <- readLines(file.choose())
columns <- NULL
row <- NULL
df <- data.frame()
ncol <- 0
for(line in lines) {
if((m <- sub("^\\! *(.*)$", "\\1", line)) != line){