Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:12:43+00:00 2026-06-09T02:12:43+00:00

I use tryCatch a lot to capture potential error messages in my R code,

  • 0

I use tryCatch a lot to capture potential error messages in my R code, but I faced a strange issue today with ggplot – and I really do not have any idea what is going on (even after some time spent on Google/SO searching).

Simple example with an incomplete ggplot call (no added layer):

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

So the error is not captured. Even more: I get an R object with ggplot class:

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

Any ideas to capture these kind of errors?


Update: I am aware of the fact that the above call might be OK in some situations (like adding a layer later). But then please consider the following example (please note that we have a typo there: t is provided on y axis instead of wt):

> 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

I do get the error message, but that’s not captured by tryCatch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:12:45+00:00Added an answer on June 9, 2026 at 2:12 am

    This happens because the error doesn’t occur in the call to ggplot, but in the call to print.ggplot.

    So your tryCatch works, but the error occurs only later, when trying to print. This means you have to put the tryCatch around the print statement:

    Try this instead:

    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>
    

    And for your second example:

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a strange issue in R. Consider the following code (a really simplified
When I use tryCatch , I can define an error handler and use conditionCall
Possible Duplicate: When to use try/catch blocks? Main method code entirely inside try/catch: Is
In some old Java code, I found a class that contains a lot of
As mentioned elsewhere , this code has the potential for a race condition: //
So I use Qt a lot with my development and love it. The usual
When i use @try @catch, i get error: expected identifier or '(' before 'try'
When I use Try/Catch, is there a way just like If/Else to run code
Here a simple question : What do you think of code which use try
I'm trying to understand how I'm going to use Throw in my code. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.