2012-08-02 8 views
6

나는 내 R 코드에서 잠재적 인 오류 메시지를 캡처하기 위해 많은 것을 사용하지만 오늘은 ggplot과 같은 이상한 문제에 직면했다. 실제로 어떤 일이 벌어지고 있는지 전혀 알지 못한다. Google/SO 검색). 불완전한 ggplot 호출 (더 추가 레이어)와tryCatch 및 ggplot

간단한 예 :

> tryCatch(eval(parse(text = 'ggplot(mtcars, aes(x=hp, y=wt))')), error = function(e) e) 
Error: No layers in plot 

그래서 오류가 캡처되지 않습니다. 더욱 : 나는 ggplot 클래스와 함께 R 객체를 얻을 :

> str(tryCatch(eval(parse(text = 'ggplot(mtcars, aes(x=hp, y=wt))')), error = function(e) e)) 
List of 8 
$ data  :'data.frame': 32 obs. of 11 variables: 
    ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... 
    ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ... 
    ..$ disp: num [1:32] 160 160 108 258 360 ... 
    ..$ hp : num [1:32] 110 110 93 110 175 105 245 62 95 123 ... 
    ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... 
    ..$ wt : num [1:32] 2.62 2.88 2.32 3.21 3.44 ... 
    ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ... 
    ..$ vs : num [1:32] 0 0 1 1 0 1 0 1 1 1 ... 
    ..$ am : num [1:32] 1 1 1 0 0 0 0 0 0 0 ... 
    ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ... 
    ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ... 
$ layers  : list() 
$ scales  :Reference class 'Scales' [package "ggplot2"] with 1 fields 
    ..$ scales: NULL 
    ..and 20 methods, of which 9 are possibly relevant: 
    .. add, clone, find, get_scales, has_scale, initialize, input, n, 
    .. non_position_scales 
$ mapping :List of 2 
    ..$ x: symbol hp 
    ..$ y: symbol wt 
$ options :List of 1 
    ..$ labels:List of 2 
    .. ..$ x: chr "hp" 
    .. ..$ y: chr "wt" 
$ coordinates:List of 1 
    ..$ limits:List of 2 
    .. ..$ x: NULL 
    .. ..$ y: NULL 
    ..- attr(*, "class")= chr [1:2] "cartesian" "coord" 
$ facet  :List of 1 
    ..$ shrink: logi TRUE 
    ..- attr(*, "class")= chr [1:2] "null" "facet" 
$ plot_env :<environment: R_GlobalEnv> 
- attr(*, "class")= chr "ggplot" 

어떤 아이디어 오류 이런 종류의를 캡처?


업데이트 : 나는 위의 호출은 (나중에 레이어를 추가 같은) 어떤 상황에서 확인 될 수 있다는 사실을 알고 있습니다. 그러나 다음의 예를 고려하시기 바랍니다 (우리가 거기에 오타가 있음을 유의하시기 바랍니다 : t 대신 wty 축에 제공됩니다) :

> tryCatch(eval(parse(text = 'ggplot(mtcars, aes(x=hp, y=t)) + geom_point()')), error = function(e) e) 
Error in data.frame(x = c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123, : 
    arguments imply differing number of rows: 32, 0 

나는 오류 메시지를받을 수 있나요하지만이 tryCatch에 의해 캡처 아닙니다.

답변

8

이 오류는 ggplot을 호출 할 때 오류가 발생하지 않지만 print.ggplot을 호출하면 발생합니다.

따라서 tryCatch이 작동하지만 나중에 인쇄 할 때 오류가 발생합니다.

대신이 시도 :


그리고 두 번째 예를 들어

x <- tryCatch(eval(parse(text = 'ggplot(mtcars, aes(x=hp, y=wt))')), 
       error = function(e) e) 
tryCatch(print(x), error = function(e) e) 
<simpleError: No layers in plot> 
:

x <- eval(parse(text = 'ggplot(mtcars, aes(x=hp, y=t)) + geom_point()')) 
tryCatch(print(x), error = function(e) e) 

<simpleError in data.frame(x = c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175, 66, 91, 113, 264, 175, 335, 109), y = structure(function (x) standardGeneric("t"), generic = structure("t", package = "base"), package = "base", group = list(), valueClass = character(0), signature = "x", default = structure(function (x) UseMethod("t"), target = structure("ANY", class = structure("signature", package = "methods"), .Names = "x", package = "methods"), defined = structure("ANY", class = structure("signature", package = "methods"), .Names = "x", package = "methods"), generic = structure("t", package = "base"), class = structure("derivedDefaultMethod", package = "methods")), skeleton = structure(function (x) UseMethod("t"), target = structure("ANY", class = structure("signature", package = "methods"), .Names = "x", package = "methods"), defined = structure("ANY", class = structure("signature", package = "methods"), .Names = "x", package = "methods"), generic = structure("t", package = "base"), class = structure("derivedDefaultMethod", package = "methods"))(x), class = structure("standardGeneric", package = "methods")),  PANEL = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L), check.names = FALSE, stringsAsFactors = TRUE): arguments imply differing number of rows: 32, 0> 
+1

최고, 덕분에 많은 이것은 당신이 print 문 주위에 tryCatch을 넣어 의미! 어떻게 내가 이것을 놓칠 수 있니? :) 첫 번째 줄을 한 눈에 살펴본 후 꽤 분명했지만이 질문은 두 시간 전에 저에게 두통을주었습니다. 그래서 짧게 : 다시 한번 감사드립니다 :) – daroczig

+0

기꺼이 도와 드리겠습니다! 때로는 코드에 너무 가까울 수도 있습니다 ... – Andrie