2012-08-02 12 views
6

मैं अपने आर कोड में संभावित त्रुटि संदेशों को कैप्चर करने के लिए tryCatch का उपयोग करता हूं, लेकिन मुझे 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 

तो त्रुटि पर कब्जा कर लिया नहीं है। इससे भी अधिक: त्रुटियों के इन प्रकार पर कब्जा करने के

> 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" 

कोई भी विचार: मैं ggplot वर्ग के साथ एक अनुसंधान वस्तु मिल सकता है?


अद्यतन: मैं तथ्य यह है कि इसके बाद के संस्करण कॉल कुछ स्थितियों में ठीक हो सकता है (जो बाद में एक परत को जोड़ना) के बारे में पता कर रहा हूँ। लेकिन तब निम्न उदाहरण पर विचार करें (कृपया ध्यान दें कि हम एक टाइपो वहाँ है: twt के बजाय y अक्ष पर प्रदान की जाती है):

> 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 काम करता है, लेकिन प्रिंट करने का प्रयास करते समय त्रुटि केवल बाद में होती है। इसका मतलब है आप print बयान के आसपास 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

बहुत बढ़िया, बहुत बहुत शुक्रिया! मैं इसे कैसे याद कर सकता हूं? :) आपकी पहली पंक्ति में एक नज़र लेने के बाद यह बहुत स्पष्ट था, लेकिन इस सवाल ने मुझे इससे पहले कुछ घंटे सिरदर्द दिया। तो संक्षेप में: धन्यवाद फिर से :) – daroczig

+0

खुशी है कि मैं मदद कर सकता हूँ! कभी-कभी कोई कोड के बहुत करीब हो सकता है ... – Andrie