#title R의 기능들
[[TableOfContents]]

==== 예외처리(tryCatch) ====
{{{
err <- F
err <- tryCatch({cal <- replace("aaa", "aaa") }, #replace 함수의 사용이 잘못되었다.
     error = function(e){return(T)}, warning = function(e){return(T)})
err #err의 값이 T로 바뀌었다
}}}
==== Pipe 사용하기 ====
 * http://freesearch.pe.kr/archives/4145

==== 바이트 코드로 컴파일하기(성능향상) ====
{{{
x <- 1:10000000
y <- 1:10000000

#함수
f <- function(){
  for(i in 1:length(x)){
    z[i] <<- i[i] + y[i]
  }
}

#바이트코드 컴파일
library(compiler)
cf <- cmpfun(f)
}}}

결과
{{{
> start.time <- Sys.time()
> z <- x + y #벡터 연산
> Sys.time() - start.time
Time difference of 0.03900194 secs
> start.time <- Sys.time()
> f() #R 함수
> Sys.time() - start.time
Time difference of 24.81421 secs
> start.time <- Sys.time()
> cf() #바이트코드로 컴파일된 R 함수
> Sys.time() - start.time
Time difference of 5.823333 secs
}}}

==== Including arguments in R CMD BATCH mode ====
출처: http://www.r-bloggers.com/including-arguments-in-r-cmd-batch-mode/
{{{
R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out &
}}}

test.R
{{{
##First read in the arguments listed at the command line
args=(commandArgs(TRUE))

##args is now a list of character vectors
## First check to see if arguments are passed.
## Then cycle through each element of the list and evaluate the expressions.
if(length(args)==0){
    print("No arguments supplied.")
    ##supply default values
    a = 1
    b = c(1,1,1)
}else{
    for(i in 1:length(args)){
         eval(parse(text=args[[i]]))
    }
}
print(a*2)
print(b*3)
}}}

==== r object 저장/불러오기 ====
http://www.fromthebottomoftheheap.net/2012/04/01/saving-and-loading-r-objects/
{{{
fit1 <-lm(Volume~., data=trees)
saveRDS(fit1, "d:\\fit1.rds")

fit1 <- readRDS("d:\\fit1.rds")
summary(fit1)
}}}

==== install.packages 예제 ====
{{{
install.packages("RMySQL", repos='http://cran.r-project.org', dependencies = TRUE)

install.packages("foo.zip", repos = NULL)
}}}

==== PMML ====
{{{
library(nnet)
model <- nnet(Species~., data=iris, size=4)

library(pmml)
pmml(model)
}}}

==== R-Stuido Server 설치하기 ====
 * [R-Stuido Server 설치하기]