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 7619343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:39:50+00:00 2026-05-31T03:39:50+00:00

I have a wrapper around the in-built warning() function in R that basically calls

  • 0

I have a wrapper around the in-built warning() function in R that basically calls warning(sprintf(...)):

warningf <- function(...)
    warning(sprintf(...))

This is because I use warning(sprintf(...)) so often that I decided to make a function out of it (it’s in a package I have of functions I use often).

I then use warningf when I write functions. i.e., instead of writing:

f <- function() {
    # ... do stuff
    warning(sprintf('I have %i bananas!',2))
    # ... do stuff
}

I write:

f <- function() {
    # ... do stuff
    warningf('I have %i bananas!',2)
    # ... do stuff
}

If I call the first f(), I get:

Warning message:
In f() : I have 2 bananas!

This is good – it tells me where the warning came from f() and what went wrong.

If I call the second f(), I get:

Warning message:
In warningf("I have %i bananas!",2) : I have 2 bananas!

This is not ideal – it tells me the warning was in the warningf function (of course, because it’s the warningf function that calls warning, not f), masking the fact that it actually came from the f() function.

So my question is : Can I somehow “raise” the warning call so it displays the warning in f() message instead of the warning in warningf ?

  • 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-05-31T03:39:51+00:00Added an answer on May 31, 2026 at 3:39 am

    One way of dealing with this is to get a list of the environments in your calling stack, and then pasting the name of the parent frame in your warning.

    You do this with the function sys.call() which returns an item in the call stack. You want to extract the second from last element in this list, i.e. the parent to warningf:

    warningf <- function(...){
      parent.call <- sys.call(sys.nframe() - 1L)
      warning(paste("In", deparse(parent.call), ":", sprintf(...)), call.=FALSE)
    }  
    

    Now, if I run your function:

    > f()
    Warning message:
    In f() : I have 2 bananas! 
    

    Later edit : deparse(parent.call) converts the call to a string in the case that the f() function had arguments, and shows the call as it was specified (ie including arguments etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wrapper around a C++ function call which I call from C#
I have a C++/CLI wrapper around native .lib and .h files. I use the
I have some written a number of unit tests that test a wrapper around
I have a collection of unmanaged dlls with a C# wrapper around them that
I'm writing a wrapper around a class library that I have no control over.
I have the following, fairly standard code as a wrapper around CSharpCodeProvider . This
I have a custom wrapper function around jquery ajax. custom.get = function (path, callback)
I have a class written in c# which is acting as a wrapper around
I have a wrapper generic class that intended to be used with a set
I have a script, which is essentially a wrapper around an executable by the

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.