R에서 함수에서 에러를 내고 멈추는 함수인 stop()이 있다. 

https://stat.ethz.ch/R-manual/R-devel/library/base/html/stop.html



해당 함수의 실제 용법을 본다.



httr 모듈을 사용한 R elasticsearch client (search_POST)를 살펴본다. 

202 이상의 Http 상태 값을 가지면 stop() 함수가 호출된다. 


https://github.com/ropensci/elastic/blob/2e3a72e42f14d73560a5367195cc2461acc32975/R/search.r

search_POST <- function(path, index=NULL, type=NULL, args, body, raw, asdf, ...) {

  checkconn()

  conn <- es_get_auth()

  url <- make_url(conn)

  if (is.null(index) && is.null(type)) {

    url <- paste(url, path, sep = "/")

  } else {

    if (is.null(type) && !is.null(index)) {

      url <- paste(url, index, path, sep = "/")

    } else {

      url <- paste(url, index, type, path, sep = "/")

    }

  }

  url <- prune_trailing_slash(url)

  body <- check_inputs(body)

  tt <- POST(url, make_up(), ..., query = args, body = body)

  if (tt$status_code > 202) stop(error_parser(tt, 1), call. = FALSE)

  res <- content(tt, as = "text")

  if (raw) res else jsonlite::fromJSON(res, asdf)

}





만약 stop()함수 결과를 처리하려면 어떻게 할지에 대한 결과처리는 다음과 같다. 

stop() 함수 결과는 에러 메시지는 character 타입을 가지고 있으며 "try-error" 클래스 이다. 이에 맞는 처리를 해주면 정상처리할 수 있다. 아래 코드는 while 루프에서 루프를 나갈 수 있는 조건으로 활용하고 있다. 



while(true) {

..

scroll_result <- try(search_POST(url = conn$baseurl, path = "_search/scroll?scroll=10m", body = scroll_id))

if (typeof(scroll_result) == "character" && class(scroll_result) == "try-error") {
    break;

}
..
}




'R' 카테고리의 다른 글

[R] RStudio를 특정 디렉토리에서 작업하기  (0) 2015.08.12
[R] R에서 메모리 정리하기  (0) 2015.08.12
[R] List Iterator  (0) 2015.08.06
R 의 함수 aliasing (별명)  (0) 2015.08.06
[R] rjson으로 data frame 으로 만들기  (0) 2015.08.05
Posted by '김용환'
,