Skip to content

Instantly share code, notes, and snippets.

@minhoryang
Last active April 17, 2017 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhoryang/386329c2e09a0f2fe71767790f85dca0 to your computer and use it in GitHub Desktop.
Save minhoryang/386329c2e09a0f2fe71767790f85dca0 to your computer and use it in GitHub Desktop.
"R거지", R the hard way

R거지, R the hard way

why?

files:

  • main.R

  • _library.R

  • _read_csv.R

  • _assign.R
    • R의 라이브러리 함수들은 종종 data frame의 key를 그냥 object처럼 넘겨버릴 때가 있음.
    • R의 변수를 key/value로 관리하고 싶어서 만듬.

how?

  • stopifnot()
  • assign()/get()
  • function(){}
  • source()
ASSIGN = function (.KEY, .VALUE) {
stopifnot(is.character(.KEY))
print(.KEY)
stopifnot(is.function(.VALUE))
print(.VALUE)
assign(.KEY, .VALUE(get(.KEY)), inherits=TRUE)
}
# _library.R
IS_LIBRARY_EXISTS = function(.NAME) {
length(find.package(.NAME, quiet = TRUE)) != 0
}
LIBRARY = function(.NAME, .TYPE=getOption("pkgType")) {
stopifnot(is.character(.NAME))
if (!IS_LIBRARY_EXISTS(.NAME)) {
install.packages(.NAME, type=.TYPE)
}
if (IS_LIBRARY_EXISTS(.NAME)) {
suppressPackageStartupMessages({
library(.NAME, character.only=TRUE)
})
TRUE
} else {
FALSE
}
}
JAVA_LIBRARY = function(.NAME, .TYPE=getOption("pkgType"), .LIBJVM_SO='/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/server/libjvm.dylib') {
if (Sys.getenv('JAVA_HOME') == '') { # FIXME.
dyn.load(.LIBJVM_SO)
}
LIBRARY(.NAME, .TYPE)
}
RENAME_VARIABLE = function(.FILENAME) {
stopifnot(is.character(.FILENAME))
. = gsub('/', '_', .FILENAME)
. = gsub('-', '_', .)
.
}
READ_CSV = function(.FILENAME) {
source('_library.R')
stopifnot(LIBRARY('readr'))
stopifnot(is.character(.FILENAME))
. = RENAME_VARIABLE(.FILENAME)
assign(., read_csv(file=.FILENAME), inherits=TRUE)
.
}
# 0. initialize
rm(list=ls())
setwd('~/projects/LOVER/')
source('_read_csv.R')
key1 = READ_CSV('watcha-doc2vec-regression/reviews1_11.csv')
key2 = READ_CSV('watcha-doc2vec-regression/reviews12_27.csv')
source('_library.R')
stopifnot(LIBRARY('dplyr'))
database = full_join(get(key1), get(key2))
rm(key1, key2, list=c(key1, key2))
source('_assign.R')
ASSIGN('database', function (data) {
. = subset(data, select=c('movie_title', 'text'))
na.omit(.)
})
stopifnot(LIBRARY('KoNLP'))
useSejongDic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment