Skip to content

Instantly share code, notes, and snippets.

View leoluyi's full-sized avatar
🎯
Focusing

Leo Lu leoluyi

🎯
Focusing
View GitHub Profile
@leoluyi
leoluyi / gtk_install.md
Last active July 20, 2017 15:09 — forked from sebkopf/gtk_install.md
Installation information for R with GTK on Windows/Mac OS

Installation information for R with GTK+

Windows

Install the newest version of R. Additionally, I highly recommend R-Studio for working with R regularly (but the basic command line will work just fine for most applications). Once R is installed, you can install GTK directly from within R (details below). In short:

  1. From the R command line (e.g. in R-Studio), install the RGtk2 package by running: install.packages("RGtk2", depen=T)
@leoluyi
leoluyi / devtols_install_github_behind_proxy.R
Created July 12, 2017 10:45 — forked from datalove/devtols_install_github_behind_proxy.R
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))
@leoluyi
leoluyi / RFM_try.R
Created May 25, 2017 03:41
消費者分層-RFM模型
library(magrittr)
library(dplyr)
sales <- data_frame("cust_id" = sample(1000:1999, replace = T, size = 10000),
"sales_value" = abs(round(rnorm(10000, 28, 13))),
"date" = as.Date("2012/1/1") + 700 * sort(stats::runif(10000)))
# sales_value: 產生均值為28,方差為13的1萬個數,用來模擬用戶的消費情況
# cust_id: 從1000到1999這些數字中,# 有放回抽樣進行取樣,
# 一共取1萬筆消費記錄,平均每個樣本取10次
# date: 產生1萬個服從0,1上均勻分佈的數
@leoluyi
leoluyi / text_preprocess.py
Created May 23, 2017 15:18
Python text mining
# https://datawarrior.wordpress.com/2015/08/12/codienerd-1-r-or-python-on-text-mining/
# import all necessary libraries
from nltk.stem import PorterStemmer
from nltk.tokenize import SpaceTokenizer
from nltk.corpus import stopwords
from functools import partial
from gensim import corpora
from gensim.models import TfidfModel
import re
@leoluyi
leoluyi / tor.R
Created March 22, 2017 03:34
Using TOR in R
# Installing TOR on mac: brew install tor
# Run TOR on custom port: tor --SOCKSPort 9050
# Check the 'origin' field in the response to verify TOR is working.
library(httr)
GET("https://httpbin.org/get", use_proxy("socks5://localhost:9050"))
# Set proxy in curl
library(curl)
h <- new_handle(proxy = "socks5://localhost:9050")
@leoluyi
leoluyi / README.md
Created March 14, 2017 05:48 — forked from jcheng5/README.md
Using arbitrary Leaflet plugins with Leaflet for R

Using arbitrary Leaflet JS plugins with Leaflet for R

The Leaflet JS mapping library has lots of plugins available. The Leaflet package for R provides direct support for some, but far from all, of these plugins, by providing R functions for invoking the plugins.

If you as an R user find yourself wanting to use a Leaflet plugin that isn't directly supported in the R package, you can use the technique shown here to load the plugin yourself and invoke it using JS code.

library(magrittr)
library(httr)
library(rvest)
library(data.table)
library(stringr)
library(parallel)
set_config(config(ssl_verifypeer = 0L))
list(
library(rvest)
library(httr)
library(stringr)
library(magrittr)
library(data.table)
# library(quantmod)
set_config(config(ssl_verifypeer = 0L))
# 取得上市股票類別
@leoluyi
leoluyi / shp2gj.py
Created January 18, 2017 05:37 — forked from frankrowe/shp2gj.py
PyShp, shp to geojson in python
import shapefile
# read the shapefile
reader = shapefile.Reader("my.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
buffer.append(dict(type="Feature", \
@leoluyi
leoluyi / convert.py
Created January 16, 2017 05:38 — forked from jwass/convert.py
Simple Shapefile to GeoJSON converter. Using the shapefile from here: http://www.mass.gov/anf/research-and-tech/it-serv-and-support/application-serv/office-of-geographic-information-massgis/datalayers/senate2012.html it will result in an error "ValueError: Record's geometry type does not match collection schema's geometry type: 'Polygon' != 'Unk…
import fiona
import fiona.crs
def convert(f_in, f_out):
with fiona.open(f_in) as source:
with fiona.open(
f_out,
'w',
driver='GeoJSON',
crs = fiona.crs.from_epsg(4326),