R이든 파이썬이든 코드를 돌리다가 에러가 나는 경우가 많다. 코드가 정확하더라도 크롤링이나, 마크다운, 셀레늄 등의 작업은 응답속도 지연이나 알 수 없는 이유로 중단되는 경우가 있다.

특히 for나 while같은 루프 함수를 돌리다가 문제가 생기는 경우 상당히 일이 귀찮아 지는데 자동으로 코드를 다시 돌리는 함수를 미리 만들어 두면 상당히 유용하다.

여기서는 retry함수라고 명명하고 아래와 같은 함수를 만들어본다.

retry <- function(expr, isError=function(x) "try-error" %in% class(x), maxErrors=5, sleep=0) {
  attempts = 0
  retval = try(eval(expr))
  while (isError(retval)) {
    attempts = attempts + 1
    if (attempts >= maxErrors) {
      msg = sprintf("retry: too many retries [[%s]]", capture.output(str(retval)))
      flog.fatal(msg)
      stop(msg)
    } else {
      msg = sprintf("retry: error in attempt %i/%i [[%s]]", attempts, maxErrors, 
                    capture.output(str(retval)))
      flog.error(msg)
      warning(msg)
    }
    if (sleep > 0) Sys.sleep(sleep)
    retval = try(eval(expr))
  }
  return(retval)
}

사용방법은 아래와 같다.

retry(사용할 코드, maxErrors=10, sleep=2)
* maxErrors = 시도할 횟수
* sleep = 시도와 시도사이 쉬는 시간(단위: 초)

for (DEPARTMENT in unique(DATA$회사명)){
                                retry(rmarkdown::render("Report-Letter.Rmd", 
                                output_format = "pdf_document",
                                output_file = paste("report_", DEPARTMENT, ".pdf", sep=''),
                                encoding = "UTF-8"),
                                maxErrors=10, 
                                sleep=2)
}

예를 들어 위와 같은 코드를 만들었다고 치면 Report-Letter.Rmd파일을 PDF파일로 변경할 때 실패하게 되면 2초 간격으로 최대 10번 시도하게 되며 성공시 바로 다음 루프로 넘어가게 된다.

여기서 소개하는 함수 외에도 RETRY라는 함수가 있는데 해당 함수가 에러를 제대로 캐치하지 못하고 넘기는 경우가 있어서 웬만하면 retry함수를 만들어서 쓰는 중이다.

카테고리: IT

17개의 댓글

Garage Door Service Company Okmulgee · 2022-01-07 9:03 오전

so much wonderful info on here, : D.

wordpress step by step · 2022-01-11 3:09 오후

so much fantastic information on here, : D.

Davidamire · 2023-01-23 5:32 오전

https://drugsoverthecounter.shop/# over the counter medicine for uti

DonaldJep · 2023-02-07 11:31 오후

Cautions. Prescription Drug Information, Interactions & Side.
nexium prices
earch our drug database. Some trends of drugs.

Davidmob · 2023-02-08 4:28 오전

Read information now. All trends of medicament.
https://nexium.top/# generic nexium price
drug information and news for professionals and consumers. Long-Term Effects.

WillieKef · 2023-02-08 9:00 오전

Get warning information here. Read now.
where to buy nexium without a prescription
Actual trends of drug. п»їMedicament prescribing information.

Davidmob · 2023-02-11 10:08 오전

Read information now. Read information now.
where to get mobic
Top 100 Searched Drugs. Everything what you want to know about pills.

Philipgrali · 2023-02-16 7:08 오후

Some are medicines that help people when doctors prescribe. Medicament prescribing information.
https://edonlinefast.com the best ed pill
Generic Name. Comprehensive side effect and adverse reaction information.

RobertGed · 2023-02-17 12:03 오전

Get information now. Drugs information sheet.
what are ed drugs
Drugs information sheet. Drugs information sheet.

MortonBiali · 2023-02-17 4:28 오전

Everything what you want to know about pills. Get here.
ed pills that work
Get here. Some trends of drugs.

Philipgrali · 2023-02-17 5:15 오후

drug information and news for professionals and consumers. Everything about medicine.
https://edonlinefast.com ed pills gnc
Read information now. Commonly Used Drugs Charts.

MortonBiali · 2023-02-18 2:07 오전

Drug information. Actual trends of drug.
https://edonlinefast.com ed pills
Everything information about medication. Read now.

EfrenBeils · 2023-02-18 12:23 오후

Medscape Drugs & Diseases. п»їMedicament prescribing information.
ed meds online
Everything information about medication. Definitive journal of drugs and therapeutics.

MichaelRot · 2023-02-20 10:30 오전

Generic Name. Medscape Drugs & Diseases.
drugs from canada
Read information now. Read here.

RobertGed · 2023-02-22 12:00 오후

Read information now. Some trends of drugs.
https://canadianfast.online/# prescription drugs without doctor approval
Comprehensive side effect and adverse reaction information. Everything about medicine.

Alita · 2023-05-12 7:14 오후

This article extremely helpful, I have learnt so much from it.

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다